Spring 的application.properties项目配置与注解
程序员文章站
2022-05-01 23:27:38
...
application.properties 配置文件
默认启动项目的url配置,不需要加项目路径默认为/ 可以自行修改。
端口默认8080 修改端口号为8888
server.port=8888
server.context-path=/HelloWorld
helloWorld=spring boot
根据key-value直接注入helloWorld
@RestController
public class HelloWorldController {
@Value("${helloWorld}")
private String helloWorld;
@RequestMapping("/helloWorld")
public String say(){
return helloWorld;
}
}
application.properties配置数据库连接
有前缀的属性注入
请求url
mysql.jdbcName=com.mysql.jdbc.Driver
mysql.dbUrl=jdbc:mysql://localhost:3306/db_boot
mysql.userName=root
mysql.password=123456
@Component 让spring加载
@ConfigurationProperties(prefix="mysql") 就是application配置文件的前缀mysql
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Mysql属性配置文件
* @author Administrator
*
*/
@Component
@ConfigurationProperties(prefix="mysql")
public class MysqlProperties {
private String jdbcName;
private String dbUrl;
private String userName;
private String password;
public String getJdbcName() {
return jdbcName;
}
public void setJdbcName(String jdbcName) {
this.jdbcName = jdbcName;
}
public String getDbUrl() {
return dbUrl;
}
public void setDbUrl(String dbUrl) {
this.dbUrl = dbUrl;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@Component已经变为spring管理的bean了@Resource 直接引入
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@Resource
private MysqlProperties mysqlProperties;
@RequestMapping("/showJdbc")
public String showJdbc(){
return "mysql.jdbcName:"+mysqlProperties.getJdbcName()+"<br/>"
+"mysql.dbUrl:"+mysqlProperties.getDbUrl()+"<br/>"
+"mysql.userName:"+mysqlProperties.getUserName()+"<br/>"
+"mysql.password:"+mysqlProperties.getPassword()+"<br/>";
}
}
spring
@Controller 处理http请求的注解,请求后台转发页面
@RequestMapping 映射路径
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/freemarker")
public class HelloWorldFreemarkerController {
@RequestMapping("/say")
public ModelAndView say(){
ModelAndView mav=new ModelAndView();
mav.addObject("message", "springboot!");
mav.setViewName("helloWorld");
return mav;
}
}
helloWorld的模板文件
helloWorld.ftl
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
show: ${message}
</body>
</html>
@PathVariable获取url参数
@RequestParam 获取get或post参数或者是form和url参数
rest风格的资源url请求
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="http://www.java1234.com/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript">
</script>
</head>
<body>
<button onclick="show()">你好</button>
<a href="/HelloWorld/blog/21">天天</a>
<a href="/HelloWorld/blog/query?q=123456">搜索</a>
</body>
</html>
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/blog")
public class BlogController {
@RequestMapping("/{id}")
public ModelAndView show(@PathVariable("id") Integer id){
ModelAndView mav=new ModelAndView();
mav.addObject("id", id);
mav.setViewName("blog");
return mav;
}
@RequestMapping("/query")
public ModelAndView query(@RequestParam(value="q",required=false)String q){
ModelAndView mav=new ModelAndView();
mav.addObject("q", q);
mav.setViewName("query");
return mav;
}
}
blog.ftl
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
id:${id}
</body>
</html>
上一篇: CSS水平和垂直居中技术
下一篇: absolute 元素完全居中的两种方法
推荐阅读