Java tutorial
/* * Copyright 2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.slipp.config; import java.io.IOException; import javax.inject.Inject; import javax.sql.DataSource; import net.sf.ehcache.CacheException; import org.apache.commons.dbcp.BasicDataSource; import org.springframework.cache.CacheManager; 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.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.core.io.ClassPathResource; @Configuration @EnableCaching @ComponentScan(basePackages = "net.slipp", excludeFilters = { @Filter(Configuration.class) }) @PropertySource("classpath:application-private.properties") public class MainConfig { @Inject private Environment environment; @Bean(destroyMethod = "close") public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(environment.getProperty("jdbc.driverClassName")); dataSource.setUrl(environment.getProperty("jdbc.url")); dataSource.setUsername(environment.getProperty("jdbc.username")); dataSource.setPassword(environment.getProperty("jdbc.password")); return dataSource; } @Bean public CacheManager cacheManager() throws CacheException, IOException { EhCacheCacheManager cacheManager = new EhCacheCacheManager(); EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean(); factoryBean.setConfigLocation(new ClassPathResource("ehcache.xml")); factoryBean.afterPropertiesSet(); cacheManager.setCacheManager(factoryBean.getObject()); return cacheManager; } }