弱龄寄事外,委怀在琴书。这篇文章主要讲述Spring系列:@FactoryBean注解用法介绍相关的知识,希望能为你提供帮助。
?今天给大家介绍@FactoryBean注解用法,希望对大家能有所帮助!?
?1、@FactoryBean注解介绍??FactoryBean是实现了FactoryBean<
T>
接口的Bean,可以该Bean的ID从BeanFactory中获取的实际上是FactoryBean中getObject()方法返回的实例对象,而并不是直接FactoryBean本身,想要获取FactoryBean对象本身,可以在id前面加一个&
符号来获取。?
?BeanFactory部分代码:?
?说明:String FACTORY_BEAN_PREFIX = "&
" ,&
符号表示要获取FactoryBean本身?
package org.springframework.beans.factory;
import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
public interface BeanFactory
String FACTORY_BEAN_PREFIX = "&
";
?2、@FactoryBean的源码内容如下:??Spring3.0版本之后,FactoryBean开始支持泛型,即接口声明改为FactoryBean<
T>
的形式?
package org.springframework.beans.factory;
import org.springframework.lang.Nullable;
public interface FactoryBean<
T>
String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";
@Nullable
T getObject() throws Exception;
@Nullable
Class<
?>
getObjectType();
default boolean isSingleton()
return true;
【Spring系列(@FactoryBean注解用法介绍)】?实现FactoryBean<
T>
接口有一下三个方法?
?3、用法示例??3.1 新建Book.java?
package com.spring.bean;
public class Book
private String bookName;
private String bookType;
private double price;
public Book(String bookName, String bookType, double price)
this.bookName = bookName;
this.bookType = bookType;
this.price = price;
public String getBookName()
return bookName;
public void setBookName(String bookName)
this.bookName = bookName;
public String getBookType()
return bookType;
public void setBookType(String bookType)
this.bookType = bookType;
public double getPrice()
return price;
public void setPrice(double price)
this.price = price;
@Override
public String toString()
return "Book" +
"bookName=" + bookName + \\ +
", bookType=" + bookType + \\ +
", price=" + price +
;
?3.2 新建BookFactoryBean.java?
package com.spring.bean;
import org.springframework.beans.factory.FactoryBean;
public class BookFactoryBean implements FactoryBean<
Book>
public BookFactoryBean factoryBeanVO()
return new BookFactoryBean();
public Book getObject() throws Exception
return new Book("红楼梦", "中国名著", 88);
public Class<
?>
getObjectType()
return Book.class;
public boolean isSingleton()
return true;
?3.3 FactoryBeanConfig.java?
package com.spring.config;
import com.spring.bean.BookFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FactoryBeanConfig
@Bean
public BookFactoryBean bookFactoryBean()
return new BookFactoryBean();
?3.4 TestFactoryBean.java?
package com.spring.test;
import com.spring.config.FactoryBeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestFactoryBean
public static void main(String[] args)
AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(FactoryBeanConfig.class);
// 获取工厂类的bean
Object bookFactoryBean = annotationContext.getBean("bookFactoryBean");
System.out.println(bookFactoryBean.getClass());
// 输出结果: class com.spring.bean.Book
//获取FactoryBean 本身 加 &
符号
Object bookFactoryBean1 = annotationContext.getBean("&
bookFactoryBean");
System.out.println(bookFactoryBean1.getClass());
// 输出结果: class com.spring.bean.BookFactoryBean
?4、@FactoryBean应用场景?
?5、典型应用?
推荐阅读
- Termux镜像在阿里云镜像站首发上线
- Linux环境下部署Jpress大型博客网站 #yyds干货盘点#
- 磁盘文件系统管理及挂载管理
- 使用APICloud平台实现朋友圈功能
- 威联通(QNAP)NAS忘记密码怎么办(手把手教你!请收藏!关键时刻有用!)
- 涨粉写作经验分享 - 写作技巧
- 虚拟机快照及bash初识
- 分享一个定义需求优先级的公式,让迭代尽可能实现最有价值功能
- C++核心编程中的函数-占位参数和默认参数