[英]Autowired spring bean is nul
我定義了一個簡單的類,其中包含我的Elasticsearch客戶端的配置屬性(稱為ElasticClientConfig.java)。
我為開發,產品和測試環境定義了配置。 每個配置概要文件都有一個返回類型為ElasticClientConfig的bean的方法,並使用特定於環境的參數構建MyConfig對象。 這是開發版本:
@Configuration
@Profile("dev")
public class DevConfig {
@Bean
public ElasticClientConfig getElasticClientConfig(){
//build the ElasticSearchConfig and return it
}
}
我在web.xml文件中將活動配置文件設置為“ dev”:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
我使用@Autowired注解注入了一個ElasticClientConfig對象,但它為null。 關於我可以檢查的任何想法嗎? 我的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注入到的類。
我忘了依賴注入的工作原理,意識到我正在嘗試訪問構造函數中的自動裝配屬性。 決定使用@PostConstruct,效果很好!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.