0

I got a Spring MVC application. It runs over the Tomcat 7 server.

I want to make a props.properties file, so my app could access properties during Beans initialization process.

So i did the following:

1.Create a context-parameter to my web.xml

<context-param>
        <param-name>mainProps</param-name>
        <param-value>${catalina.home}/conf/props.properties</param-value>
    </context-param>

2. I created a MainCobfig class

@Configuration
@PropertySource("classpath:/racoonsoft/wish/properties/props.properties")
@Import({WebConfig.class })

public class MainConfig {
@Autowired
Environment env;

@Value("${db.host}")
static String dbHost;
@Value("${db.name}")
static String dbName;
@Value("${db.login}")
static String dbLogin;
@Value("${db.password}")
static String dbPassword;

@Value("${ozon.login}")
static String ozonLogin;
@Value("${ozon.password}")
static String ozonPassword;
@Value("${ozon.apiurl}")
static String ozonApiUrl;

@Value("${payture.apihost}")
static String paytureApiHost;
@Value("${payture.merchant}")
static String paytureMerchant;

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

@Bean
public ScheduledAnnotationBeanPostProcessor scheduledAnnotationBeanPostProcessor() {
    return new ScheduledAnnotationBeanPostProcessor();
}

@Bean
public OzonProcessor apiProcessor() {
    return new OzonProcessor(ozonLogin, ozonPassword, ozonApiUrl);
}

@Bean
public PGSQLDataSource pgsqlDataSource() throws Exception{
    PGSQLDataSource result = new PGSQLDataSource(dbHost,dbName,5432,dbLogin,dbPassword,"org.postgresql.Driver","jdbc:postgresql:");
    result.loadSettings();
    if(FacebookController.dbProc==null)
    {
        FacebookController.dbProc = result;
    }
    //FacebookController.dbProc = result;
    return result;
}
@Bean
public PaytureProcessor paytureProcessor()
{
    PaytureProcessor proc = new PaytureProcessor(paytureApiHost,paytureMerchant);
    return proc;
}
}

3 - I created props.properties file and put it into /conf directory

When i start my application it didnt throw the exception (file not found) - so i beleave it sees the properties file. But during bean initialization my fields (dbHost,dbLogin etc.) are still null`s.

How can i put values from properties file to my fields?

Help me please.

4

2 回答 2

3

The annotated factory method of PropertySourcesPlaceholderConfigurer MUST be a static method:

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

Spring API Reference manual of @Bean remarks this.

A little bit more detailed explanation:

This is because PropertySourcesPlaceholderConfigurer is a BeanFactoryPostProcessor (BFPP). BFPP does a post-processing on the bean factory just before other (normal) beans are being instantiated and intialized. So, BFPP's are needed to be created in order to work, before the MainConfig bean is instantiated. Marking this factory method as a static method, we can invoke this method without instantiating MainConfig.

于 2013-05-15T13:10:41.223 回答
-2
# form user login properties
userName.required = User Name is required

在控制器方面,您仅声明userName.required. 这就是你声明它的方式。

于 2013-05-15T12:39:49.990 回答