Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3877128
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:27:43+00:00 2026-05-19T22:27:43+00:00

I have a page with a datatable that reads data from a service class.

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T22:27:43+00:00Added an answer on May 19, 2026 at 10:27 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web app, that consumes a web service. The main page runs
Let's say I have a class that has a MyCustomDatabaseAccess as a data member.
I have a page that lists all from my Post table, it looks like
Here's the problem: 1.) We have page here... www.blah.com/mypage.html 2.) That page requests a
I have a page that is generated which inserts an HTML comment near the
I have a page with a Google Maps mashup that has pushpins that are
I have a page that uses $(id).show(highlight, {}, 2000); to highlight an element when
I have a page which does quite a bit of work and I don't
I have a page that has a few textboxes and a dropdownlist. When a
In my JSF page I have a combobox that updates a table when an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.