我试图将bean配置中的硬编码数据库设置替换为db.properties文件,如下所示:
@Configuration
public class BeansConfig {
private @Value("#{database['db.url']}") String dbURL;
public ComboPooledDataSource dataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("something");
dataSource.setJdbcUrl(dbURL);
dataSource.setUser("something")
dataSource.setPassword("something")
return dataSource;
}
}
但它没有'工作。并且它突出显示,我可以按住Ctrl键并单击它,它会将我转到.properties文件。 但是,当我开始使用应用时,它只是不起作用。它试图进入数据库,但它并没有。当我在" url"上更改 dbURL 时本身它运作顺利。
尝试了许多导游,但没有帮助。
修改
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:*/appSecurity.xml
classpath:*/appContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:*/appContext.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
答案 0 :(得分:0)
如果有,
您的属性文件中的 db.url = jdbc:mysql://localhost:3306/livedump
(必须为application.properties
或使用@PropertySource("classpath:name_of_file.properties")
注释该类。)
你可以做到,
@Value("${db.url}")
String dbURL;