org.springframework.beans.factory.BeanCreationException:创建名称为' sessionFactory'的bean时出错在"包名称中定义"

时间:2017-11-12 21:28:50

标签: java spring hibernate maven servlets

我是spring和hibernate以及stackoverflow的新手 使用spring hibernate maven开发的电子商务项目 在创建两个文件之后为前端创建两个文件,为后端创建第二个文件我将后端的依赖项添加到前端pom.ml文件中

我正在使用java来创建sessionfactory,datasource和事务管理器 这是代码,我不明白我在做什么错误

:- write('loading disease database'), nl.
:- [facts].
:- write('disease database loaded'), nl, nl.

getsymptoms:-
    write('>   Enter a diseae name followed by a period.'), nl,
    write('For Example: Measles'), nl,
    write('Disease Name?:'),
    read(Input), nl,
    symptom(Input,Output),
    write(Output).

控制器

package com.ecom.Config;


import java.util.Properties;

import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource; 
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import 
org.springframework.transaction.annotation.EnableTransactionManagement;

import com.ecom.Model.CustomerRegistration;



 @Configuration
 @ComponentScan("com.ecom")
 @EnableTransactionManagement
 public class AnnotationConfigApplicationContext {

@Bean(name = "dataSource")
public DataSource getH2DataSource() {

    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setUrl("jdbc:h2:tcp://localhost/~/Ecommerce");

    dataSource.setDriverClassName("org.h2.Driver");

    dataSource.setUsername("sa");

    dataSource.setPassword("");

    return dataSource;
}


private Properties getHibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    properties.put("hibernate.show_sql", "true");
    properties.put("hibernate.hbm2ddl.auto", "create");
    return properties;
}

@Autowired
@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) {

    LocalSessionFactoryBuilder sessionBuilder = new 
LocalSessionFactoryBuilder(dataSource);
    sessionBuilder.addProperties(getHibernateProperties());
    sessionBuilder.addAnnotatedClass(CustomerRegistration.class);


    return sessionBuilder.buildSessionFactory();
}

@Autowired
@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(SessionFactory 
sessionFactory) {
    HibernateTransactionManager transactionManager = new 
 HibernateTransactionManager(sessionFactory);

    return transactionManager;
}



}

DAO

 package com.ecom.Controller;


 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;

 import org.springframework.web.bind.annotation.ModelAttribute;

 import org.springframework.web.bind.annotation.RequestMapping;

 import com.ecom.DAO.CustomerRegDAO;
 import com.ecom.Model.CustomerRegistration;






@Controller
public class CustomerRegController
{
 @Autowired     
 private CustomerRegDAO  customerRegDAO ;   

 @RequestMapping("/registrationForm")
 public String Registration(Model theModel)
 {
     CustomerRegistration theCustomerRegistration = new 
CustomerRegistration();

     theModel.addAttribute("customer", theCustomerRegistration);

     return "registration-form";
 }

 @RequestMapping("/saveCustomer")
 public String saveCustomer(@ModelAttribute("customer")  
CustomerRegistration theCustomerRegistration)
 {

     customerRegDAO.saveCustomer(theCustomerRegistration);

     return "index";
 }  


 }

错误:

      package com.ecom.DAOImplementation;

     import javax.transaction.Transactional;

     import org.hibernate.Session;
     import org.hibernate.SessionFactory;
     import org.springframework.stereotype.Repository;

     import com.ecom.DAO.CustomerRegDAO;
    import com.ecom.Model.CustomerRegistration;

  @Repository("CustomerRegDAO")
  @Transactional
  public class CustomerRegDAOImpl implements CustomerRegDAO {

    private SessionFactory sessionFactory;

  @Transactional
  public void saveCustomer(CustomerRegistration theCustomerRegistration) {

    Session currentsession = sessionFactory.getCurrentSession();

    currentsession.saveOrUpdate(theCustomerRegistration);

   } 

  }

1 个答案:

答案 0 :(得分:0)

The exception is indicating that the class JavaReflectionManager is not found. This class is present in hibernate-commons-annotations jar file. Can you add this in your pom.xml and try running it again?

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-commons-annotations</artifactId>
    <version>4.0.2.Final</version>
</dependency>