<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="personService" class="com.ecommerce.assignment.service.PersonService">
<property name="personDAO" ref="personDAO" />
</bean>
<bean id="personBean" class="com.ecommerce.assignment.bean.PersonBean" />
<bean id="personDAO" class="com.ecommerce.assignment.dao.PersonDAO" />
</beans>
这是人DAO的界面。
public interface IPersonDAO {
public void addPerson(Person instance);
}
此personDAO类实现IpersonDAO接口及其未实现的方法。
public class PersonDAO implements IPersonDAO {
@Override
public void addPerson(Person instance) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction().begin();
session.saveOrUpdate(instance);
session.getTransaction().commit();
session.close();
}
}
这是接口personService。 public interface IPersonService { public void addPerson(Person instance); }
public class PersonService implements IPersonService,Serializable {
private IPersonDAO personDAO;
public IPersonDAO getPersonDAO() {
return personDAO;
}
public void setPersonDAO(IPersonDAO personDAO) {
this.personDAO = personDAO;
}
@Override
public void addPerson(Person instance) {
getPersonDAO().addPerson(instance);
}
}
public class PersonBean implements Serializable {
private static final long serialVersionUID = 1L;
private String emailAddress;
private String password;
private String username;
@ManagedProperty(value = "#{personService}")
private IPersonService iPersonService;
public void addPerson(){
try{
Person person = new Person();
person.setUsername(getUsername());
person.setEmailAddress(getEmailAddress());
person.setPassword(getPassword());
iPersonService.addPerson(person);
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
答案 0 :(得分:0)
由于您的人员bean由Spring管理,您应该使用@Autowired
注释来进行依赖注入,或者通过XML注入,就像您对PersonService
所做的那样。您也可以将bean声明为@Component
和@Scope
。
您错误在于使用JSF的@ManagedProperty
注释在bean中注入服务,该注释仅在JSF上下文中工作,即使用JSF托管bean,使用@ManagedBean
注释,或通过XML声明。 / p>