在spring数据jpa中加载持久性配置文件

时间:2017-10-08 06:13:10

标签: java spring spring-data-jpa

我正在使用spring数据JPA,并在Java类而不是XML中定义了数据源,实体管理器和hibernate配置。这是我的班级:

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启动,没有主要的方法等。

1 个答案:

答案 0 :(得分: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类并将其设置为扫描包(com.example.config),其中的PersistenceConfig将加载使用@Configuration注释的配置bean。

@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 {
     ....
}

如果我的答案不清楚,请告诉我,我可以详细介绍一下。

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