2016-05-16 6 views
0

web xmlに別のコンテキストを追加するにはどうすればよいですか?スプリングアプリケーションに別のコンテキストを追加する

これは、Javaクラスとして書かれた私のPersistanceContextです:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
     version="3.1"> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/applicationContext.xml 

     </param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
</web-app> 

そしてこれは、現在のアプリケーションである。別のアプリケーションコンテキストを(私はそれがデフォルトのものだと思う)が

@Configuration 
@EnableTransactionManagement 
@EnableJpaRepositories(basePackages = { 
     "ro.academy.model.daos" 
}) 
@ComponentScan(basePackages = "ro.academy.service") 
public class PersistenceContext { 
    @Bean(destroyMethod = "close") 
    DataSource dataSource(Environment env) { 
     BasicDataSource dataSourceConfig = new BasicDataSource(); 
     dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver")); 
     dataSourceConfig.setUrl(env.getRequiredProperty("db.url")); 
     dataSourceConfig.setUsername(env.getRequiredProperty("db.username")); 
     dataSourceConfig.setPassword(env.getRequiredProperty("db.password")); 

     return dataSourceConfig; 
    } 

    @Bean 
    LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, 
                   Environment env) { 
     LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); 
     entityManagerFactoryBean.setDataSource(dataSource); 
     entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); 
     entityManagerFactoryBean.setPackagesToScan("net.petrikainulainen.springdata.jpa.todo"); 

     Properties jpaProperties = new Properties(); 

     //Configures the used database dialect. This allows Hibernate to create SQL 
     //that is optimized for the used database. 
     jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect")); 

     //Specifies the action that is invoked to the database when the Hibernate 
     //SessionFactory is created or closed. 
     jpaProperties.put("hibernate.hbm2ddl.auto", 
       env.getRequiredProperty("hibernate.hbm2ddl.auto") 
     ); 

     //Configures the naming strategy that is used when Hibernate creates 
     //new database objects and schema elements 
     jpaProperties.put("hibernate.ejb.naming_strategy", 
       env.getRequiredProperty("hibernate.ejb.naming_strategy") 
     ); 

     //If the value of this property is true, Hibernate writes all SQL 
     //statements to the console. 
     jpaProperties.put("hibernate.show_sql", 
       env.getRequiredProperty("hibernate.show_sql") 
     ); 

     //If the value of this property is true, Hibernate will format the SQL 
     //that is written to the console. 
     jpaProperties.put("hibernate.format_sql", 
       env.getRequiredProperty("hibernate.format_sql") 
     ); 

     entityManagerFactoryBean.setJpaProperties(jpaProperties); 

     return entityManagerFactoryBean; 
    } 

    @Bean 
    JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { 
     JpaTransactionManager transactionManager = new JpaTransactionManager(); 
     transactionManager.setEntityManagerFactory(entityManagerFactory); 
     return transactionManager; 
    } 
} 

ウェブXML xmlファイルとして作成されたコンテキスト:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <context:annotation-config/> 
</beans> 

春はないようです私のPersistanceContextを認識して、自分の設定をすべて保持します。

アプリケーションに追加する方法や、春にそれを認識させる方法はありますか?
事前に感謝します!

答えて

1

PersistentContextクラスの別の注釈として@ImportResource("classpath:applicationContext.xml")を追加します。あなたが逆のアプローチを取るしたい場合は

また、あなたが春のXMLから

変更をJavaの設定をロードするために春のコンテキストxmlファイルに<context:component-scan base-package="com.javaconfig" />パッケージを追加する必要があり、web.xmlの代わり

<context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </context-param>を追加base-packageをjava configが存在するパッケージ名に置き換えます。

関連する問題