jsf Spring@Autowired(required=true)为空
我有一个带有JSF2 end Spring 4.3的webmodule。在支持bean中,我使用@Autowired
作为JAR服务的DI。EAR模块中有WAR、带有@Service
Spring的JAR和带有Spring配置文件的JAR
下面是一个web.xml
片段:
<context-param>
<param-name>locatorFactorySelector</param-name>
<param-value>classpath:beanRefContext.xml</param-value>
</context-param>
<context-param>
<param-name>parentContextKey</param-name>
<param-value>sharedContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
applicationContext.xml
<context:annotation-config />
<context:spring-configured />
<!-- package of @Service class in jar module in EAR-- >
<context:component-scan base-package="com.ipdb.service" />
beanRefContext。xml:
<bean id="sharedContext" class="org.springframework.context.support.ClassPathXmlApplicationContext"> <constructor-arg> <list> <value>spring-ctx.xml</value> </list> </constructor-arg> </bean>
当我在支持Bean中使用@Autowired(required=null)
时,值是null
(没有任何异常)。我的JSFbean
@Component
@ManagedBean
@ViewScoped
public class PortfolioController {
@Autowired(required = true)
private PortfolioService portfolioService;
...
你能帮帮我吗
# 1 楼答案
PortfolioController
被认为是一个JSF
上下文bean,将@Component
添加到@ManagedBean
是完全错误的,您不能在两个不同的上下文(JSF
和Spring
)中将同一类标记为bean两种解决方案要么使
PortfolioController
成为spring bean,从而移除@ManagedBean
和@ViewScoped
,要么通过JSF
注入注释@ManagedProperty
注入PortfolioController