Custom properties
application.properties provides support for custom properties, so we can configure some constants here
mysql.jdbcName=com.mysql.jdbc.Driver
mysql.jdbcUrl=jdbc:mysql://localhost:3306/test
mysql.userName=root
mysql.userPassword=root
Then directly take it out by annotating @Value(value=”${config.name}”) where it is to be used:
package com.example.demo.Controller;
import com.example.demo.properties.MysqlBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class DemoController {
/**
* 数据库的属性的注入
*/
@Value(value="${mysql.jdbcName}")
private String jdbcName;
@Value(value="${mysql.jdbcUrl}")
private String jdbcUrl;
@Value(value="${mysql.userName}")
private String userName;
@Value(value="${mysql.userPassword}")
private String userPassword;
@RequestMapping("/sjk")
public String shk(){
return "mysql.jdbcName:"+jdbcName+"<br/>" +"mysql.jdbcUrl:"+jdbcUrl+"<br/>" +"mysql.userName:"+userName+"<br/>" +"mysql.userPassword:"+userPassword+"</br>";
}
}
In the configuration here, we found that there is a feature. All configurations start with "mysql", and sometimes there are too many attributes, and it is too tiring to bind one by one to the attribute fields. The official advocates binding a bean of an object. Here we build a MysqlProperties .java class, which needs to be declared using the annotation @ConfigurationProperties(prefix = "mysql") at the top:
package com.example.demo.properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Component转换为实体bean
*/
@Component
@ConfigurationProperties(prefix = "mysql")
public class MysqlBean {
private String jdbcName;
private String jdbcUrl;
private String userName;
private String userPassword;
public String getJdbcName() {
return jdbcName;
}
public void setJdbcName(String jdbcName) {
this.jdbcName = jdbcName;
}
public String getJdbcUrl() {
return jdbcUrl;
}
public void setJdbcUrl(String jdbcUrl) {
this.jdbcUrl = jdbcUrl;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
@Resource
private MysqlBean mysqlBean;
@RequestMapping("/sjk")
public String shk(){
return "mysql.jdbcName:"+mysqlBean.getJdbcName()+"<br/>" +"mysql.jdbcUrl:"+mysqlBean.getJdbcUrl()+"<br/>" +"mysql.userName:"+mysqlBean.getUserName()+"<br/>" +"mysql.userPassword:"+mysqlBean.getUserPassword()+"</br>";
}
At this point, you need to add @EnableConfigurationProperties to the spring Boot entry class and specify which bean to load. If you don't write RandomProperties.class, add @Configuration or @Component to the bean class
@SpringBootApplication
@EnableConfigurationProperties({RandomProperties.class})
public class SpringbootdemoApplication
public static void main(String[] args) { SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
Putting all custom configurations in application.properties will be bloated. At this time, we can define another configuration file. Here I named it random.properties, and the path is placed under src/main/resources/waiting.
Here you only need to add @PropertySource("classpath:waiting/random.properties") and @Configuration annotations to the previous bean.
Note: The previous @EnableConfigurationProperties({RandomProperties.class}) method can no longer be taken.
Finally, when the controller layer or service is obtained, it can be obtained with the @Autowired annotation.
Chinese garbled problem
When there is Chinese in the configuration file of .properties, the garbled characters are read. The following operations are required:
1. Add configuration
Set the spring-boot encoding format
banner.charset=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8
1
2
3
4
5
6
7
2. Set the file type
Change the file type of application.properites to the encoding type of UTF-8.
The value obtained through the above method test is still garbled.
Solution
2.1, IDEA
Set the Transparent native-to-ascii conversion of File Encodings to true. The specific steps are as follows:
Click
File -> Settings -> Editor -> File Encodings
to set the Default encoding for properties files under Properties Files (*.properties) to UTF- 8. Check the box before Transparent native-to-ascii conversion.
Annotation
@RequestMapping configures url mapping
@Controller handles http requests
@RestController handles ajax requests
@PathVariable gets url parameters
@RequestParam gets request parameters
@Controller
@RequestMapping("/param")
public class ParamController {
@RequestMapping("/query")
public ModelAndView query(@RequestParam(value = "q",required = false)String q ){
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("q",q);
***modelAndView.setViewName("param");***
return modelAndView;
}
}
There is an error here, modelAndView.setViewName("param"); and @RequestMapping("/param")