无法从spring父(共享)上下文自动装配bean

时间:2015-04-21 11:55:31

标签: java spring java-ee websphere

我有简单的.war,有两个弹簧上下文。 我的web.xml包含:

<context-param>
    <param-name>parentContextKey</param-name>
    <param-value>ejbContext</param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/applicationContext.xml
    </param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

beanRefContext.xml:

<bean id="ejbContext" class="org.springframework.context.support.ClassPathXmlApplicationContext">
    <property name="configLocation" value="WEB-INF/eeContext.xml"></property>
</bean>

eeContext.xml

    <bean name="parentSpringBean" class="ru.fns.bean.spring.ParentSpringBean">
        <property name="stringProperty" value="parentSpringBean.stringProperty value"></property>
    </bean>

如上所述,父上下文只有一个bean定义,根据文档,它意味着它必须可以在EJB和子弹簧上下文之间访问。实际上,ejb bean在注入parentSpringBean的情况下工作正常,但是将这个bean自动装入bean中,在子上下文中定义 - 它失败并显示消息:

  

000002b3 ContextLoader E org.springframework.web.context.ContextLoader initWebApplicationContext上下文初始化失败                                    org.springframework.beans.factory.BeanCreationException:创建名为'springBean'的bean时出错:自动连接依赖项的注入失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private ru.fns.bean.spring.ParentSpringBean ru.fns.bean.spring.SpringBean.parentSpringBean;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[ru.fns.bean.spring.ParentSpringBean]的匹配bean:期望至少有一个bean可以作为此依赖项的autowire候选者。

applicationContext.xml包含:

<context:annotation-config />
<context:component-scan base-package="ru.fns"/>

springBean代码:

@Component()
@Qualifier("springBean")
public class SpringBean {
private static transient final Logger log = LoggerFactory.getLogger(SpringBean.class);

@Value("${logs.level}")
private String stringProperty;

@EJB(mappedName = "EEBean")
private EEBean eeBean;


@Autowired
@Qualifier("parentSpringBean")
private ParentSpringBean parentSpringBean; // it fails

此外,将springBean注入parentSpringBean工作正常,但如何?!

parentSpringBean代码示例:

 // there's neither @Component nor @Service annotation
public class ParentSpringBean {
private static transient final Logger log = LoggerFactory.getLogger(ParentSpringBean.class);

private String stringProperty;

@EJB(mappedName = "EEBean")
private EEBean eeBean; // ok



@Autowired
private SpringBean springBean; // child context bean, works but why?

我已经在WebSphere上测试了几天,但仍然无法理解这种奇怪的行为。有人知道为什么会发生这种情况吗?先谢谢。

0 个答案:

没有答案