赞
踩
在pojo类中
Pojo类
@Data
@Component//将这个类加载到Ioc容器中
public class DateModule {
//读取的要素
@Value(value = "${spring.datasource.driver-class-name}")
private String provider;
@Value(value = "${spring.datasource.url}")
private String dbUrl;
@Value(value = "${spring.datasource.username}")
private String dbUser;
@Value(value = "${spring.datasource.password}")
private String dbPassword;
Test单元测试
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private DateModule dateModule;
@Test
public void JdbcTest() throws ClassNotFoundException, SQLException {
//通过list来读取
List<Ins_cost_JDBC> list = new ArrayList<>();
//1. 注册驱动,这个时候再读取就不是空了
Class.forName(dateModule.getProvider());
//2. 获取链接
Connection conn = DriverManager.getConnection(dateModule.getDbUrl(), dateModule.getDbUser(), dateModule.getDbPassword());
我做了下面的测试,当我在
@SpringTest类中的时候使用
@Autowired private Environment env;
System.out.println(env);
这个时候的env不是null,
但是我在pojo类中使用env,就一直为null
所以我使用@Value的方式来获取配置类中的值了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。