Spring @Configuration和FactoryBean
<cache:annotation-driven /><context:component-scan base-package='org.bk.samples.cachexml'></context:component-scan><bean id='cacheManager' class='org.springframework.cache.support.SimpleCacheManager'><property name='caches'><set><ref bean='defaultCache'/></set></property></bean><bean name='defaultCache' class='org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean'><property name='name' value='default'/></bean>
工厂bean ConcurrentMapCacheFactoryBean是一个依次负责创建Cache bean的bean。
我第一次尝试将此设置转换为@Configuration样式:
@Bean
public SimpleCacheManager cacheManager(){SimpleCacheManager cacheManager = new SimpleCacheManager();List<Cache> caches = new ArrayList<Cache>();ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean();cacheFactoryBean.setName('default');caches.add(cacheFactoryBean.getObject());cacheManager.setCaches(caches );return cacheManager;
}
但是,这没有用,原因是我在这里完全绕过了一些Spring bean生命周期机制。 事实证明,ConcurrentMapCacheFactoryBean还实现了InitializingBean接口,并在InitializingBean的'afterPropertiesSet'方法中对缓存进行了急切的初始化。 现在,通过直接调用factoryBean.getObject(),我完全绕过了afterPropertiesSet方法。
有两种可能的解决方案:
1.以与在XML中定义的相同方式定义FactoryBean:
@Bean
public SimpleCacheManager cacheManager(){SimpleCacheManager cacheManager = new SimpleCacheManager();List<Cache> caches = new ArrayList<Cache>();caches.add(cacheBean().getObject());cacheManager.setCaches(caches );return cacheManager;
}@Bean
public ConcurrentMapCacheFactoryBean cacheBean(){ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean();cacheFactoryBean.setName('default');return cacheFactoryBean;
}
在这种情况下,从@Bean方法返回一个显式的FactoryBean,Spring将负责在此bean上调用生命周期方法。
2.复制相关生命周期方法中的行为,在此特定实例中,我知道FactoryBean在afterPropertiesSet方法中实例化ConcurrentMapCache,我可以通过以下方式直接复制此行为:
@Bean
public SimpleCacheManager cacheManager(){SimpleCacheManager cacheManager = new SimpleCacheManager();List<Cache> caches = new ArrayList<Cache>();caches.add(cacheBean());cacheManager.setCaches(caches );return cacheManager;
}@Bean
public Cache cacheBean(){Cache cache = new ConcurrentMapCache('default');return cache;
}
将FactoryBean从xml转换为@Configuration时要记住的一点。
注意:
可以根据需要进行一页有效的测试:
package org.bk.samples.cache;import static org.hamcrest.MatcherAssert.assertThat;import static org.hamcrest.Matchers.equalTo;import java.util.ArrayList;import java.util.List;import java.util.Random;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.Cache;import org.springframework.cache.annotation.Cacheable;import org.springframework.cache.annotation.EnableCaching;import org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean;import org.springframework.cache.support.SimpleCacheManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.stereotype.Component;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes={TestSpringCache.TestConfiguration.class})public class TestSpringCache {@Autowired TestService testService;@Testpublic void testCache() {String response1 = testService.cachedMethod('param1', 'param2');String response2 = testService.cachedMethod('param1', 'param2');assertThat(response2, equalTo(response1));}@Configuration@EnableCaching@ComponentScan('org.bk.samples.cache')public static class TestConfiguration{@Beanpublic SimpleCacheManager cacheManager(){SimpleCacheManager cacheManager = new SimpleCacheManager();List<Cache> caches = new ArrayList<Cache>();caches.add(cacheBean().getObject());cacheManager.setCaches(caches );return cacheManager;}@Beanpublic ConcurrentMapCacheFactoryBean cacheBean(){ConcurrentMapCacheFactoryBean cacheFactoryBean = new ConcurrentMapCacheFactoryBean();cacheFactoryBean.setName('default');return cacheFactoryBean;}}}interface TestService{String cachedMethod(String param1,String param2);}@Componentclass TestServiceImpl implements TestService{@Cacheable(value='default', key='#p0.concat('-').concat(#p1)')public String cachedMethod(String param1, String param2){return 'response ' + new Random().nextInt();}}
参考: all和其他博客中来自我们JCG合作伙伴 Biju Kunjummen的Spring @Configuration和FactoryBean 。
翻译自: https://www.javacodegeeks.com/2012/08/spring-configuration-and-factorybean.html
Spring @Configuration和FactoryBean相关推荐
- 六、spring之通过FactoryBean为ioc容器中添加组件
前面我们已经介绍了几种为容器中添加组件的方法,今天一起学习通过FactoryBean添加组件的方法. 首先我们准备一个类,也就是我们需要注册进spring的ioc容器中的类 类Color: // 不必 ...
- Spring @Configuration和@Component的区别(enhancer的原因)
Spring @Configuration 和 @Component 区别 一句话概括就是 @Configuration 中所有带 @Bean 注解的方法都会被动态代理,因此调用该方法返回的都是同一个 ...
- 给容器中注册组件 || @Scope -- @Lazy -- @Conditional({Condition}) -- @Import--使用Spring提供的 FactoryBean
* @Scope:调整作用域 * prototype:多实例的:ioc容器启动并不会去调用方法创建对象放在容器中. * 每次获取的时候才会调用方法创建对象: ...
- Spring @Configuration – RabbitMQ连接
我一直在转换必须使用Spring @Configuration机制配置到RabbitMQ的连接的应用程序-最初,我使用xml bean定义文件来描述配置. 这是我的原始配置: <beans .. ...
- 解决 Unmapped Spring configuration files found.Please configure Spring facet.
最近在学习使用IDEA工具,觉得与Eclipse相比,还是有很多的方便之处. 但是,当把自己的一个项目导入IDEA之后,Event Log提示"Unmapped Spring configu ...
- Spring - Configuration Metadata
metadata:元数据 Spring configuration metadata则是告知Spring容器: 如何初始化,配置,包裹,和组合应用内特定的对象. Spring从2002年发布第一版到至 ...
- IDEA Unmapped Spring configuration files found.
2019独角兽企业重金招聘Python工程师标准>>> Spring Configuration Check Unmapped Spring configuration fil ...
- Unmapped Spring configuration files found. Please configure Spring facet or use 'Create Default Con
今天打开项目,突然报了上面的警告,没在意,运行Tomcat时报错如下错误!! 原因是web工程中的spring配置文件没有被IDEA所管理 Error:Internal error: (java.la ...
- Spring configuration check
参考:https://blog.csdn.net/fighting_wzc/article/details/78960045?utm_medium=distribute.pc_relevant_t0. ...
最新文章
- 走近酷点KoolPoint,让我们深度挖掘网管软件的设计细节
- 前端 学习笔记day48 CSS介绍
- Java写十进制和二进制互转
- CodeIgniter的快速操作
- Android中JSON解析细解及实例
- java 操作日志设计_日志系统新贵 Loki,确实比笨重的ELK轻
- ssh图片上传 java_ssh上传并显示图片
- JavaScript覆盖率统计实现
- java 使用json-lib 对象,String,json互转
- keil uvisoin软件出现闪退和打开工程以前添加的c文件上面出现黄色感叹号
- Crontab使用心得
- android记账系统预算功能,怎么挑选合适的 Android 记账应用?你可以从记账需求入手...
- 微软drive服务器,OneDrive:微软云存储服务
- Dukto 傻瓜安装教程
- “MATLAB拒绝访问”问题的解决方法
- windows云服务器价格_windows云服务器安装(微软云服务器价格)
- vite使用vite-aliases插件配置路径别名
- 普通2d视频转3d视频
- 安卓搭建http服务器——NanoHttpd
- TBB基础之parallel_for
热门文章
- maven 版本号插件_测试Maven版本插件自动递增版本号
- idea 构建spring_以Spring方式构建企业Java应用程序
- 白盒测试方法静态分析_静态分析的教育方面
- java bean 验证_Java Bean验证基础
- neo4j cypher_Neo4j:Cypher –避免热切
- 硒等待:内隐,外显,流利和睡眠
- 无服务器:不费吹灰之力!
- 使用Java 9向Javadoc搜索添加术语
- 使用Eclipse和Open Liberty的Java EE 8上的Java 9
- mapreduce 聚合_MapReduce:处理数据密集型文本处理–局部聚合第二部分