internalTransactionalEventListenerFactory无法使用jpa查找错误

时间:2017-08-27 15:18:54

标签: java spring hibernate jpa jdbc

我在我的项目中使用Spring 4.3和JPA。我使用java配置来配置Hibernate而不是XMl配置。但是当我正在编译时,会收到错误消息。请指导我解决这个问题。

以下是堆栈跟踪

java.lang.IllegalStateException: could not find the type for bean named org.springframework.transaction.config.internalTransactionalEventListenerFactory
    at org.jboss.resteasy.plugins.spring.SpringBeanProcessor.getBeanClass(SpringBeanProcessor.java:456)
    at org.jboss.resteasy.plugins.spring.SpringBeanProcessor.processBean(SpringBeanProcessor.java:280)
    at org.jboss.resteasy.plugins.spring.SpringBeanProcessor.postProcessBeanFactory(SpringBeanProcessor.java:258)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:131)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.jboss.resteasy.plugins.spring.SpringContextLoaderListener.contextInitialized(SpringContextLoaderListener.java:48)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1284)
    at org.apache.catalina.manager.HTMLManagerServlet.start(HTMLManagerServlet.java:666)
    at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:217)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.filters.CsrfPreventionFilter.doFilter(CsrfPreventionFilter.java:136)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:108)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:616)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1533)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1489)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

JPA配置类

`@Configuration
@EnableTransactionManagement
@PropertySource(value = { "classpath:application/core-application.properties" })
public class JPAConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(dataSource());
        factoryBean.setPackagesToScan(new String[] { "com.dak.school.model" });
        factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
        factoryBean.setJpaProperties(jpaProperties());
        return factoryBean;
    }

    /*
    * Provider specific adapter.
    */
    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        return hibernateJpaVendorAdapter;
    }

    /*
     * Here you can specify any provider specific properties.
     */
    private Properties jpaProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        // properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        return properties;
    }

    @Bean
    @Autowired
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(emf);
        return txManager;
    }
} `

应用程序属性文件

> jdbc.driverClassName = com.mysql.jdbc.Driver jdbc.url =
> jdbc:mysql://localhost:3306/school_suit jdbc.username = root
> jdbc.password = admin123fg hibernate.dialect =
> org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=create-drop
> hibernate.show_sql = true hibernate.format_sql = true

服务实施类

@Service
//@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
@Transactional
public class UserServiceImpl implements UserService{

    @Autowired(required = false)
    private  UserDao userDao;

    @Override
    public void saveUser(final User user) {

        userDao.create(user);

    }
}

Dao实施班

@Repository
public class UserDaoImpl extends DaoImpl<User,Long> implements UserDao {

} 

抽象Daoimpl类

public abstract class DaoImpl<T, PK extends Serializable> implements IDao<T, PK> {

  /* Persistence Entity class type. */
  protected Class<T> entityClass;

  /* Entity manager to perform DAO operations. */
  @PersistenceContext
  protected EntityManager entityManager;

  /**
   * Constructor
   */
  @SuppressWarnings("unchecked")
  public DaoImpl() {

    ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
    this.entityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0];
  }


  /*
   * (non-Javadoc)
   * 
   * @see com.fg.baking.core.dao.IDao#create(java.lang.Object)
   */
  public T create(T t) {

    this.entityManager.persist(t);
    return t;
  }


  /*
   * (non-Javadoc)
   * 
   * @see com.fg.baking.core.dao.IDao#read(java.io.Serializable)
   */
  public T read(PK id) {

    return this.entityManager.find(entityClass, id);
  }


  /*
   * (non-Javadoc)
   * 
   * @see com.fg.baking.core.dao.IDao#update(java.lang.Object)
   */
  public T update(T t) {

    return this.entityManager.merge(t);
  }


  /*
   * (non-Javadoc)
   * 
   * @see com.fg.baking.core.dao.IDao#delete(java.lang.Object)
   */
  public void delete(T t) {

    t = this.entityManager.merge(t);
    this.entityManager.remove(t);
  }


  /*
   * (non-Javadoc)
   * 
   * @see com.yummy.platform.spring.dao.BaseDao#findAll()
   */
  @SuppressWarnings("unchecked")
  public List<T> findAll() {

    Query query = entityManager.createQuery("FROM " + entityClass.getName() + " c");
    return (List<T>) query.getResultList();
  }


}

依赖关系详情

 <spring.version>4.3.0.RELEASE</spring.version>
        <spring.security.version>4.2.2.RELEASE</spring.security.version>
        <jstl.version>1.2</jstl.version>
        <taglib.standard.version>1.1.2</taglib.standard.version>
        <!--<servlet.api.version>3.0.1</servlet.api.version>-->
        <servlet.api.version>3.1.0</servlet.api.version>
        <servlet.api.jsp.version>2.3.1</servlet.api.jsp.version>
        <resteasy.version>3.0.11.Final</resteasy.version>

        <apache.commnos.beanutil.version>1.9.0</apache.commnos.beanutil.version>
        <apache.commnos.codec.version>1.9</apache.commnos.codec.version>
        <commons.collections.version>3.2.1</commons.collections.version>
        <commons-digester.version>3.2</commons-digester.version>
        <commons-logging.version>1.1.3</commons-logging.version>
        <commons-dbcp.version>2.1.1</commons-dbcp.version>
        <commons-lang.version>2.4</commons-lang.version>

        <mysql.connector.version>6.0.5</mysql.connector.version>
        <hibernate.core.version>5.2.9.Final</hibernate.core.version>
        <javax.validation.version>1.1.0.Final</javax.validation.version>
        <hibernate-validator.version>5.1.3.Final</hibernate-validator.version>

3 个答案:

答案 0 :(得分:0)

用这个替换你的@Bean public JpaTransactionManager transactionManager() { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setDataSource(dataSource()); txManager.setEntityManagerFactory(entityManagerFactory().getObject()); return txManager; } bean:

open System
open Akka.Actor
open Akka.Configuration
open Akka.Remote
open Akka.FSharp

let system = System.create "system" (Configuration.defaultConfig())

type GreeterMsg =
    | Hello of string
    | Goodbye of string

let greeter = spawn system "greeter" <| fun mailbox ->
    let rec loop() = actor {
        let! msg = mailbox.Receive()

        match msg with
        | Hello name -> printf "Hello, %s!\n" name
        | Goodbye name -> printf "Goodbye, %s!\n" name

        return! loop()
    }
    loop()

答案 1 :(得分:0)

这似乎与https://issues.jboss.org/browse/RESTEASY-1241类似。我通过将RESTEasy从3.0.10.Final升级到3.0.19.Final来解决我的问题。

答案 2 :(得分:0)

在将现有的Spring应用程序(具有XML配置)迁移到springboot时,我遇到了类似的错误:

APPLICATION FAILED TO START

Description:

The bean 'org.springframework.transaction.config.internalTransactionalEventListenerFactory' could not be registered. A bean with that name has already been defined and overriding is dis
abled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

我通过从旧xml中删除几个重复的“ <tx:”配置来解决此问题。 最初,我添加了属性spring.main.allow-bean-definition-overriding=true,该属性只会从控制台隐藏该错误。但这并不能解决潜在的问题。因此,删除重复的配置解决了我的问题。