spring中BeanFactory与FactoryBean区别

一、BeanFactory

参照这篇文章:https://www.cnblogs.com/redcool/p/6413461.html

二、FactoryBean

      注入FactoryBean最终取到的是getObject方法返回值,并可通过isSingleton方法设置是否为单例。如下:

         1、PersonFactoryBean :

package com.hubena.FactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

import com.hubena.entity.IPerson;
import com.hubena.entity.PersonAImpl;

@Component(value = "personFactoryBean")
public class PersonFactoryBean implements FactoryBean<IPerson> {
	private static final Logger logger = LoggerFactory.getLogger(PersonFactoryBean.class);
	
	private PersonAImpl personA;
	private IPerson proxyObject;
	
	
	/**
	 * {@inheritDoc}<P>
	 * 返回需要的类,并为其生成代理。
	 */
	@Override
	public IPerson getObject() throws Exception {
		personA = new PersonAImpl();
		IPerson proxyObject = (IPerson) Proxy.newProxyInstance(personA.getClass().getClassLoader(), personA.getClass().getInterfaces(), new InvocationHandler() {
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				logger.error("proxy参数:{},method参数{}", proxy.getClass(), method.getName());
				Object result = method.invoke(personA, args);
				return result;
			}
		});
		return proxyObject;
	}

	@Override
	public Class<?> getObjectType() {
		return proxyObject == null ? Object.class : proxyObject.getClass();
	}
	
	@Override
	public boolean isSingleton() {
		return false;
	}

}

          2、FactoryBeanTest 

package com.hubena.FactoryBean;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.hubena.FactoryBean.Configuration.FactoryBeanSpringConfiguration;
import com.hubena.entity.IPerson;

/**
 * 测试FactoryBean.
 * @author Zengxb
 *
 */
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {FactoryBeanSpringConfiguration.class})
public class FactoryBeanTest {
	private static final Logger logger = LoggerFactory.getLogger(FactoryBeanTest.class);
	
	@Resource(name = "personFactoryBean")
	IPerson iPerson;
	
	@Resource(name = "personFactoryBean")
	IPerson iPerson2;
	
	
	@Test
	public void testFactoryBean() {
		iPerson.eatSome();
		
		// 结果为:false,说明scope为prototype的类多次注入的类不同
		logger.error(String.valueOf((iPerson == iPerson2)));
	}
}

           3、FactoryBeanSpringConfiguration 

package com.hubena.FactoryBean.Configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(value = {"com.hubena.FactoryBean"})
public class FactoryBeanSpringConfiguration {
	
}

          4、IPerson 

package com.hubena.entity;

public interface IPerson {
	
	public default void eatSome() {
		System.out.println("名字为Person的人开始吃东西了!");
	}
}

          5、PersonAImpl

package com.hubena.entity;

import java.text.MessageFormat;

public class PersonAImpl implements IPerson {
	private String name;
	private int age;
	
	
	@Override
	public void eatSome() {
		System.out.println(MessageFormat.format("名字为:{0},类名为{1}", 
			this.getName(), this.getClass().getName()));
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
	
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值