0

We are creating a spring and hibernate application and using a legacy database. Our requirement is to get values from few database tables on server startup. We are planning to put these values in properties files.So that we don't need to fetch DB for these values again and again. We have used ApplicationListener to get hook on startup using following stackoverflow question:- Listener for server starup and all spring bean loaded completely

the code being used is as below

@Component
public class SpringContextListener implements ApplicationListener<ContextRefreshedEvent> {

  private List<Yosemitecompany> companyList = new ArrayList<Yosemitecompany>();


    private YosemitecompanyRI iYosemitecompanyBO;

    public SpringContextListener(){

    }

    public SpringContextListener(YosemitecompanyRI iYosemitecompanyBO) {
        this.iYosemitecompanyBO = iYosemitecompanyBO;
    }

 public void onApplicationEvent(final ContextRefreshedEvent event) {
  System.out.println("ApplicationListener Started"+iYosemitecompanyBO);
  if(companyList == null || (companyList != null && companyList.size() <= 0) && iYosemitecompanyBO != null)
  {
    companyList = iYosemitecompanyBO.getCompanyDetailsWithStatus();
      }
   }

 public List<Yosemitecompany> getCompanyList()
{
    return companyList;
}

  }

and this is the repository class

@Repository
@Transactional
public class YosemitecompanyRI implements IYosemitecompanyR{

static final Logger log = Logger.getLogger("YosemitecompanyDAOI");

@Autowired
private SessionFactory sessionFactory;

protected Session getSession() {
    log.info(sessionFactory);
    if (sessionFactory != null)
        return sessionFactory.getCurrentSession();
    else
        return null;
}

@Override

public List<Yosemitecompany> getCompanyDetailsWithStatus()
{
    List<Yosemitecompany> results = new ArrayList<Yosemitecompany>();
    log.info("reached "+getSession());
    if(getSession() != null)
    {
        log.info("executing query");
        Criteria cr = getSession().createCriteria(Yosemitecompany.class);
        cr.add(Restrictions.eq("cmpstatus",new BigDecimal(1)));
        results = (List<Yosemitecompany>)cr.list();
    }
    return results;
  }
 }

Now on server startup..i get sessionFactory always as null..so my code for getting the list never gets executed.

i am new to spring and Hibernate.If this approach is fine then please help me to know what i am doing wrong.if there is a better approach to achieve please suggest that too.

Thanks in advance.

Community
  • 1
  • 1
Mahi
  • 33
  • 1
  • 6
  • It cannot be null unless you either aren't using annotations for injection or you are creating a new instance of the bean yourself. Spring will fail to startup if it cannot autowire things. – M. Deinum Nov 19 '15 at 12:21
  • i am using annotation @Autowired with sessionFactory.and there is no other code for instantiating the YosemiteCompany bean. – Mahi Nov 19 '15 at 12:49
  • The fact that you have an annotation doesn't mean the context is going to use it. You need to tell the context you want to use it. Also your listener isn't using annotations so you are doing something manually with that. So post your configuration. – M. Deinum Nov 19 '15 at 12:51
  • may be this approach can help you http://www.ekiras.com/2015/10/spring-boot-how-to-create-bootstrap-class.html – Ekansh Rastogi Nov 19 '15 at 13:22
  • i used following http://stackoverflow.com/questions/16223952/configure-a-spring-bean-callback-on-server-startup approach and now i am getting the values from database on startup – Mahi Nov 20 '15 at 04:55

0 Answers0