2017-10-08 147 views
0

我使用弹簧数据JPA,并且已经在Java类而不是XML中定义了数据源,实体管理器和hibernate配置。这里是我的课程:在弹簧数据中加载持久性配置文件jpa

import javax.persistence.EntityManagerFactory; 
import javax.sql.DataSource; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.jdbc.datasource.DriverManagerDataSource; 
import org.springframework.orm.jpa.JpaTransactionManager; 
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; 
import org.springframework.transaction.PlatformTransactionManager; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 

@Configuration 
@EnableJpaRepositories("com.test.user.repository") 
@EnableTransactionManagement 
public class ApplicationConfig { 

    @Bean 
    public DataSource dataSource() { 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName("oracle.jdbc.OracleDriver"); 
     dataSource.setUrl("xxx"); 
     dataSource.setUsername("xxx"); 
     dataSource.setPassword("xxx"); 
     return dataSource; 
    } 


    @Bean 
    public EntityManagerFactory entityManagerFactory() { 
     HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
     vendorAdapter.setGenerateDdl(true); 
     LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); 
     factory.setJpaVendorAdapter(vendorAdapter); 
     factory.setPackagesToScan("com.test.user.repository.domain"); 
     factory.setDataSource(dataSource()); 
     factory.afterPropertiesSet(); 
     return factory.getObject(); 
    } 

    @Bean 
    public PlatformTransactionManager transactionManager() { 
     JpaTransactionManager txManager = new JpaTransactionManager(); 
     txManager.setEntityManagerFactory(entityManagerFactory()); 
     return txManager; 
    } 
} 

我的问题是,我必须为这个配置类创建一个bean定义吗?它在应用程序启动时如何加载?我们不使用Spring引导,没有主要方法等。

回答

0

您可以在web.xml或WebApplicationInitializer中配置它,以便您可以使用不同类型的配置文件设置根上下文。 见下面的例子

public class WebAppInitializer implements WebApplicationInitializer { 
    private static final Logger LOG = LoggerFactory.getLogger(WebAppInitializer.class); 

    @Override 
    public void onStartup(ServletContext container) throws ServletException { 
     AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 
     rootContext.register(AppConfig.class,PersistenceConfig.class,SecurityConfig.class); 
//  rootContext.register(AppConfig.class); 

     container.addListener(new ContextLoaderListener(rootContext)); 

     AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext(); 
     dispatcherServlet.register(MvcConfig.class); 

     ... 
} 

或者,你可以注册只是你的AppConfig类,并将其设置为扫描,其中PersistenceConfig位于封装(com.example.config)在它会加载配置Bean与@Configuration注释。

@Configuration 
@ComponentScan(basePackages={"com.example.config","com.example.validator","com.example.service", "com.example.security","com.example.aspect"}) 
@PropertySource(value = {"classpath:application.properties" }) 

public class AppConfig { 
    .... 
} 

让我知道如果我的答案不明确,我可以详细介绍一下它。

此链接可能会帮助您了解春季Web应用程序的初始化http://www.baeldung.com/spring-xml-vs-java-config