Original address: https://www.cnblogs.com/redcool/p/6413461.html
1. BeanFactory
Beanfactory, ending at the end of Factory, indicates that it is a factory (interface) for a factory for managing Bean. In Spring, BeanFactory is the core interface of IOC containers. Its responsibilities include: instance, positioning, configuration of objects in applications, and dependence between these objects.
Spring provides us with many easy -to -use BeanFactory implementation. XMLBEANFACTORY is a commonly used one. The implementation of the dependencies between objects and objects described by XML will be described in XML. The XMLBEANFACTORY class will hold this XML configuration metadata and use it to build a completely configurable system or application.
instantiated container
1 Resource resource = new FileSystemResource("beans.xml"); 2 BeanFactory factory = new XmlBeanFactory(resource);
1 ClassPathResource resource = new ClassPathResource("beans.xml"); 2 BeanFactory factory = new XmlBeanFactory(resource);
1 ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "applicationContext-part2.xml"}); 3 BeanFactory factory = (BeanFactory) context;
Basically, that’s it, and then use the getBean (String Beanname) method to obtain the instance of the Bean; the method provided by BeanFactory and its simple, only six methods are provided for customers to call:
- Boolean Containsbean (String Beanname) Determine whether the factory contains the bean definition given by the given name, if any, return True
- Object getBean (string) returns a Bean instance with a given name registration. According to the configuration of the Bean, if it is the Singleton mode, a shared example will be returned, otherwise it will return a new instance. If it is not found to be specified, this method may throw an abnormality.
- Object Getbean (String, Class) Return to the Bean instance registered for a given name and convert it into a given class
- Class Gettype (String name) Returns the class of Bean with a given name. If not found a specified bean instance, exclude NosuchBeandefinitionException abnormalities
- BOOLEAN ISSINGLETON (String) determine whether the bean definition of the given name is a singles mode
- string [] GetaliaSes (String name) Return to all alias of the given bean name
2. FactoryBean
At the end of Bean, it means that it is a bean, which is different from the ordinary bean: it is the bean that implements the FactoryBean <t> interface. According to the ID of the bean from the BeanFactory Objects, not FactoryBean itself. If you want to get the FactoryBean object, add a & symbol in front of the ID to get it.
For example, you can implement a FactoryBean yourself. Function: Used to proxy an object, intercept all methods of the object, output a line of logs before and after calling, and imitate the function of ProxyFactoryBean.
1 /** 2 * my factory bean<p> 3 * A proxy of a class, intercept all the methods of this class, and output the log before and after the call of the method. 4 * @author daniel.zhao 5 * 6 */ 7 public class MyFactoryBean implements FactoryBean<Object>, InitializingBean, DisposableBean { 8 9 private static final Logger logger = LoggerFactory.getLogger(MyFactoryBean.class); 10 11 private String interfaceName; 12 13 private Object target; 14 15 private Object proxyObj; 16 17 @Override 18 public void destroy() throws Exception { 19 logger.debug("destroy......"); 20 } 21 22 @Override 23 public void afterPropertiesSet() throws Exception { 24 proxyObj = Proxy.newProxyInstance( 25 this.getClass().getClassLoader(), 26 new Class[] { Class.forName(interfaceName) }, 27 new InvocationHandler() { 28 29 @Override 30 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 31 logger.debug("invoke method......" + method.getName()); 32 logger.debug("invoke method before......" + System.currentTimeMillis()); 33 Object result = method.invoke(target, args); 34 logger.debug("invoke method after......" + System.currentTimeMillis()); 35 return result; 36 } 37 38 }); 39 logger.debug("afterPropertiesSet......"); 40 } 41 42 @Override 43 public Object getObject() throws Exception { 44 logger.debug("getObject......"); 45 return proxyObj; 46 } 47 48 @Override 49 public Class<?> getObjectType() { 50 return proxyObj == null ? Object.class : proxyObj.getClass(); 51 } 52 53 @Override 54 public boolean isSingleton() { 55 return true; 56 } 57 58 public String getInterfaceName() { 59 return interfaceName; 60 } 61 62 public void setInterfaceName(String interfaceName) { 63 this.interfaceName = interfaceName; 64 } 65 66 public Object getTarget() { 67 return target; 68 } 69 70 public void setTarget(Object target) { 71 this.target = target; 72 } 73 74 public Object getProxyObj() { 75 return proxyObj; 76 } 77 78 public void setProxyObj(Object proxyObj) { 79 this.proxyObj = proxyObj; 80 } 81 82 }
xml-bean configuration is as follows
1 <bean id="fbHelloWorldService" class="com.ebao.xxx.MyFactoryBean"> 2 <property name="interfaceName" value="com.ebao.xxx.HelloWorldService" /> 3 <property name="target" ref="helloWorldService" /> 4 </bean>
Junit Test class
@ContextConfiguration(classes = { MyFactoryBeanConfig.class })
public class MyFactoryBeanTest {
@Autowired
private ApplicationContext context;
/**
* Test verification FactoryBean principle, proxy a printed log can also be used for other treatment before calling its method.
* Get a customized FactoryBean from ApplicationContext
* Context.getBean (String Beanname) —-> The Object obtained by the final obtained is factoryBean.getobejct (),
* Use proxy.newinstance to generate the agent class of the service
*/
@Test
public void testFactoryBean() {
HelloWorldService helloWorldService = (HelloWorldService) context.getBean(“fbHelloWorldService”);
helloWorldService.getBeanName();
helloWorldService.sayHello();
}
}
In fact, the characteristic of FactoryBean can achieve a lot of useful functions, and achieve more comments and more comments and discuss it together.