PropertyPlaceholderConfigurer 使用@PropertySource 错误访问 属性 文件
PropertyPlaceholderConfigurer accessing property file using @PropertySource error
我正在尝试通过 @PropertySource 注释将数据源变量注入我的 PropertyPlaceholderConfigurer,但出现错误 "Could not resolve placeholder 'jdbc.url' in string value "${jdbc.url}"
我的配置class
@Configuration
@ComponentScan(basePackages="com.spring.contacts")
@EnableWebMvc
@PropertySource(value = "file:${catalina.base}/spring.properties")
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
public static PropertyPlaceholderConfigurer properties(){
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
return ppc;
}
@Value( "${jdbc.url}" )
private String jdbcUrl;
@Value( "${jdbc.driverClassName}" )
private String driverClassName;
@Value( "${jdbc.username}" )
private String username;
@Value( "${jdbc.password}" )
private String password;
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/JDBC/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean(name="datasource")
public DataSource getDataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(driverClassName);
ds.setUrl(jdbcUrl);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
spring.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
我从 eclipse 开始 tomcat
-Dcatalina.base="C:\apache-tomcat-8.0.21"
追踪
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.spring.contacts.config.MvcConfiguration.jdbcUrl; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbc.url' in string value "${jdbc.url}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 31 more
我用
PropertySourcesPlaceholderConfigurer
而不是 PropertyPlaceholderConfigurer
我正在尝试通过 @PropertySource 注释将数据源变量注入我的 PropertyPlaceholderConfigurer,但出现错误 "Could not resolve placeholder 'jdbc.url' in string value "${jdbc.url}"
我的配置class
@Configuration
@ComponentScan(basePackages="com.spring.contacts")
@EnableWebMvc
@PropertySource(value = "file:${catalina.base}/spring.properties")
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
public static PropertyPlaceholderConfigurer properties(){
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
return ppc;
}
@Value( "${jdbc.url}" )
private String jdbcUrl;
@Value( "${jdbc.driverClassName}" )
private String driverClassName;
@Value( "${jdbc.username}" )
private String username;
@Value( "${jdbc.password}" )
private String password;
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/JDBC/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
@Bean(name="datasource")
public DataSource getDataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(driverClassName);
ds.setUrl(jdbcUrl);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
spring.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
我从 eclipse 开始 tomcat -Dcatalina.base="C:\apache-tomcat-8.0.21"
追踪
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.spring.contacts.config.MvcConfiguration.jdbcUrl; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbc.url' in string value "${jdbc.url}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 31 more
我用
PropertySourcesPlaceholderConfigurer
而不是 PropertyPlaceholderConfigurer