DataTable 之后的 Bean 注入
我有一个带有数据表的页面,该数据表从服务类读取数据。该服务类应该注入 contactDAO,但它不会立即注入。事实上,当页面第一次加载时,数据表是空的,因为 DAO 尚未注入。但是,如果我调用 addContact(),列表“联系人”会很好地更新,因为那时 contactDAO 已被注入。
如何确保在数据表需要使用服务类之前注入 contactDAO?我正在使用 Spring 3 和 JSF 2.0。
包含数据表的页面绑定到列表“contactServiceImpl.contacts
”:
<h:dataTable var="contact" value="#{contactServiceImpl.contacts}">
...
</h:dataTable>
我的 ContactServiceImpl 如下所示:
@Service
public class ContactServiceImpl implements ContactService {
private static List<Contact> contacts = new ArrayList<Contact>();
@Autowired
private static ContactDAO contactDAO;
private ContactServiceImpl() {
contacts = new ArrayList<Contact>();
//TODO: need to inject contactDAO at the same time as instantiation
contacts.clear();
try {
contacts.addAll( contactDAO.getContacts() );
} catch (Exception e) {
e.printStackTrace();
}
}
public void addContact(Contact contact) {
contacts.add(contact);
contactDAO.addContact(contact);
contacts.clear();
contacts.addAll( contactDAO.getContacts() );
}
@Autowired
public void setContactDAO(ContactDAO contactDAO) {
ContactServiceImpl.contactDAO = contactDAO;
System.out.println("DAO is injected");
}
}
和 applicationContext.xml
<bean id="contactServiceImpl" class="com.example.service.ContactServiceImpl"
scope="session">
<property name="contactDAO" ref="contactDAOImpl"/>
</bean>
I have a page with a datatable that reads data from a service class. That service class is suppose to be injected with a contactDAO but it does not get injected right away. In fact, when the page first loads up, the data table is empty because the DAO has not been injected yet. However, if I call addContact(), the list 'contacts' is updated fine because the contactDAO is injected by then.
How do I ensure the contactDAO is injected before the datatable needs to use the service class? I am using Spring 3 and JSF 2.0.
The page with the datatable is bind to the list 'contactServiceImpl.contacts
':
<h:dataTable var="contact" value="#{contactServiceImpl.contacts}">
...
</h:dataTable>
My ContactServiceImpl looks like this:
@Service
public class ContactServiceImpl implements ContactService {
private static List<Contact> contacts = new ArrayList<Contact>();
@Autowired
private static ContactDAO contactDAO;
private ContactServiceImpl() {
contacts = new ArrayList<Contact>();
//TODO: need to inject contactDAO at the same time as instantiation
contacts.clear();
try {
contacts.addAll( contactDAO.getContacts() );
} catch (Exception e) {
e.printStackTrace();
}
}
public void addContact(Contact contact) {
contacts.add(contact);
contactDAO.addContact(contact);
contacts.clear();
contacts.addAll( contactDAO.getContacts() );
}
@Autowired
public void setContactDAO(ContactDAO contactDAO) {
ContactServiceImpl.contactDAO = contactDAO;
System.out.println("DAO is injected");
}
}
And applicationContext.xml
<bean id="contactServiceImpl" class="com.example.service.ContactServiceImpl"
scope="session">
<property name="contactDAO" ref="contactDAOImpl"/>
</bean>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Spring总是在使用构造函数注入属性之前创建bean,因此您不能在注入属性之前使用它们。要解决这个问题,您必须重写逻辑或获取 contactDAO 作为构造函数参数(我知道非常丑陋的解决方案,但可以工作)
强烈建议搜索其他解决方案(例如,我在我的项目中使用 Wicket,还有另一个渲染概念表 - 使用 DataProviders 所以我没有这样的问题,也许你可以在你的 JSF 项目中使用这个概念)
Spring always create bean before inject propertes with constructor, so you can't use injected properties before they injected. To solve this problem you must rewrite logic or get contactDAO as constructor arg (I'm know VERY ugly solution, but work)
Higly recommend search other solution (For example, I'm use Wicket in my projects, there is another concept of rendering tables - using DataProviders so i'm don't have such problem, may be you can use this concept in your JSF project)