2017-10-08 1 views
0

스프링 데이터 JPA를 사용 중이며 XML 대신 Java 클래스에서 데이터 소스, 엔티티 관리자 및 최대 절전 모드 구성을 정의했습니다. 여기 내 수업이 있습니다 :스프링 데이터 jspa에서 지속성 구성 파일로드

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 정의를 만들어야합니까? 응용 프로그램 시작시 어떻게로드됩니까? 우리는 스프링 부트를 사용하지 않고 메인 메소드 등은 없습니다.

답변

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); 

     ... 
} 

아래 예 참조하거나 @Configuration과 주석을 구성 빈을로드에 당신은 당신의 appconfig가 클래스를 등록하고 PersistenceConfig가 위치한 패키지 (com.example.config)을 스캔하도록 설정할 수 있습니다 .

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

내 대답이 명확하지 않으면 알려주세요. 그것에 대해 자세히 설명해 드리겠습니다.

이 링크는 봄 웹 응용 프로그램 초기화에 대해 도움이 될 수 있습니다. http://www.baeldung.com/spring-xml-vs-java-config