1. Import the Jar package

3.0.2 is the integration of Spring with other utility classes on the market.

Import corresponding Jar packages according to the functions to be implemented. Basic function is to import beans, core, context, and expression.

In addition, you need to import the log package. Otherwise, errors will be reported

2. Import constraints and write configuration files (The constraint file of Spring configuration files is Schema).

  • Location of the file: SRC is recommended. The name of the configuration file is applicationContext.xml
  • Import constraints:
  1. Select the path to the constraint file

  1. Add the constraint when the XML file is created.

Open the view

  1. Introduction of constraints

  • Write a configuration file to register objects in the configuration file
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
  <bean name="User" class="com.itheima.pojo.User"></bean>
</beans>
Copy the code
  • Test configuration success cases
public class Demo {
	@Test
	public void fun (){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		User u = (User) ac.getBean("User"); System.out.println(u); }}Copy the code

The configuration,

  1. Bean element: Use this element to describe objects that need to be managed by Spring
attribute Break down
class The full class name of the managed object
name Give the managed object a name. Get the object by this name when retrieving it.(== can be repeated and special characters can be used, but == is not recommended.)
id Same as the name attribute (== cannot be repeated, special characters == cannot be used)

Conclusion: Try to use the name attribute ==

  1. The scope attribute:
value Break down
singleton Indicates that this object is a singleton and only one instance will exist in the Spring container
prototype Indicates that the object is an example. A new object is created each time the object is fetched in the Spring container.
request Indicates that the object has the same life cycle as the Request and is removed from the Spring container at the end of a request
session Indicates that the object has the same life cycle as the session and is removed from the Spring container when the session ends

Note: The default value of scope is Singleton, and the scope value of the Action object needs to be set to Prototype when struts2 is integrated. Because the action object must be multi-instance

  1. Init&destory (initialization and destruction)

Object initialization is performed when the container is started, because all objects in the configuration file are created when the container is started. When the container is closed, object destruction is performed.

Note that the ApplicationContext interface does not have closed methods. A subinterface ClassPathApplicationContext.

// Entity class public voidinit (){
		System.out.println("User object initialized");
	}
	
	public void destory (){
		System.out.println("The user object was destroyed."); } // In the configuration file <bean name="user" class="com.demo.pojo.User" init-method="init" destroy-method="destory"></bean> // execute @test public voidfun2() {/ / create a container object ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext.xml"); // Get User object User u = (User) ac.getbean ("user");
		System.out.println(u);
		ac.close();
	}
Copy the code
  1. modular

As the number of project objects increases, the configuration needs to be modular, and other configuration files can be introduced from one configuration file

<import resource=""/>
Copy the code

Spring(Three ways to create objects)

1. Empty parameter construction mode

Because the interface we are using is applicationContext, the objects in the configuration file are all created when the container is started.

@Test
	public void fun1 (){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	<bean name="user" class="com.itheima.pojo.User"></bean>
Copy the code

2. Static factory

We want us developers to create the objects ourselves, and then hand them over to the Spring container to manage. We also get it from the container when we need it

public static User createUser (){
		
		System.out.println("User object created");
		
		return new User ();
	}


<bean name="user2" 
	class="com.itheima.test.UserFactory" 
	factory-method="createUser"></bean>
Copy the code

3. Example factory

Since the method is not static, it cannot be called by the class name, because we must create the object to call the method and create the desired object.

@Test
public void fun1 (){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User u = (User) ac.getBean("user3");

}


public User createUser2 (){

System.out.println("User object created on the fly");

returnnew User (); } // Config file <bean name="userFactory" class="com.demo.test.UserFactory"></bean>
	<bean name="user3" 
	factory-method="createUser2"
	factory-bean="userFactory">
	</bean>
Copy the code

Spring property injection

  1. Set injection
  • Properties into
<bean name="user" class="com.demo.pojo.User">
     <property name="name" value="tom"></property>
     <property name="age" value="18"></property>
     </bean>
Copy the code
  • Objects into
<bean name="user" class="com.demo.pojo.User">
     <property name="name" value="tom"></property>
     <property name="age" value="18"></property>
     <property name="Car" ref="car"></property>
 </bean>
     
     <bean name="car" class="com.demo.pojo.Car">
      <property name="name" value="AE86"></property>
      <property name="color" value="White"></property>
     </bean>

Copy the code

Note: Value types use the value attribute and reference types use the ref attribute.

  1. Constructor injection

The constructor – arg elements

attribute Break down
name Object property name
value value
ref What object to refer to
index The order of the constructor’s argument list
type The type of argument to the constructor.
<bean name="user" class="com.demo.pojo.User">
        <constructor-arg name="name" value="666" index="1" type="java.lang.Integer"></constructor-arg>
        <constructor-arg name="car" ref="car" index="0"</constructor-arg> </bean> public User(String name, Car Car) {this.name = name; this.car = car; } public User(Car car, Integer name) { this.name = name+"";
		this.car = car;
	}
Copy the code
  1. P namespace injection

Pre-work: import the P namespace

Add the following code to the root element

 xmlns:p="http://www.springframework.org/schema/p"  
Copy the code

That’s set injection

<bean name="user2" class="com.demo.pojo.User" p:name="tom"
		p:age="18" p:car-ref="car"></bean>
Copy the code
  1. Spel injection
<! -- spring expression language --> <bean name="user3" class="com.demo.pojo.User">
		<property name="name" value="#{user.name}"></property>
		<property name="age" value="#{user1.age}"></property>
		<property name="car" ref="car"></property>
	</bean>
Copy the code

Note: Reference types cannot use SPEL, just as we used reference types before

  1. Complex type injection

Array, collection,Map collection,Properties

Note: When there is only one value, the value can be written in the value attribute of the property element

 <property name="arr" value="tom"></property>
 <property name="list" value="jerry"></property>
Copy the code
<bean name="cb" class="com.demo.pojo.CollectionBean">
	    <property name="arr">
	       <array>
	           <value>tom</value>
	           <value>jerry</value>
	           <ref bean="car"/>
	       </array>
	    </property>
	    
	    <property name="list">
	       <list>
	         <value>#{user.name}</value>
	         <value>#{user.age}</value>
	         <ref bean="car"/>
	       </list>
	    </property>
	    
	    <property name="map">
	      <map>
	         <entry key="name" value="mama"></entry>
	         <entry key="uu" value-ref="user2"></entry>
	         <entry key-ref="user2" value-ref="user3"></entry>
	      </map>
	    </property>
	    
	    <property name="prop">
	       <props>
	           <prop key="name">tom</prop>
	           <prop key="age">18</prop>
	       </props>
	    </property>
	</bean>
Copy the code