Spring @Autowired(required = true)为空 [英] Spring @Autowired(required = true) is null
问题描述
@Autowired
。在EAR模块中有WAR,JAR与 @Service
Spring和JAR与Spring配置文件。 code> 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 />
<! - 在EAR中的jar模块中的@Service类的包 - >
< context:component-scan base-package =com.ipdb.service/>
beanRefContext.xml:
< bean id =sharedContextclass =org.springframework.context.support.ClassPathXmlApplicationContext> <构造精氨酸>
< list>
< value> spring-ctx.xml< / value>
< / list>
< / constructor-arg> < /豆腐>
当我使用 @Autowired(required = null)
在Backing Bean中,值为 null
(没有任何异常)。我的JSF bean
@Component
@ManagedBean
@ViewScoped
public class PortfolioController {
@Autowired(required = true)
private PortfolioService portfolioService;
...
可以帮我吗>
PortfolioController
被认为是一个 JSF
上下文bean添加 @Component
到 @ManagedBean
是完全错误的,你不能标记与bean类似的类两个不同的上下文( JSF
和 Spring
)。
两个解决方案可使 PortfolioController
一个spring bean,从而删除 @ManagedBean
和 @ViewScoped
或注入 PortfolioController
通过 JSF
注释注释 @ManagedProperty
@ManagedProperty(#{portfolioService} )
private PortfolioService portfolioService;
I have a webmodule with JSF 2 end Spring 4.3. In a backing bean I use @Autowired
for DI of a service of a JAR. In EAR module there are WAR, JAR with @Service
Spring and JAR with Spring configuration file.
Below a web.xml
snippet:
<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>
When I Use @Autowired(required=null)
in a Backing Bean the value is null
(there is not any exception). My JSF bean
@Component
@ManagedBean
@ViewScoped
public class PortfolioController {
@Autowired(required = true)
private PortfolioService portfolioService;
...
Can you help me, please.
PortfolioController
is considered a JSF
context bean adding @Component
to @ManagedBean
is totally wrong you can't mark same class as bean in two different contexts (JSF
and Spring
).
Two solutions either make PortfolioController
a spring bean thus remove the @ManagedBean
and @ViewScoped
or inject PortfolioController
via JSF
injection annotation @ManagedProperty
@ManagedProperty("#{portfolioService}")
private PortfolioService portfolioService;
这篇关于Spring @Autowired(required = true)为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!