0

我已经从 XML 配置转移到基于代码的配置。这是我的JpaContext.java课:

@Configuration
@ComponentScan(basePackages = { "com.foo.bar.repository", "com.foo.bar.domain" })
@EnableJpaRepositories(basePackages={"com.foo.bar.repository"})
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ, order = 0)  // is like <tx:annotation-driven/>
@PropertySource( {"classpath:persistence.properties" } )

public class MyJpaContext {
    private static final Logger logger = LoggerFactory.getLogger(MyJpaContext.class);

    @Autowired
    Environment env;

    private @Value("${persistence.db.driverClass}") String driverClass;
    private @Value("${persistence.db.url}") String url;
    private @Value("${persistence.db.username}") String userName;
    private @Value("${persistence.db.password}") String passWord;

    @Bean
    public DataSource dataSource() {
        System.out.println(">>>>> Creating DataSource...");
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        logger.info(String.format("Using JDBC driver: %1$s", driverClass));
        logger.info(String.format("Using DB URL: %1$s", url));
        logger.info(String.format("Logging into DB with username: <%1$s> and password <%2$s>", userName, passWord));
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(env.getProperty("persistence.db.url"));
        dataSource.setUsername(userName);
        dataSource.setPassword(passWord);
        System.out.println("<<<<< DataSource successfully created.");
        return dataSource;
    }

    @Bean
    public HibernateJpaVendorAdapter jpaVendorAdapter() {
        System.out.println(">>>>> Creating HibernateJpaVendorAdapter...");
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setShowSql          (env.getProperty("persistence.hibernate.showSql", Boolean.class     ));
        adapter.setGenerateDdl      (env.getProperty("persistence.hibernate.generateDdl", Boolean.class ));
        adapter.setDatabasePlatform (env.getProperty("persistence.hibernate.databasePlatform"           ));
        System.out.println("<<<<< HibernateJpaVendorAdapter successfully created.");
        return adapter;
    }

    @Bean
    //  @DependsOn({"dataSource"})
    public EntityManagerFactory entityManagerFactory() {
        System.out.println("Creating EntityManagerFactory...");
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setPersistenceXmlLocation("classpath*:META-INF/persistence.xml");
        em.setDataSource(dataSource());
        em.setPackagesToScan("com.foo.bar.repository"); //this implicitly generates an appropriate persistence.xml for us at runtime . 
        em.setJpaVendorAdapter(jpaVendorAdapter());
        em.setJpaProperties(additionalProperties());
        em.setPersistenceUnitName("My_PU");
        em.setPersistenceProviderClass(org.hibernate.ejb.HibernatePersistence.class);
        em.afterPropertiesSet();
        System.out.println("<<<<< EntityManagerFactory successfully created.");
        return em.getObject();
    }

    Properties additionalProperties() {
        return new Properties() {
            {  // Hibernate Specific:
                setProperty("hibernate.hbm2ddl.auto", "update");
                setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
            }
        };
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        System.out.println("Creating JpaTransactionManager...");
        return new JpaTransactionManager(entityManagerFactory());
    }

//  @Bean
//  public PlatformTransactionManager txManager() {
//      System.out.println(env.getProperty("moka.session.timeout"));
//      return new DataSourceTransactionManager(dataSource());
//  }

    @Bean
        public PersistenceExceptionTranslator hibernateExceptionTranslator() {
        System.out.println(">>>>> Creating HibernateExceptionTranslator...");
        return new HibernateExceptionTranslator();
    }
}

调用em.afterPropertiesSet();时我遇到了

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: 
Factory method [public javax.persistence.EntityManagerFactory
com.foo.bar.config.MyJpaContext.entityManagerFactory()] threw exception; 
nested exception is 
java.lang.TypeNotPresentException: 
Type com.foo.bar.domain.AbcTicket not present

当我开始 TOMCAT 时......虽然这个 AbcTicket 类是明确存在的。

Spring 能够初始化我的 Web 应用程序,我错过了什么?

在从基于 XML 的配置转移到基于代码的配置(一个一个元素)之前,我能够启动我的 Web 应用程序。

4

1 回答 1

0

这似乎是一个重构问题。我删除了所有与 AbcClass 的事件和关系并再次重新创建它们……现在它运行良好。

于 2013-10-25T20:23:24.160 回答