Java knowledge 16 Spring IOC container to create two ways

1, the container object directly IOC  

 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

Package together:

 1 public class ApplicationContextUtil {
 2     private static ApplicationContext applicationContext = null;
 3     public ApplicationContextUtil(){  
 4         //无参构造器,可以不用写
 5     }
 6     
 7     static{
 8         applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 9     }
10     
11     public ApplicationContext getApplicationContext() {
12         return applicationContext;
13     }

When used, the name of the class method calls directly to:. . ApplicationContextUtil ApplicationContextUtil ();

 

2, obtained by the object factory class created IOC container  

1 Resource resource = new ClassPathResource("applicationContext.xml");
2 BeanFactory factory = new XmlBeanFactory(resource);

Package together:

 1 public class ApplicationContextFactoryUtil {
 2     private static BeanFactory beanFactory = null;
 3     public ApplicationContextFactoryUtil(){
 4         
 5     }
 6     
 7     static{
 8         Resource resource = new ClassPathResource("applicationContext.xml");
 9         beanFactory = new XmlBeanFactory(resource);
10     }
11     
12     public BeanFactory getBeanFactory() {
13         return beanFactory;
14     }
15 }

When used, the name of the class method calls directly to:. ApplicationContextFactoryUti. The getBeanFactory ();

 

 

 

 

 

Original author: DSHORE

Home Author: http://www.cnblogs.com/dshore123/

From the original: https://www.cnblogs.com/dshore123/p/11682126.html

Welcome reproduced, reprinted, be sure to indicate the source. ( If this article helpful, you can click the bottom right corner of the recommendation , or comment, thank you! )

Guess you like

Origin www.cnblogs.com/dshore123/p/11682126.html