springboot的参数绑定,先介绍一种常用方式:@ConfigurationProperties
应用场景有2种,一种是在自己建的类上使用,一种是在第三方组件上使用。下面分别介绍:
目录
(1)新建类:
package com.example._02configuration;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Package: com.example._02configuration
* @ClassName: MyDataSource
* @Description: <p>Description: </p>
* @author: zq
* @since: 2020/5/1 21:06
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@Getter
@Setter
@ToString
@Component("myDataSource1")
@ConfigurationProperties(prefix = "db")
/*
@ConfigurationProperties支持松绑定
@ConfigurationProperties(prefix = "db")
比如:private String userName;
允许匹配方式:
db.userName=xx
db.user_name=xx
db.user-name=xx
db_user_name=xx
*/
public class MyDataSource {
/**
* userName
*/
private String userName;
/**
* password
*/
private String password;
/**
* url
*/
private String url;
/**
* driverClass
*/
private String driverClass;
}
(2)在application.properties中新增配置
db.userName=zhangsan
db.password=123456
db.url=jdbc:localhost:3306
db.driverClass=mysql.jdbc.Driver
(3)在controller中调用
package com.example._02configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
/**
* @Package: com.example._02configuration
* @ClassName: DataSourceController
* @Description: <p>Description: </p>
* @author: zq
* @since: 2020/5/1 21:14
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@Controller
public class DataSourceController {
@Resource
MyDataSource myDataSource1;
@RequestMapping("/dataSourcePage1")
@ResponseBody
public String dataSourcePage1() {
return "dataSourcePage1" + myDataSource1;
}
}
运行访问:http://localhost:8084/dataSourcePage1
(1)添加获取组件的引用
@Bean("myDataSource2")
@ConfigurationProperties(prefix = "db")
public MyDataSource getMyDataSource() {
return new MyDataSource();
}
(2)在controller中添加并使用
package com.example._02configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
/**
* @Package: com.example._02configuration
* @ClassName: DataSourceController
* @Description: <p>Description: </p>
* @author: zq
* @since: 2020/5/1 21:14
* @version: 1.0
* @Copyright: 2020 zq. All rights reserved.
*/
@Controller
public class DataSourceController {
@Resource
MyDataSource myDataSource1;
@Resource
MyDataSource myDataSource2;
@RequestMapping("/dataSourcePage1")
@ResponseBody
public String dataSourcePage1() {
return "dataSourcePage1" + myDataSource1;
}
@RequestMapping("/dataSourcePage2")
@ResponseBody
public String dataSourcePage2() {
return "dataSourcePage2" + myDataSource2;
}
}
运行访问:http://localhost:8084/dataSourcePage2
注意:
一、@ConfigurationProperties支持松绑定
@ConfigurationProperties(prefix = "db")
比如:private String userName;
允许匹配方式:
db.userName=xx
db.user_name=xx
db.user-name=xx
db_user_name=xx
二、刚刚引入@ConfigurationProperties时可能会报错“spring boot Configuration Annotation Proessor not found in classpath”
在pom文件中添加以下依赖即可:
<!--解决 @ConfigurationProperties spring boot Configuration Annotation Proessor not found in classpath的问题 begin-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--解决 @ConfigurationProperties spring boot Configuration Annotation Proessor not found in classpath的问题 end-->