将会话工厂从一个服务传递到另一个服务 [英] Passing session factories from one service to another

查看:73
本文介绍了将会话工厂从一个服务传递到另一个服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的spring + hibernate应用程序中定义了两个会话工厂。我需要在运行时在这两者之间切换。所以我只是将这两个会话工厂注入到下面的一个服务中。

  @Service 
public class HibernateUtil {

@Autowired
SessionFactory sessionFactory;
@Autowired
@Qualifier(sessionFactorySports)
SessionFactory sessionFactorySports;

public static String sessionFac =sports;

public SessionFactory getFactory(){

if(sessionFac.equalsIgnoreCase(sports))
return sessionFactorySports;
else
return sessionFactory;


$ b $ / code>

然后我尝试访问这个会话

  @Service 
@EnableTransactionManagement
public class StoreAssociateService {

@Autowired
HibernateUtil hibernateUtil;
SessionFactory sessionFactory = hibernateUtil.getFactory();

}

但是我收到以下异常

 原因:org.springframework.beans.factory.BeanCreationException:无法自动装入字段:StoreAssociateService;嵌套异常是org.springframework.beans.factory.BeanCreationException:在文件[StoreAssociateService.class]中定义的名称为'storeAssociateService'的bean创建时出错:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[StoreAssociateService]:构造函数抛出异常;嵌套的异常是java.lang.NullPointerException 

所以这里有什么问题?这种方法是否可行?

编辑1:

trace。

  org.springframework.beans.factory.BeanCreationException:创建名为'loginController'的bean时出错:注入自动装配依赖失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装入字段:StoreAssociateService LoginController.storeAssociateService;嵌套异常是org.springframework.beans.factory.BeanCreationException:在文件[StoreAssociateService.class]中定义的名称为'storeAssociateService'的bean创建时出错:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[StoreAssociateService]:构造函数抛出异常;嵌套异常是org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:326)中的java.lang.NullPointerException 
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory中的
。在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)上
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory上
。 createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry .getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(Abstrac tBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons( DefaultListableBeanFactory.java:725)在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757

在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:在org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658 480)

在org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)$ b在org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)$ b。在org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext
(框架ervlet.java:543)在org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484
)在org.springframework.web.servlet.HttpServletBean.init
(HttpServletBean.java:在javax.servlet.GenericServlet.init(GenericServlet.java:160 136)

。在org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
。在有机.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
在org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
在org.apache.catalina .core.StandardContext.loadOnStartup(StandardContext.java:5176)
在org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
在org.apache.catalina.util.LifecycleBase 。开始(LifecycleBase.java:150)
在org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1559)
在org.apache.catalina.core.ContainerBase $ StartChild .CALL(ContainerBase.java:1 (FutureTask.java:138)
(java.util.concurrent.FutureTask.run)
在java.util.concurrent.ThreadPoolExecutor中的$ Worker.runTask(ThreadPoolExecutor.java:895)
在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:918)
在java.lang中.Thread.run(Thread.java:662)
原因:org.springframework.beans.factory.BeanCreationException:无法自动装入字段:StoreAssociateService LoginController.storeAssociateService;嵌套异常是org.springframework.beans.factory.BeanCreationException:在文件[StoreAssociateService.class]中定义的名称为'storeAssociateService'的bean创建时出错:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[StoreAssociateService]:构造函数抛出异常;嵌套的异常是在在org.springframework.beans.factory.annotation org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:542)
显示java.lang.NullPointerException
。 InjectionMetadata.inject(InjectionMetadata.java:87)
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:323)
... 30多个
所致:org.springframework.beans.factory.BeanCreationException:在文件[StoreAssociateService.class]中定义的名称为'storeAssociateService'的bean创建时出错:bean的实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化bean类[StoreAssociateService]:构造函数抛出异常;嵌套异常是java.lang.NullPointerException
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1095)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory。在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)上
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory中
。 createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry .getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFacto
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates( DefaultListableBeanFactory.java:1081)
在org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1006)
在org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency( DefaultListableBeanFactory.java:904)
在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
... 32多个
引起的:组织.springframework.beans.BeanInstantiationException:无法实例化bean类[com.sasken.soln.retail.msell.services.StoreAssociateService]:构造函数抛出异常;嵌套的异常是在在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
显示java.lang.NullPointerException
(SimpleInstantiationStrategy。 java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1088)
... 43 more
由java.lang.NullPointerException引发
。在com.sasken.soln.retail.msell.services.StoreAssociateService<初始化>(StoreAssociateService.java:29)
。在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
在java.lang.reflect.Constructor.newInstance(构造函数.java:513)org.spring
framework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 45 more
2015年4月21日3:03:11 org.apache.catalina.core.StandardContext loadOnStartup
严重:Servlet的/ MSellApp扔负荷()例外
显示java.lang.NullPointerException
。在com.sasken.soln.retail.msell.services.StoreAssociateService< INIT>(StoreAssociateService.java:29)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl。 Java的:在java.lang.reflect.Constructor.newInstance(Constructor.java:513 27)

在org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.be ans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1088)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040)
。在org.springframework。 beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)
处org.springframework org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
。 beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
at org.springframework .beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactor y.getBean(AbstractBeanFactory.java:193)
在org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1081)
at org.springframework.beans.factory.support。 DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1006)
处org.springframework.beans.factory.annotation org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
。 AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory。 annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:323)
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populate豆(AbstractAutowireCapableBeanFactory.java:1204)
处org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
。 createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry .getSingleton(DefaultSingletonBeanRegistry.java:229)
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
在org.springframework.beans.factory.support.AbstractBeanFactory .getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
at org.springframework.conte
在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
在org.springframework.web.servlet。
在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
在org.springframework.web.servlet。 FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
在org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
在org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext( FrameworkServlet.java:672)在org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543

在org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java: 484)
在org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)在javax.servlet.GenericServlet.init(GenericServlet.java:160

。在组织.apache.catalina。 core.StandardWrapper.initServlet(StandardWrapper.java:1280)
位于org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
位于org.apache.catalina.core.StandardWrapper。负载(StandardWrapper.java:1088)
。在org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
。在org.apache.catalina.core.StandardContext.startInternal(StandardContext。
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:
at org.apache.catalina.core.ContainerBase $ StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor $ Worker.runTask(ThreadPoolExecutor.java:895)
at java .util.concurrent.ThreadPoolExecutor $ Worker.run(苏氨酸eadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)


解决方案

好的。以下是我必须做的工作。它只是我们必须用来初始化sessionFactory的@PostConstruct方法因为注入的对象在构造器级别不可用。

  @Service 
@EnableTransactionManagement
公共类StoreAssociateService {

@Autowired
HibernateUtil hb;
SessionFactory sessionFactory;
@PostConstruct
public void init()
{
sessionFactory = hb.getFactory();
}

}


I have two session factories defined in my spring + hibernate application. I need to switch between these two at runtime. So I just injected those two session factories in one of the service like below.

@Service
public class HibernateUtil {

    @Autowired
    SessionFactory sessionFactory;
    @Autowired
    @Qualifier("sessionFactorySports")
    SessionFactory sessionFactorySports;

    public static String sessionFac="sports";

    public SessionFactory getFactory() {

        if(sessionFac.equalsIgnoreCase("sports"))
            return sessionFactorySports;
        else
            return sessionFactory;

    }
}

Then I tried to access this session factory like below in other service.

@Service
@EnableTransactionManagement
public class StoreAssociateService {

    @Autowired
    HibernateUtil hibernateUtil;
    SessionFactory sessionFactory = hibernateUtil.getFactory();

}

But I am getting the following exception

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: StoreAssociateService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'storeAssociateService' defined in file [StoreAssociateService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [StoreAssociateService]: Constructor threw exception; nested exception is java.lang.NullPointerException

So what's wrong here ? Will this method work ?

EDIT 1:

Here is the full stack trace.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: StoreAssociateService LoginController.storeAssociateService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'storeAssociateService' defined in file [StoreAssociateService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [StoreAssociateService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:326)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: StoreAssociateService LoginController.storeAssociateService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'storeAssociateService' defined in file [StoreAssociateService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [StoreAssociateService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:542)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:323)
    ... 30 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'storeAssociateService' defined in file [StoreAssociateService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [StoreAssociateService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1081)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1006)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    ... 32 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.sasken.soln.retail.msell.services.StoreAssociateService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1088)
    ... 43 more
Caused by: java.lang.NullPointerException
    at com.sasken.soln.retail.msell.services.StoreAssociateService.<init>(StoreAssociateService.java:29)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 45 more
Apr 21, 2015 3:03:11 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /MSellApp threw load() exception
java.lang.NullPointerException
    at com.sasken.soln.retail.msell.services.StoreAssociateService.<init>(StoreAssociateService.java:29)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1088)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1081)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1006)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:323)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:662)

解决方案

OK. Here's what I had to do to get it work. It just the @PostConstruct method which we have to use to initialize the sessionFactory Since injected objects are not available at contructor level.

@Service
@EnableTransactionManagement
public class StoreAssociateService {

    @Autowired
    HibernateUtil hb;
    SessionFactory sessionFactory ;
    @PostConstruct
    public void init()
    {
        sessionFactory = hb.getFactory();
    }

}

这篇关于将会话工厂从一个服务传递到另一个服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆