Spring@Autowired(required=true)为空
我有一个带有JSF2 EndSpring4.3的webmodule。在支持bean中,我使用Spring@Autowired(required=true)为空,spring,jsf,dependency-injection,Spring,Jsf,Dependency Injection,我有一个带有JSF2 EndSpring4.3的webmodule。在支持bean中,我使用@Autowired对JAR服务进行DI。EAR模块中有WAR、带有@ServiceSpring的JAR和带有Spring配置文件的JAR 下面是web.xml代码段: <context-param> <param-name>locatorFactorySelector</param-name> <param-value&g
@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>
beanRefContext.xml:
您能帮助我吗。如果您的jar依赖项中有
applicationContext.xml
,则需要在类路径后添加星号:
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
contextConfigLocation
classpath*:applicationContext.xml
使用星号spring搜索文件
applicationContext.xml
在类路径中的任何位置,不仅是当前项目。PortfolioController
被认为是一个JSF
上下文bean将@Component
添加到@ManagedBean
是完全错误的,您不能在两个不同的类中将同一类标记为bean上下文(JSF
和Spring
)
两种解决方案要么使PortfolioController
成为Springbean,从而删除@ManagedBean
和@ViewScoped
,要么通过JSF
注入注释@ManagedProperty
注入
@ManagedProperty("#{portfolioService}")
private PortfolioService portfolioService;
它不是SpringBean,Spring没有管理或自动连接它。因为您的问题最初被标记为[SpringMVC],这里有一些值得思考的东西:好的,我已经用@ManagedBean
删除了@Autowired
。现在检测到实现服务,但我有以下错误:无法转换com.ipdb.service.portfolio.impl。PortfolioServiceImpl@5276229c类型为com.ipdb.service.portfolio.impl.PortfolioServiceImpl到接口com.ipdb.service.PortfolioService的类
PortfolioServiceImpl
实现PortfolioService
。你能帮我吗?我解决了,这是pom中的一个依赖性问题;)
@Component
@ManagedBean
@ViewScoped
public class PortfolioController {
@Autowired(required = true)
private PortfolioService portfolioService;
...
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
@ManagedProperty("#{portfolioService}")
private PortfolioService portfolioService;