Migrate the tools-config.xml XML context configuration file to ToolsConfig.java

This commit is contained in:
Antoine Rey 2015-05-27 21:26:21 +02:00
parent 54420cd610
commit 32c59a5c58
4 changed files with 47 additions and 54 deletions

View file

@ -2,11 +2,9 @@ package org.springframework.samples.petclinic.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
@Configuration
@Import(BusinessConfig.class)
@ImportResource("classpath:spring/tools-config.xml")
@Import({BusinessConfig.class, ToolsConfig.class})
public class RootApplicationContextConfig {
}

View file

@ -0,0 +1,43 @@
package org.springframework.samples.petclinic.config;
import net.sf.ehcache.CacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.core.io.ClassPathResource;
import org.springframework.samples.petclinic.util.CallMonitoringAspect;
@Configuration
@EnableCaching // enables scanning for @Cacheable annotation
@EnableMBeanExport
@EnableAspectJAutoProxy
public class ToolsConfig {
@Bean
@Description("Call monitoring aspect that monitors call count and call invocation time")
public CallMonitoringAspect callMonitor() {
return new CallMonitoringAspect();
}
@Bean
@Autowired
public EhCacheCacheManager ehCacheCacheManager(CacheManager cacheManager) {
EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
ehCacheCacheManager.setCacheManager(cacheManager);
return ehCacheCacheManager;
}
@Bean
public EhCacheManagerFactoryBean cacheManager() {
EhCacheManagerFactoryBean ehCacheManager = new EhCacheManagerFactoryBean();
ehCacheManager.setConfigLocation(new ClassPathResource("cache/ehcache.xml"));
return ehCacheManager;
}
}

View file

@ -1,49 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Application context definition for PetClinic on JPA.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--
Simply defining this bean will cause requests to owner names to be saved.
This aspect is defined in petclinic.jar's META-INF/aop.xml file.
Note that we can dependency inject this bean like any other bean.
-->
<aop:aspectj-autoproxy>
<aop:include name="callMonitor"/>
</aop:aspectj-autoproxy>
<!-- Call monitoring aspect that monitors call count and call invocation time -->
<bean id="callMonitor" class="org.springframework.samples.petclinic.util.CallMonitoringAspect"/>
<!--
Exporter that exposes the CallMonitoringAspect via JMX,
based on the @ManagedResource, @ManagedAttribute, and @ManagedOperation annotations.
-->
<context:mbean-export/>
<!-- enables scanning for @Cacheable annotation -->
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"/>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:cache/ehcache.xml"/>
</bean>
</beans>

View file

@ -11,6 +11,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.samples.petclinic.config.BusinessConfig;
import org.springframework.samples.petclinic.config.ToolsConfig;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
@ -29,8 +30,8 @@ import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy({
@ContextConfiguration(classes = BusinessConfig.class),
@ContextConfiguration(locations = {"classpath:spring/tools-config.xml", "classpath:spring/mvc-core-config.xml"})})
@ContextConfiguration(classes = { BusinessConfig.class, ToolsConfig.class }),
@ContextConfiguration(locations = "classpath:spring/mvc-core-config.xml")})
@ActiveProfiles("spring-data-jpa")
public class VetControllerTests {