目录
用法二:@PropertySource 和 @ConfigurationProperties
用法三:@ConfigurationProperties + application.yml
源码
- package org.springframework.context.annotation;
-
- import java.lang.annotation.Documented;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Repeatable;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
- import org.springframework.core.io.support.PropertySourceFactory;
-
- @Target(ElementType.TYPE)
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Repeatable(PropertySources.class)
- public @interface PropertySource {
-
- /**
- * 属性源的名称
- */
- String name() default "";
-
- /**
- * 属性文件的存放路径
- */
- String[] value();
-
- /**
- * 如果指定的属性源不存在,是否要忽略这个错误
- */
- boolean ignoreResourceNotFound() default false;
-
- /**
- * 属性源的编码格式
- */
- String encoding() default "";
-
- /**
- * 属性源工厂
- */
- Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
-
- }
属性文件:jdbc.properties
- jdbc.driverClassName=jdbc.mysql
- jdbc.url=mysql:jdbc://localhost:3306/ssm_db
- jdbc.username=root
- jdbc.password=123
-
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.PropertySource;
- import org.springframework.stereotype.Component;
-
-
- @Component
- @PropertySource(value = {"classpath:jdbc.properties"})
- public class JdbcProperty {
-
- private String driverClassName;
- private String url;
- private String username;
- private String password;
-
-
- public String getDriverClassName() {
- return driverClassName;
- }
-
- public void setDriverClassName(String driverClassName) {
- this.driverClassName = driverClassName;
- }
-
- public String getUrl() {
- return url;
- }
-
- public void setUrl(String url) {
- this.url = url;
- }
-
- 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;
- }
-
- @Override
- public String toString() {
- return "JdbcProperty{" +
- "driverClassName='" + driverClassName + '\'' +
- ", url='" + url + '\'' +
- ", username='" + username + '\'' +
- ", password='" + password + '\'' +
- '}';
- }
- }
-
- @Component
- @PropertySource(value = {"classpath:jdbc.properties"})
- @ConfigurationProperties(prefix = "jdbc")
- public class JdbcProperty {
-
- private String driverClassName;
- private String url;
- private String username;
- private String password;
-
-
- public String getDriverClassName() {
- return driverClassName;
- }
-
- public void setDriverClassName(String driverClassName) {
- this.driverClassName = driverClassName;
- }
-
- public String getUrl() {
- return url;
- }
-
- public void setUrl(String url) {
- this.url = url;
- }
-
- 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;
- }
-
- @Override
- public String toString() {
- return "JdbcProperty{" +
- "driverClassName='" + driverClassName + '\'' +
- ", url='" + url + '\'' +
- ", username='" + username + '\'' +
- ", password='" + password + '\'' +
- '}';
- }
测试案例:
- @RestController
- public class UserController {
-
- @Autowired
- private UserService userService;
- @Autowired
- JdbcProperty jdbcProperty;
-
- @PostMapping("/save")
- public String save(@RequestParam Map<String, Object> userMap){
- System.out.println(jdbcProperty.toString());
-
- // userService.save(userMap);
- return "save success";
- }
- }
测试结果:
JdbcProperty{driverClassName='jdbc.mysql', url='mysql:jdbc://localhost:3306/ssm_db', username='root', password='123'}
DbConfig.java
- @Configuration
- public class DBConfig {
-
- @Bean
- @ConfigurationProperties(prefix = "jdbc")
- public DataSource dataSource(){
- return new DruidDataSource();
- }
-
- @Bean
- public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
- SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
- factoryBean.setDataSource(dataSource);
- return factoryBean;
- }
-
- }
application.yml
- server:
- port: 1111
- jdbc:
- driverClassName: jdbc.mysql
- url: mysql:jdbc://localhost:3306/ssm_db
- username: root
- password: 123
测试案例: