一、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 + "]";
}
}