【问题标题】:Spring + Hibernate: sessionFactory.getCurrentSession() results in NullPointerExceptionSpring + Hibernate: sessionFactory.getCurrentSession() 导致 NullPointerException
【发布时间】:2017-01-25 01:37:24
【问题描述】:

我正在使用 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 的配置类如下所示:

@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

【问题讨论】:

  • 你能在你使用 sessionFactory 的地方添加你的代码吗?
  • 您的 application.properties 到底在哪里?
  • @DeendayalGarg 请看我的编辑
  • @kuhajeyan 我的 application.properties 位于“src/main/resources/properties”
  • 只是看看,你能不能把你的属性文件移到src/main/resources 看看它是否报错?

标签: java hibernate spring-mvc


【解决方案1】:

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

@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

}

或者你可以试试

@Resource
public Environment env;

【讨论】:

  • 这很有趣,因为它修复了 NPE,但我得到一个异常说“没有找到 WebApplicationContext:没有注册 ContextLoaderListener 或 DispatcherServlet”。这很奇怪,因为它在 NPE 突然出现之前确实有效。
  • 对此的补充:如果我将注释从 Autowired 更改为 Resource,它将给我与以前相同的 NPE
猜你喜欢
  • 2015-03-05
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
相关资源
最近更新 更多