今天给大家介绍@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 = "&";}
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;
}
}
实现FactoryBean<T>接口有一下三个方法
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 +
'}';
}
}
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;
}
}
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();
}
}
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
}
}
IT技术分享社区
个人博客网站:https://programmerblog.xyz