我正在尝试使用@ManagedProperty
注释在JSF ManagedBean中使用spring Container中创建的bean。但是在使用该bean时我得到空指针。一旦启动我的服务器,我就可以看到我的bean被创建了这里
Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@9d532ae: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,userBean,userService];
HomepageBean.java
package come.test.backingbean
@ManagedBean
@sessionScoped
public Class HomepageBean{
@ManagedProperty(value="#{userBean}")
private UserBean userBean;// getters and setters
public String doLogin() {
String url = "login.xhtml";
LoginBean manager = new LoginBean(); // This bean has a condition which check for Username and password entered by user.
if (manager.auth(username, password)) {
isLoggedIn = true;
url = "homepage";
String username=sample;
userBean.getUserInfo(username);
} else {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(username, new FacesMessage(
"Invalid Username and or Password"));
}
return url;
}
UserBean.java
package com.test.mypackage
@Component
Public Class UserBean{
@Autowired
private UserService userServie // getters and setters.
public void getUserInfo(String userId){
userService.findByUserId(userId)
}
}
}
UserService.java
package com.test.service;
public interface UserService {
public void save(User User);
public void update(User user);
public void delete(User user);
public User findByUserId(String userId);
}
我可以看到我的服务器启动时我尝试使用的bean是预先实例化的。我在applicationContext.xml
中将web.xml
定义为Context-param
。我正在定义Spring.xml
中的所有bean,就像这样
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.test" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>
位于我的类路径中,并将其作为资源导入applicationContext.xml
。
的applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="classpath:database/DataSource.xml" />
<import resource="classpath:database/Hibernate.xml" />
<import resource="classpath:config/Spring.xml" />
</beans>
my faces-confi.xml
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
<resource-bundle>
<base-name>com.test.boundles.messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
我的方法有任何问题。
答案 0 :(得分:1)
应该更好地处理多项事情。
1)您只需要<context:component-scan base-package="com.test" />
,删除annotation-config和AutowiredAnnotationBeanPostProcessor。
参见原因:Difference between <context:annotation-config> vs <context:component-scan> 和Documentation
2)你的UserBean没有范围,如果你没有提到任何范围,默认范围将是Singleton,在这种情况下我认为不合适。
3)您正在尝试使用接口而不是实现此接口的可实例化类。
4)然后,您应该将实现类标记为@Service以进行自动装配。
5)我希望你有吸气剂和制定者,而不仅仅是那些评论。
有关示例,请参阅此link
另见: