gpt4 book ai didi

java - 带注解的 Spring 4 PropertyPlaceholder

转载 作者:行者123 更新时间:2023-11-29 03:22:37 25 4
gpt4 key购买 nike

好吧,我知道这个问题已经被问过好几次了,我已经尝试了他们建议答案中的所有解决方案,但还没有找到一个有效的解决方案,所以我希望这里的人能给我指明正确的方向。

我正在尝试将 Spring 4 与以 Java 为中心的配置一起使用,但在从属性文件加载属性时遇到问题。我的最新方法几乎是逐字遵循 Spring 的文档 here但即使这样也行不通。

这是我的 properties-config.xml 文件(位于我的类路径的/config 目录中):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:property-placeholder location="classpath:db.properties"/>

</beans>

这是我的 Web 应用程序初始化程序类(无论如何都是一个片段):

public class TestWebAppInitializer implements WebApplicationInitializer
{
@Override
public void onStartup(ServletContext container)
{
// Instantiate a new web application context
AnnotationConfigWebApplicationContext appContext =
new AnnotationConfigWebApplicationContext();

// Load the configurations
appContext.scan("com.acme.config");
appContext.refresh();

// Add the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);

// Add the various listeners
container.addListener(new ContextLoaderListener(appContext));
container.addListener(new RequestContextListener());
}
}

最后是一个使用属性文件的小示例配置类:

package com.acme.config;

@Configuration
@ImportResource("classpath:config/properties-config.xml")
public class HibernateConfiguration
{
@Value("${jdbc.url}")
private String jdbcUrl;

@Value("${jdbc.username}")
private String jdbcUsername;

@Value("${jdbc.password")
private String jdbcPassword;

@Bean(name = "dataSource")
public ComboPooledDataSource getDataSource() throws PropertyVetoException
{
// Define a variable to hold the result
ComboPooledDataSource ds = new ComboPooledDataSource();

System.out.println("URL: " + jdbcUrl);
System.out.println("Username: " + jdbcUsername);
System.out.println("Password: " + jdbcPassword);

// Set the properties for the data source
ds.setJdbcUrl(jdbcUrl);
ds.setUser(jdbcUsername);
ds.setPassword(jdbcPassword);

// Return the result
return ds;
}
}

最后但同样重要的是,属性文件:

jdbc.url=jdbc:hsqldb:hsql://localhost/test
jdbc.username=myusername
jdbc.password=mypassword

System.out.println 语句对每个阻止我的数据源设置的值都返回“null”。

有人可以告诉我我做错了什么吗?谢谢!

最佳答案

这里真正的问题是注释配置在任何导入的 XML 之前加载,你真的必须将属性占位符配置移动到 @Configuration:

@Configuration    
@PropertySource("classpath:db.properties")
public class HibernateConfiguration {

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}

请注意 propertySourcesPlaceholderConfigurer bean 定义。没有它,您只能使用 Autowiring 的 Environment 访问文件中的属性。

从这里开始,属性占位符配置也可用于导入的 xml。

有关详细信息,请参阅 PropertySource JavaDocs

关于java - 带注解的 Spring 4 PropertyPlaceholder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22745684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com