如何做织梦手机网站,网站建设网站软件有哪些内容,网上培训,企业文化怎么写1、ConfigurationProperties 在类上通过ConfigurationProperties注解声明当前类为属性读取类。 举例#xff1a; ConfigurationProperties(prefix jdbc) prefixjdbc 读取属性文件中#xff0c;前缀为jdbc的值。 在类上定义各个属性#xff0c;名称… 1、ConfigurationProperties 在类上通过ConfigurationProperties注解声明当前类为属性读取类。 举例 ConfigurationProperties(prefix jdbc) prefixjdbc 读取属性文件中前缀为jdbc的值。 在类上定义各个属性名称必须与属性文件中 jdbc. 后面部分一致。 需要注意的是如果我们没有指定属性文件的地址SpringBoot 默认读取 application.properties/application.yml 中的属性文件名。 Data
ConfigurationProperties(prefix jdbc)
class JdbcProperties {private String url;private String driverClaprivate;private String username;private String password;
} 2、EnableConfigurationProperties ConfigurationProperties 注解我们可以理解成用来把 properties 配置文件转化为 Bean 使用的而 EnableConfigurationProperties 注解的作用是让 ConfigurationProperties 注解生效。 如果只配置 ConfigurationProperties 注解在 IOC 容器中是获取不到 properties 配置文件转化的 Bean 的。 那么我们如何获取我们使用了 ConfigurationProperties 注解的类呢 2.1 Autowired 注入 Autowired
private JdbcProperties prop; 2.2 构造函数注入 private JdbcProperties prop;
public JdbcConfig(Jdbcproperties prop){this.prop prop;
} 2.3 声明有Bean的方法参数注入 Bean
public TestBean dataSource(JdbcProperties jdbcProperties) {syso(jdbcProperties.getUsername());// syso 简写return new TestBean;
} 通过上方三种方式都可以在 JdbcProperties jdbcProperties 中直接拿到注入的数据。