7, IOC of spring
<bean id="hello" class="com.wang.bean.Hello" scope="singleton" init-method="init" autowire="byType" destroy-method="destroy"> <property name="userName" value="spring"></property> </bean>
Container usage 1: create the object of the container and tell it to initialize and create the container and content with the specified configuration file. If the bean in the container needs to be used in the program, get it directly from the container and use it.
//Use the Hello class in the way spring requires @Test public void test2(){ //Create spring container objects. There are many kinds of container objects. Pay attention to their usage //The current configuration is written in xml, ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:applicationcontext.xml"); Hello hello =(Hello)ac.getBean("hello"); System.out.println(hello); }
Container usage 2: use the spring test component to automatically create the container object by using the configuration file. Use the @ Resource annotation in the class to match the variable name and bean name. If there is a bean with the same name, give the bean object to the injected attribute. At this time, the attribute refers to the bean in the container. The bean can be used anywhere in the class.
//As a spring test class, you need to add two annotations @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationcontext.xml") public class SpringDemo1SpringTest { //Beans that can be injected automatically @Resource Hello hello; @Test public void test(){ System.out.println(hello); } }
Connections and differences:
1. Both use IOC and DI provided by spring container to complete bean creation and injection.
2. Both create spring containers and need spring configuration files.
3. Usage 1 requires you to create a container yourself, and usage 2 requires spring test to create a container.
4. In usage 1, we need to get beans from the container and use them. In usage 2, the container injects beans into objects for us to use directly.
5. Obviously, usage two is more comfortable and elegant than usage one.
What is IOC (control reversal)
In the traditional way, an object needs to use other objects, and it is responsible for creating the object itself; The IOC method is that other objects to be used by an object are not created by itself, but by the container. When it needs to be used, the object is injected in. This change reflects a reversal of action, transforming what you do into what others do. IOC is an important part of modern programming thought. Compared with the traditional way, great changes have taken place in thought and behavior. It is more a programming idea than a specific implementation
What is DI (dependency injection)
An object needs to use other objects. It does not create objects by itself. The container creates and provides objects. Therefore, it is a concrete implementation of IOC. In the spring framework, the IOC programming idea is realized by DI.
8, How spring creates containers and initializes bean s
1. BeanFactory interface, which represents the container of spring and has the most basic status. Spring itself uses this interface to use the container.
2. ApplicationContext interface, which represents the spring container, inherits from BeanFactory and has stronger functions, reflecting the characteristics of the container with parent-child structure; It is mainly used in application programming.
Combined with 1 and 2, we know that if we want to use containers in our own programs, we use the second interface.
3. The concrete implementation of the container provided by spring
(1) spring can work with any type of Java program, such as ordinary Java projects, such as Web projects, such as projects configured with annotations, etc. different types of projects have different ways to start and manage containers, so different container implementations are required for them, Just like different databases have different drivers. But these drivers implement jdbc interfaces.
(2) implementation classes of common containers
ClassPathXmlApplicationContext, search by classpath, and configure the container with XML.
FileSystemXmlApplicationContext is a container configured with XMl according to the directory location of the file system
AnnotationConfigApplicationContext uses java annotations as the container for configuration
Stubbwebapplicationcontext, a container used in java Web applications, can use both xml and java configuration classes
4. spring container creation process
(1) the process of creating a container is very complex, in which many interfaces and classes are used, which are staggered with each other.
(2) if you can explain the principle of the container clearly in the interview with programmers from large factories, you can get extra points.
5. Key points in the working process of container
(1) get the content of the configuration file through a character input stream (Reader) provided by spring, and analyze the configuration content in detail, that is, read the node, attributes and child nodes of the node into a certain data structure according to the basic structure of the configuration content. Xmlbean definitionreader is the configuration reader mentioned above, which is created in the xmlbean factory class. There are multiple overloaded methods of loadBeanDefinitions in XMLBeanDefinitionReader, which are used to encapsulate the Bean definition content into the BeanDefinition object.
(2) analyze bean attributes and create beans. The BeanDefinition interface reflects the specific attributes and characteristics of beans. The createBeanDefinition method of the BeanDefinitionParseDelegate class creates an object defined by the bean. parseBeanDefinitionElement is used to parse the configuration content of the bean. The parsePropertyValue method parses the property value of the bean.
(3) registered bean
The DefaultListableBeanFactory class has the Map<String, BeanDefinition>beanDefinitionMap=new ConcurrentHashMap (256) attribute, which is a thread safe Map. All Bean objects that are created will be registered in the Map, so when the getBean() method is invoked in the program, the object of Bean is obtained from the Map. The registerBeanDefinition method in the above class is responsible for registering the created bean in the above map.
9, How to define beans in the configuration file, that is, how to use DI (dependency injection)
1. Defines the basic properties of a Bean
<bean id="hello" class="com.wang.bean.Hello"> <property name="userName" value="spring"></property> </bean>
id is the name of the bean. It cannot be repeated, but there can be multiple. class is the type of bean. Do not use interfaces. The name attribute has the same function as the id, so don't use name if you use id.
2. There are three ways of dependency injection
(1) attribute injection: the attribute of the class must have setXX method, which is called by the container to complete attribute injection. The most common way. In the configuration, you need to use the property child node to set name value pairs.
<property name="userName" value="spring"></property>
(2) construction method
<bean id="hello1" class="com.wang.bean.Hello" > <constructor-arg value="springgouzao" /> </bean>
(3) factory injection method (basically not used)
3. Attribute value of dependency injection
(1) the value is a direct quantity (that is, a number or string), and the value attribute is used during configuration. If it is null, use < / null >
(2) the value is the reference type (the current attribute refers to another Bean)
You can define an external bean first, and then use the ref attribute to reference the external bean through the property child node.
<bean id="person" class="com.wang.bean.Person"> <property name="name" value="Zhang San"></property> </bean>
<bean id="hello" class="com.wang.bean.Hello" scope="singleton" init-method="init" autowire="byType" destroy-method="destroy"> <property name="userName" value="spring"></property> <property name="person" ref="person"></property> </bean>
You can also use an internal bean (define a bean inside another bean without specifying an id)
<bean id="hello" class="com.wang.bean.Hello" scope="singleton" init-method="init" autowire="byType" destroy-method="destroy"> <property name="userName" value="spring"></property> <property name="person"> <bean class="com.wang.bean.Person"> <property name="name" value="Internal Zhang San"></property> </bean> </property> </bean>
(3) Values are collections
List collection:
Define a collection type, and then make the bean refer to the collection type.
<util:list id="list1"> <value>1</value> <value>abc</value> <value>koma</value> </util:list> <bean id="hello" class="com.wang.bean.Hello" scope="singleton" init-method="init" autowire="byType" destroy-method="destroy"> <property name="userName" value="spring"></property> <property name="person" ref="person"></property> <property name="list" ref="list1"></property> </bean>
You can also define a collection as an internal collection of beans, similar to the usage of internal beans.
Set set
<util:list id="list1"> <value>1</value> <value>abc</value> <value>koma</value> </util:list> <util:set id="set1"> <value>2</value> <value>def</value> <value>ouring</value> </util:set> <bean id="hello" class="com.wang.bean.Hello" scope="singleton" init-method="init" autowire="byType" destroy-method="destroy"> <property name="userName" value="spring"></property> <property name="person" ref="person"></property> <property name="list" ref="list1"></property> <property name="set" ref="set1"></property> </bean>
Map collection
<util:list id="list1"> <value>1</value> <value>abc</value> <value>koma</value> </util:list> <util:set id="set1"> <value>2</value> <value>def</value> <value>ouring</value> </util:set> <util:map id="map1"> <entry key="k1" value="v1"> </entry> <entry key="k2" value="v2"></entry> <entry key="k3" value="v3"></entry> </util:map> <bean id="hello" class="com.wang.bean.Hello" scope="singleton" init-method="init" autowire="byType" destroy-method="destroy"> <property name="userName" value="spring"></property> <property name="person" ref="person"></property> <property name="list" ref="list1"></property> <property name="set" ref="set1"></property> <property name="map" ref="map1"></property> </bean>
The above three types of collections are commonly used, including the Properties collection.
If the element of the collection is a basic type or string, use value to write the value of the direct quantity. If the element is a reference type, use ref to reference other beans.
ref attribute is used to reference collection type in Bean.