简体   繁体   中英

Spring + Hibernate: sessionFactory.getCurrentSession() results in NullPointerException

I'm using Spring 4.3.2.RELEASE with Hibernate 4.3.11.RELEASE. As soon as I autowire any Dao I'll get a NullPointerException. Inside the Dao Implementation I autowire the sessionFactory and then use sessionFactory.getCurrentSession(). The error message itself is a long nested exception which finally leads to this:

Exception during lifecycle processing
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'applicationStartup': Unsatisfied dependency expressed through field 'batchjobDao': Error creating bean with name 'batchjobDaoImpl': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in com.TayrosCapital.investsuiteNG.config.Config: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.NullPointerException; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.TayrosCapital.investsuiteNG.config.Config: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.NullPointerException; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'batchjobDaoImpl': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in com.TayrosCapital.investsuiteNG.config.Config: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.NullPointerException; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.TayrosCapital.investsuiteNG.config.Config: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.NullPointerException
    at com.sun.enterprise.web.WebApplication.start(WebApplication.java:168)
    at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
    at org.glassfish.internal.data.ModuleInfo.start(ModuleInfo.java:291)
    at org.glassfish.internal.data.ApplicationInfo.start(ApplicationInfo.java:352)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:500)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:219)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:491)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:539)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:535)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:534)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:565)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:557)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:556)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1464)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:109)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1846)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1722)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
    at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
    at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
    at java.lang.Thread.run(Thread.java:745)

My Config Class with the sessionFactory bean looks like this:

@Configuration
@ComponentScan("com.TayrosCapital.investsuiteNG")
@EnableWebMvc
@EnableTransactionManagement
@Import({SecurityConfig.class})
@PropertySource("classpath:properties/application.properties")
public class Config extends WebMvcConfigurerAdapter {

    @Autowired
    private Environment env;

    /**
     * Provides the functionality to support the @PropertySource annotation
     * 
     * @return 
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfig() {
        PropertySourcesPlaceholderConfigurer propertyConfig = new PropertySourcesPlaceholderConfigurer();
        return propertyConfig;
    }

    /**
     * Provides the dataSource for the database access
     * 
     * @return 
     */
    @Bean(name = "dataSource")
    public DriverManagerDataSource dataSource() {        
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(env.getProperty("database.driver"));
        driverManagerDataSource.setUrl(env.getProperty("database.url"));
        driverManagerDataSource.setUsername(env.getProperty("database.username"));
        driverManagerDataSource.setPassword(env.getProperty("database.password"));
        return driverManagerDataSource;
    }

    /**
     * viewResolver to set the prefix and suffix for the views
     * 
     * @return 
     */
    @Bean(name = "viewResolver")
    public InternalResourceViewResolver viewResolver() {        
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/xhtml/");
        viewResolver.setSuffix(".xhtml");
    return viewResolver;
    }

    /**
     * runs flyway to handle SQL migrations
     * 
     * @return 
     */
    @Bean(initMethod = "migrate")
    public Flyway flyway() {
        Flyway flyway = new Flyway();
        flyway.setLocations("classpath:sql");
        flyway.setDataSource(dataSource());
        return flyway;
    }

    /**
     * provides the entity manager
     * 
     * @return 
     */
    @Bean(name = "entityManagerFactory")
    @DependsOn("flyway")
    public EntityManagerFactory entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
        bean.setDataSource(dataSource());        
        return bean.getObject();
    }

    /**
     * Provides a session factory for hibernate db access
     * 
     * @param dataSource
     * @return 
     */
    @Autowired
    @Bean(name = "sessionFactory")
    @DependsOn("flyway")
    public SessionFactory sessionFactory(DataSource dataSource) {
        LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
        sessionBuilder.scanPackages("com.TayrosCapital.investsuiteNG");
        sessionBuilder.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        sessionBuilder.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));

       return sessionBuilder.buildSessionFactory();
    }

    /**
     * Hibernate transaction manager to be able to use @Transactional annotation
     * 
     * @param sessionFactory
     * @return 
     */
    @Autowired
    @Bean(name = "transactionManager")
    public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
        return transactionManager;                
    }

    /**
     * Defines where the resources for the website are located, e.g. css files
     * 
     * @param registry 
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/**");
    }    
}

[Edit]: Here one example where I use the sessionFactory

@Repository
public class BatchjobDaoImpl extends ExtendedGenericDaoImpl<Batchjob, Integer> implements BatchjobDao {

    @Autowired
    private SessionFactory sessionFactory;

    /**
     * Will return a batchjob with the given className
     * 
     * @param className
     * @return A Batchjob
     */
    @Override
    @Transactional
    public Batchjob getByClass(String className) {
        String hql = "FROM Batchjob WHERE clazz=:className";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        query.setParameter("className", className);
        query.setMaxResults(1);

        List<Batchjob> list = (List<Batchjob>)query.list();

        if(list != null && !list.isEmpty()) {
            return list.get(0);
        }

        return null;
    } 
}

[Edit] My application.properties:

# settings for the database
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/database?useUnicode=yes&characterEncoding=UTF-8
database.username=user
database.password=password

# settings for hibernate
hibernate.show_sql=false
hibernate.dialect=org.hibernate.dialect.MySQLDialect

Probably my guess here is @Autowired Environtment env; has not initialized before beans created. Better option would be to wire EnvirontmentAware interface

@Configuration
@ComponentScan("com.TayrosCapital.investsuiteNG")
@EnableWebMvc
@EnableTransactionManagement
@Import({SecurityConfig.class})
@PropertySource("classpath:properties/application.properties")
public class Config extends WebMvcConfigurerAdapter implements EnvironmentAware{

private Environment environment;

    @Override
    public void setEnvironment(final Environment environment) {
        this.environment = environment;
    }


///other beans instantiations

}

or you may try with

@Resource
public Environment env;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM