Spring + Hibernate:sessionFactory.getCurrentSession()导致NullPointerException

时间:2016-09-16 18:30:11

标签: java hibernate spring-mvc

我正在使用Spring 4.3.2.RELEASE和Hibernate 4.3.11.RELEASE。只要我自动装载任何Dao,我就会得到一个NullPointerException。在Dao实现中,我自动连接sessionFactory,然后使用sessionFactory.getCurrentSession()。错误消息本身是一个长嵌套异常,最终导致这种情况:

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)

使用sessionFactory bean的我的Config类如下所示:

@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/**");
    }    
}

[编辑]: 这里是我使用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;
    } 
}

[编辑]我的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

1 个答案:

答案 0 :(得分:0)

可能我的猜测是@Autowired Environtment env;在bean创建之前没有初始化。更好的选择是连接EnvirontmentAware接口

public class B
{
   public void DoStuff(ref A a) // <--- "a" is a new reference of type A
   {
        // Now this will affect the reference that was passed
        // when this method is being called!
        a = new A();
   }
}

B b = new B();
A a = new A();
b.DoStuff(ref a);

或者您可以尝试使用

@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

}