2016-09-16 164 views
0

我使用Spring 4.3.2.RELEASE和Hibernate 4.3.11.RELEASE。只要我自動裝載任何道,我會得到一個NullPointerException。在Dao實現中,我自動裝入sessionFactory,然後使用sessionFactory.getCurrentSession()。錯誤消息本身是一個長期的嵌套異常最終導致這樣的:Spring + Hibernate:sessionFactory.getCurrentSession()導致NullPointerException

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的豆看起來是這樣的:

@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 
+0

嘗試,你可以添加你的代碼,你正在使用SessionFactory的? –

+0

究竟是你的application.properties? – kuhajeyan

+0

@DeendayalGarg請參閱我的編輯 – marcel2001

回答

0

可能我的猜測是@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; 
+0

這很有趣,因爲它修復了NPE,但是我得到一個異常說「沒有找到WebApplicationContext:沒有註冊ContextLoaderListener或DispatcherServlet」。這很奇怪,因爲在NPE突然出現之前它確實有效。 – marcel2001

+0

此外還有一個補充:如果我將註釋從Autowired更改爲Resource,它會給我與以前相同的NPE – marcel2001

相關問題