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

This commit is contained in:
Antoine Rey 2014-07-02 17:57:17 +02:00
parent 03f9ccc72d
commit 40c3c6aa46
3 changed files with 44 additions and 52 deletions

View file

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