springboot中普通JAVA类读取application.yml配置项
springboot中普通JAVA类读取application.yml配置项关键点:需要将类纳入SPRING管理,在类上增加@Componet注解在类中使用@Value指定默认的application.yml文件对应的节点名称使用时需要使用@Autowried注入可能的报错org.springframework.beans.factory.NoSuchBeanDefinitionException
springboot中普通JAVA类读取application.yml配置项
关键点:
需要将类纳入SPRING管理,在类上增加@Componet注解
在类中使用@Value指定默认的application.yml文件对应的节点名称
使用时需要使用@Autowried注入
可能的报错
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.***.***.ConfigBean’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.ann
或是 报 NULL对象指针异常。
原因:启动类所在包未引入@ComponentScan扫描。
application.yml内容:
spring:
datasource:
# oracle
url: jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.15)(PORT=1521))) (CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=okok)))
username: xiawu
password: xxxbbbccc
driver-class-name: oracle.jdbc.driver.OracleDriver
platform: oracle
ConfigBean.java内容:
package com.bbb;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
public class ConfigBean {
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String dbUser;
@Value("${spring.datasource.password}")
private String dbPwd;
@Value("${spring.datasource.driver-class-name}")
private String dbDriver;
public String getDbUrl() {
return dbUrl;
}
public void setDbUrl(String dbUrl) {
this.dbUrl = dbUrl;
}
public String getDbUser() {
return dbUser;
}
public void setDbUser(String dbUser) {
this.dbUser = dbUser;
}
public String getDbPwd() {
return dbPwd;
}
public void setDbPwd(String dbPwd) {
this.dbPwd = dbPwd;
}
public String getDbDriver() {
return dbDriver;
}
public void setDbDriver(String dbDriver) {
this.dbDriver = dbDriver;
}
}
启动类:
假定ConfigBean在com.bbb包下
@ComponentScan (basePackages = {“com.bbb”})
测试类(受Spring管理的类,比如在某个Controller中):
测试类中注入配置类
@Autowired
private ConfigBean configBean;
使用
configBean.getDbUrl();
说明:以上测试类也要属于SPING管理才可以正常调用,否则ConfigBean对象会报空指 针异常。
如果要在非SPRING管理的类中调用,需要先获取到SPRING的ApplicationContext对象,根据该对象中的bean去获取具体配置文件的参数。以下是代码:
获取ApplicationContext对象的工具类:
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static org.springframework.context.ApplicationContext applicationContext;
public static org.springframework.context.ApplicationContext getApplicationContext() {
return applicationContext;
}
public void setApplicationContext(org.springframework.context.ApplicationContext context){
applicationContext = context;
}
}
非SPRING管理的普通类中的调用代码:
ApplicationContext app= ApplicationContextUtil.getApplicationContext();
ConfigBean configBean = app.getBean(ConfigBean.class);
System.out.println("========="+configBean.getDbUrl());
更多推荐
所有评论(0)