Spring注解驱动开发——组件注册 使用FactoryBean
实现类
package com.mongoubiubiu.bean; import org.springframework.beans.factory.FactoryBean; //创建一个spring 定义的工厂bean public class ColorFactoryBean implements FactoryBean<Color> { /** * 返回一个Color对象,这个对象会返回到容器中 */ @Override public Color getObject() throws Exception { // TODO Auto-generated method stub return new Color(); } /** * 返回对象的类型 */ @Override public Class<?> getObjectType() { // TODO Auto-generated method stub return Color.class; } /** * 放回对象是否是单实例的 * true:是单列的,在容器中保存一份 * false:不是单列的 */ @Override public boolean isSingleton() { // TODO Auto-generated method stub return false; } }
1 2 3 4 5 | package com.mongoubiubiu.bean; public class Color { } |
将ColorFactoryBean 注入到容器中
@Bean public ColorFactoryBean colorFactoryBean(){ return new ColorFactoryBean(); }
测试
要获取MyImportBeanDefinitionRegistrar 自己的bean 加前缀就好
确实获取到了
本文作者:KwFruit
本文链接:https://www.cnblogs.com/mangoubiubiu/p/15063751.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步