Java 无法读取Spring配置类中的属性文件值
我正在尝试编写一个简单的spring程序,它有一个名为PersistenceConfig的类,并用@Configuration注释Java 无法读取Spring配置类中的属性文件值,java,spring,configuration,Java,Spring,Configuration,我正在尝试编写一个简单的spring程序,它有一个名为PersistenceConfig的类,并用@Configuration注释 @Configuration @PropertySource("classpath:application.properties") public class PersistanceConfig { @Value("${dbPassword}") private String dbPassword;
@Configuration
@PropertySource("classpath:application.properties")
public class PersistanceConfig {
@Value("${dbPassword}")
private String dbPassword;
// Set of Beans and Code
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSource.setUrl("jdbc:sqlserver://localhost;databaseName=GovernmentPayment;integratedSecurity=false;");
dataSource.setUsername("sa");
dataSource.setPassword(dbPassword);
return dataSource;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
当我运行我的程序时,dbPassword的值总是空的,但是如果我试图在我的一个控制器中读取相同的值,那么它读取值时不会出现任何问题。
我尝试过自动连接环境变量,并使用它来代替@Value,但它也不起作用。(Spring没有向环境变量注入值)
我用的是Spring4
基本上,我们希望将数据库用户名和密码外部化到一个单独的属性文件中。我认为给定的代码没有任何问题。我为您的类编写了一个简单的单元测试来证明它是有效的
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=PersistanceConfig.class)
public class PersistanceConfigTest {
@Autowired
private DriverManagerDataSource dataSource;
private final String password = "mydbPassword";
@Test
public void testDriverManagerDataSourcePassword() {
System.out.println("dataSource Password :: " + dataSource.getPassword());
assertNotNull(dataSource);
assertTrue(password.equals(dataSource.getPassword()));
}
}
假设您在src/main/resources中有application.properties,并且该文件中显示了
dbPassword=mydbPassword
。应归功于Chad Darby
这是Spring版本的一个问题
如果您使用的是Spring 4.2及更低版本,则需要在中添加标有(**)的代码。
您在控制台中是否遇到任何错误?发布stacktraceOnly错误,我无法打开到数据库的连接,因为我的用户名和密码为空。com.microsoft.sqlserver.jdbc.SQLServerException:用户“”登录失败。客户端连接ID:8cf68ac0-9c5c-4fa8-94df-EE64E5CB8319有任何警告吗?发布完整的堆栈跟踪它有助于识别根本原因。当我在任何地方使用它时,值注释都可以很好地工作,除了使用配置注释的类。是否可以读取配置类中的属性文件?我是Spring新手。value应该在任何地方工作,包括配置类。您能在项目中创建上述测试并运行发生的事情吗?
package com.luv2code.springdemo;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
@Configuration
// @ComponentScan("com.luv2code.springdemo")
@PropertySource("classpath:sport.properties")
public class SportConfig {
// add support to resolve ${...} properties
**@Bean
public static PropertySourcesPlaceholderConfigurer
propertySourcesPlaceHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}**
// define bean for our sad fortune service
@Bean
public FortuneService sadFortuneService() {
return new SadFortuneService();
}
// define bean for our swim coach AND inject dependency
@Bean
public Coach swimCoach() {
SwimCoach mySwimCoach = new SwimCoach(sadFortuneService());
return mySwimCoach;
}
}
````
In Spring 4.3 and higher, they removed this requirement. As a result, you don't need this code.