2017-03-12 108 views
0

我有一个简单的类定义了保存我的elasticsearch客户端(称为ElasticClientConfig.java)的配置属性。Autowired spring bean是nul

我有一个为我的开发,产品和测试环境定义的配置。每个配置文件都有一个返回ElasticClientConfig类型的bean的方法,并使用特定于该环境的参数构建一个MyConfig对象。下面是版本发展:

@Configuration 
@Profile("dev") 
public class DevConfig { 

    @Bean 
    public ElasticClientConfig getElasticClientConfig(){ 
      //build the ElasticSearchConfig and return it 
    } 

} 

设置我积极配置文件,如我的web.xml文件“开发”:

<context-param> 
     <param-name>spring.profiles.active</param-name> 
     <param-value>dev</param-value> 
    </context-param> 

我注入使用@Autowired注释的ElasticClientConfig对象,但它一片空白。关于我可以检查的任何想法?我的spring-servlet.xml文件非常简单:

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> 

    <context:component-scan base-package="com.elasticapp" /> 

    <mvc:annotation-driven /> 

    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix"> 
      <value>/WEB-INF/views/</value> 
     </property> 
     <property name="suffix"> 
      <value>.jsp</value> 
     </property> 
    </bean> 
    <bean class="com.elasticapp.client.ElasticClient"/> 
</beans> 

ElasticClient是我将ElasticClientConfig注入到的类。

回答

0

我忘记了依赖注入如何工作,意识到我试图访问构造函数中的自动装配属性。决定使用@PostConstruct,完美地工作!