出现这个问题由以下几个原因(不完全是):
①没有配置事务,文档中明确说明,是为每个事务配置一个SessionFacotory,所以,如果我们不配置TransactionManager,则不会有事务,也就不会有SessionFacotory.
②没有配置AOP。通过官方文档中的例子,我们可以发现在执行Service中的方法时,会配置一个事务管理器。所以如果我们不配置AOP,则也不会配置事务管理器。
③调用Service时,不是正确的方式。正确的方式是:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Service service = applicationContext.getBean(Service.class);
service.callMethod();
如果我们直接通过Service service = new Service()这种方式,则不会读取applicationContext.xml配置文件,也就自然不会配置事务管理器等
最后需要注意的是,对那些我们要使用自动注入的类,一定要加上@Component,@Service,@Controller,@Respository等注解,并在配置文件中配置自动扫描<context:component:scan>来使这些类加入到spring管理容器中。对于需要自动注入的属性,要加上@Autowire