DataTable 之后的 Bean 注入

发布于 2024-10-16 02:46:50 字数 1652 浏览 2 评论 0原文

我有一个带有数据表的页面,该数据表从服务类读取数据。该服务类应该注入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不交电费瞎发啥光 2024-10-23 02:46:50

Spring总是在使用构造函数注入属性之前创建bean,因此您不能在注入属性之前使用它们。要解决这个问题,您必须重写逻辑或获取 contactDAO 作为构造函数参数(我知道非常丑陋的解决方案,但可以工作)

    private ContactServiceImpl(ContactDAO costrContactDAO) {
    contacts = new ArrayList<Contact>();

    //TODO: need to inject contactDAO at the same time as instantiation
    contacts.clear();
    try {
        contacts.addAll( costrContactDAO.getContacts() );
    } catch (Exception e) {
        e.printStackTrace();
    }
  }


  <bean id="contactServiceImpl" class="com.example.service.ContactServiceImpl"
    scope="session">
    <constructor-arg ref="contactDAOImpl"/>
    <property name="contactDAO" ref="contactDAOImpl"/>
  </bean>

强烈建议搜索其他解决方案(例如,我在我的项目中使用 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)

    private ContactServiceImpl(ContactDAO costrContactDAO) {
    contacts = new ArrayList<Contact>();

    //TODO: need to inject contactDAO at the same time as instantiation
    contacts.clear();
    try {
        contacts.addAll( costrContactDAO.getContacts() );
    } catch (Exception e) {
        e.printStackTrace();
    }
  }


  <bean id="contactServiceImpl" class="com.example.service.ContactServiceImpl"
    scope="session">
    <constructor-arg ref="contactDAOImpl"/>
    <property name="contactDAO" ref="contactDAOImpl"/>
  </bean>

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文