Spring --- learning notes of IOC (inversion of control) principle [all]

1. IoC how objects are created

  1. Creating objects using parameterless construction

  2. If you want to create with a parametric construct:

    1. Subscript assignment constructor ARG

      <!--Have reference-->
      <bean id="User" class="com.reliable.pojo.User" >
          <constructor-arg index="0" value="Reliable poplar"></constructor-arg>
      </bean>
      
      public User(String name){
      		System.out.println("User Parametric structure!");
      		this.name=name;
      	}
      
    2. By type type="java.lang.String"

        <bean id="User" class="com.reliable.pojo.User" >
            <constructor-arg type="java.lang.String" value="Reliable poplar"></constructor-arg>
        </bean>
    
    1. Through parameter name name="name" value="reliable"
        <bean id="User" class="com.reliable.pojo.User" >
            <constructor-arg name="name" value="reliable"></constructor-arg>
        </bean>
    

    Summary: when the configuration file is loaded, the objects managed in the Spring container have been initialized successfully!

2. Spring configuration

2.1 alias

    <!--alias-->
    <alias name="User" alias="new_user"></alias>

2.2 Bean configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
    Type variable name = new type();
    Hello hello = new Hello();

    bean namely java object , from Spring Create and manage
    bean = An object
    among
    id = Variable name
    class = new Object type
    property It is equivalent to setting a value for the attribute in the object
    -->
    <bean id="Hello" class="com.reliable.pojo.Hello">
        <property name="name" value="Spring"/>
    </bean>
    <!-- No parameter -->
    <!--<bean id="User" class="com.reliable.pojo.User">-->
        <!--<property name="name" value="reliable"></property>-->
    <!--</bean>-->
    <!--There is the first one, index-->
    <!--<bean id="User" class="com.reliable.pojo.User" >
        <constructor-arg index="0" value="Reliable poplar"></constructor-arg>
    </bean>-->
    <!-- 2 type-->
   <!-- <bean id="User" class="com.reliable.pojo.User" >
        <constructor-arg type="java.lang.String" value="Reliable poplar"></constructor-arg>
    </bean>-->
    <!-- 3 Parameter name -->
    <bean id="User" class="com.reliable.pojo.User" >
        <constructor-arg name="name" value="User"></constructor-arg>
    </bean>
    <bean id="User1" class="com.reliable.pojo.User1">
        <constructor-arg name="name" value="User1"></constructor-arg>
    </bean>
    <!--Alias if you add an alias, you can use it-->
    <alias name="User" alias="new_user"></alias>
</beans>

2.3,import

It is generally used for team development. Multiple configuration files can be imported and merged into one

    <!--import -->
    <import resource="beans1.xml"></import>

3. Dependency injection (DI)

3.1 constructor injection

  • Dependency injection: Set injection
    • Dependency: the creation of bean objects depends on the container
    • Injection: all attributes in the bean object are injected by the container!

3.2 Set mode injection [ key ]

  • Complex type
public class Address {
	private String address;
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
}
  • Entity object
import java.util.*;

public class Student {
	public String getName() {
		return name;
	}

	public Address getAddress() {
		return address;
	}

	public String[] getBooks() {
		return books;
	}

	public List<String> getHobbies() {
		return hobbies;
	}

	public Map<String, String> getCard() {
		return card;
	}

	public Set<String> getGames() {
		return games;
	}

	public String getWife() {
		return wife;
	}

	public Properties getInfo() {
		return info;
	}
	private String name;
	private Address address;
	private String[] books;
	private List<String> hobbies;
	private Map<String,String> card;
	private Set<String> games;
	private String wife;
	private Properties info;
	public void setName(String name) {
		this.name = name;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public void setBooks(String[] books) {
		this.books = books;
	}
	public void setHobbies(List<String> hobbies) {
		this.hobbies = hobbies;
	}
	public void setCard(Map<String, String> card) {
		this.card = card;
	}
	public void setGames(Set<String> games) {
		this.games = games;
	}
	public void setWife(String wife) {
		this.wife = wife;
	}
	public void setInfo(Properties info) {
		this.info = info;
	}

	@Override
	public String toString() {
		return "Student{" +
				"name='" + name + '\'' +
				", address=" + address +
				", books=" + Arrays.toString(books) +
				", hobbies=" + hobbies +
				", card=" + card +
				", games=" + games +
				", wife='" + wife + '\'' +
				", info=" + info +
				'}';
	}
	//show method
	public void show(){
		System.out.println("name="+ name
				+ ",address="+ address.getAddress()
				+ ",books="
		);
		for (String book:books){
			System.out.print("<<"+book+">>\t");
		}
		System.out.println("\n hobby:"+ hobbies);
		System.out.println("card:"+card);
		System.out.println("games:"+games);
		System.out.println("wife:"+wife);
		System.out.println("info:"+info);
	}
}

xml configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="Address" class="com.kuang.pojo.Address">
        <property name="address" value="Shijiazhuang"></property>
    </bean>
    <bean id="Student" class="com.kuang.pojo.Student">
        <!-- The first: normal value injection -->
        <property name="name" value="Yang Chuanwei"></property>
        <!-- Second: ref injection -->
        <property name="address" ref="Address"></property>
        <!-- Third: array injection -->
        <property name="books">
            <array>
                <value><A dream of Red Mansions</value>
                <value><Journey to the West</value>
                <value><Outlaws of the marsh</value>
                <value><Romance of the Three Kingdoms</value>
            </array>
        </property>
        <!-- Fourth: List injection -->
        <property name="hobbies">
            <list>
                <value>Listen to the music</value>
                <value>watch movie</value>
                <value>Knock code</value>
                <value>Photography</value>
            </list>
        </property>

        <!-- Fifth: Map injection -->

        <property name="card">
            <map>
                <entry key="IDcard" value="1234567"></entry>
                <entry key="STcard" value="7654321"></entry>
            </map>
        </property>
        <!-- Sixth: Set injection -->
        <property name="games">
            <set>
                <value>Official racing version of go kart</value>
                <value>Glory of Kings</value>
            </set>
        </property>

        <!-- Seventh: set null value -->
        <property name="wife">
            <null></null>
        </property>

        <!--properties-->
        <property name="info">
            <props>
                <prop key="Student number">20194074</prop>
                <prop key="Gender">male</prop>
                <prop key="full name">Yang Chuanwei</prop>
                <prop key="username">reliable</prop>
                <prop key="userpass">resetpass01</prop>
            </props>
        </property>
    </bean>
</beans>

3.3. Extension mode injection - use p namespace and c namespace

use:

package com.kuang.pojo;

public class User {
	private String name;
	private int age;

	public User(String name,int age) {
		this.name = name;
		this.age=age;
	}
	public User(){};
	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 "User{" +
				"name='" + name + '\'' +
				", age=" + age +
				'}';
	}
}

Profile:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--P(attribute: properties)Namespace , Property should still be set set method-->
    <bean id="user" class="com.kuang.pojo.User" p:name="reliable" p:age="21"/>
    <!--C(structure: Constructor)Namespace , Property should still be set set method-->
    <bean id="user2" class="com.kuang.pojo.User" c:name="Mad God" c:age="18"/>
</beans>

Test:

	public void test2(){
		ApplicationContext context=new ClassPathXmlApplicationContext("beans03.xml");
		User user = context.getBean("user", User.class);
		System.out.println(user);
		User user2 = context.getBean("user2", User.class);
		System.out.println(user2);
	}

be careful

To introduce c and p namespaces:

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

4. Scope of the bean

4.1. Singleton mode (default)

<bean id="accountService" class="com.something.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

4.2 prototype mode: a new object is generated each time

<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype">

4.3. request, session, application, used in web Development

5. Automatic assembly of bean s

  • Automatic assembly is a way for Spring to meet Bean dependencies
    • Spring automatically finds and assembles bean properties in the context

There are three assembly methods in Spring

  1. Configuring in xml
  2. Configuring in Java
  3. Implicit automatic assembly [important]

5.1. Automatic assembly properties

Environment matching: a person has two pets, a cat and a dog

Idea:

The three entity classes implement getter s and setter s, and toString() methods, which are configured in bean s

People class is associated with cat and dog classes. After configuration, instantiate people class and call getDog() method to call Dog class method

<bean id="dog" class="com.kuang.pojo.Dog" p:dogname="dog"/>
    <bean id="cat" class="com.kuang.pojo.Cat" p:catname="cat"/>
    <bean id="people" class="com.kuang.pojo.People">
        <property name="cat" ref="cat"/>
        <property name="dog" ref="dog"/>
        <property name="str" value="***"/>

5.2,byName:

  • In the container context, the id corresponds to the parameter value of the object set method, and the id is globally unique
	public void setCat(Cat cat) {
		this.cat = cat;
	}
<bean id="dog" class="com.kuang.pojo.Dog" p:dogname="dog"/>
<bean id="cat" class="com.kuang.pojo.Cat" p:catname="cat"/>
<bean id="people" class="com.kuang.pojo.People" autowire="byName"></bean>

5.3,byType:

  • You must ensure that the type is globally unique (class is globally unique). This bean needs to be consistent with the type of the automatically injected attribute

5.4 summary:

Properties:

	private Cat cat;
	private Dog dog1;

byName: the name of the class attribute is consistent with the id name of the bean, and the id is globally unique
byType: the data type of the class attribute is consistent with the class name of the bean and is globally unique

6. Annotation for automatic assembly

  • @Autowired
    
    public @interface Autowired {
        boolean required() default true;
    }
    

    Autowired () can pass a parameter. If the required property of Autowired is explicitly defined as false, it indicates that this object can be NULL

    You can use it directly on attributes or set methods!

    Using Autowired, you don't need to write a set method, provided that the automatically assembled attribute exists in the IoC container and the id name is consistent with the attribute name!

    (Autowired searches by type first. If multiple types are found in the IoC container, search by name!)

    	@Autowired
    	private Cat cat;
    	@Autowired
    	private Dog dog;
    

    ***If the Autowired auto assembly environment is complex and cannot be implemented through an annotation, you can use [@ Qualifier ("")] to specify a bean to be used for the container

        <bean id="dog2" class="com.kuang.pojo.Dog"></bean>
        <bean id="dog1" class="com.kuang.pojo.Dog"></bean>
    
    	@Autowired
    	@Qualifier("dog1")
    	private Dog dog;
    

    For example, in the above situation: there are multiple objects of dog type in the bean, and the name of dog cannot be found. At this time, you can use @ Qualifier("dog1") to specify the bean with the name of dog1 in the container to inject!

    javax native @ route (), first find the type, and then find the name!

7. Using annotation development

After spring 4, to develop with annotations, you must import the AOP package

Import context constraints and add annotation support

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

7.1,bean

@Component

It is usually placed on a class, indicating that this class is managed by Spring

7.2. How to inject attributes

package com.reliable.y;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//@Component annotation this is a component
@Component
public class User {
	public String name;
	@Value("Reliable poplar")
	public void setName(String name) {
		this.name = name;
	}
}

7.3. Derived annotation (placed on class)

@Component has several derived annotations. We will layer according to the MVC 30% architecture in web development!

  • dao[@Repository]

  • service[@Service]

  • controller (servlet)[@Controller]

    The four annotation functions are the same. The code loads the corresponding classes into the container

7.4. Automatic assembly attribute (put on the attribute)

@Autowired: the name of the auto assembly type. If it cannot be assembled uniquely, you need to specify the id through @ Qualifier(value = "xxx")!

@Nullable: this field can be null!

@Resource: javax auto assembly type name!

7.5 scope

@Component
@Scope("prototype")
public class User {
	public String name;
	@Value("Reliable poplar")
	public void setName(String name) {
		this.name = name;
	}
}

7.6 summary

xml and annotations:

  • xml is widely applicable and easy to maintain
  • Annotations are not their own classes and cannot be used. Maintenance is relatively complex

Best practices for xml and annotations:

  • xml managed beans, that is, there is only one bean under the xml file

  • Annotation is responsible for value injection @ Value("xxx")

  • Note effective:

        <!--Specify the package to be scanned, and the comments below the package will take effect-->
        <context:component-scan base-package="com.reliable"></context:component-scan>
        <context:annotation-config/>
    

8. Configure Spring in Java

8.1 entity type:

package com.reliable.y;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//@Component indicates that this is taken over by Spring and registered in the container
@Component
public class User {
	private String name;
	@Override
	public String toString() {
		return "com.reliable.y.User{" +
				"name='" + name + '\'' +
				'}';
	}

	public String getName() {
		return name;
	}
	@Value("Reliable poplar")
	public void setName(String name) {
		this.name = name;
	}
}

8.2. Profile class:

package com.reliable.config;
import com.reliable.y.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
//YConfig will also be hosted by Spring
@Configuration
//@Configuration means that this is a configuration class, which is the same as beans.xml
@ComponentScan("com.reliable")
public class YConfig {
	/*
	1.A bean is registered here, which is equivalent to a set of bean tags
	2.The name of the method is equivalent to the id in the previous bean tag
	3.The return value of the method is equivalent to the class in the bean tag
	 */
	@Bean
	public User getUser(){
		return new User();  //The object to inject into the bean
	}
}

8.3 testing:

import com.reliable.config.YConfig;
import com.reliable.y.User;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test06 {
	public static void main(String[] args) {
		//If you completely use the configuration class method, you need to use AnnotationConfigApplicationContext to get the object load
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(YConfig.class);
		User user = (User) context.getBean("getUser");
		System.out.println(user.toString());
	}
}

Three sentence summary IoC:

  1. All classes should be placed in the bean
  2. Each bean is an instantiated object
  3. All bean s (objects) should be fetched through the container (ApplicationContext)

9. Agent mode

Proxy mode is the bottom layer of spring AOP! [spring AOP and spring MVC]

9.1. Classification of agency mode:

  • Static proxy
  • Dynamic agent

9.2 role analysis

‚Äč For example: the landlord, intermediary, customer, and rental

  • Abstract role: it is generally implemented using interfaces or abstract classes (renting a house is an abstract role class or interface)
package com.reliable.y;
//Rent a house
public interface Rent {
	public void rent();
}

  • Real role: the role of the agent (landlord)
package com.reliable.y;


public class Host implements Rent {
	public void rent() {
		System.out.println("The landlord wants to rent the house!");
	}
}

  • Agent role: an agent is really a role (intermediary) that handles something
package com.reliable.y;

public class Proxy implements Rent {
	private Host host;

	public Proxy(Host host) {
		this.host = host;
	}
	public Proxy(){}

	public void rent() {
		host.rent();
	}
	//House viewing
	public void seeHouse(){
		System.out.println("Intermediary show customers the house!");
	}
	//Intermediary fee
	public void fare(){
		System.out.println("Charge intermediary fee!");
	}
	//sign a contract
	public void makeht(){
		System.out.println("sign a contract!");
	}
}

  • Customer: role of access agent
package com.reliable.y;

public class Client {
	public static void main(String[] args) {
		Host host = new Host();
		//The agent role usually has more operations than the real role
		Proxy proxy = new Proxy(host);
		proxy.rent();
		proxy.seeHouse();
		proxy.fare();
		proxy.makeht();
	}
}

9.3 benefits of static proxy mode:

  • It can make the operation of real roles more pure without paying attention to some public businesses
  • The public business is handed over to the agent role to realize the division of business
  • When the public business is expanded, it is convenient for centralized management

Disadvantages of static proxy:

  • A real role needs to produce an agent role. The amount of code is large and the development efficiency becomes low

9.4 dynamic agent

  • Same as static proxy role
  • Dynamic proxy class dynamic generation
  • It is divided into two categories: interface based dynamic agent and class based dynamic agent
    • Interface based - JDK
    • Based on class --- cglib
    • Java bytecode --- javasist
Rent interface
package com.reliable.demo1;
//Abstract role: rent a house
public interface Rent {
	public void rent();
}
Real role: Host
package com.reliable.demo1;
//Real role: landlord, the landlord wants to rent the house
public class Host implements Rent{
	public void rent() {
		System.out.println("House rental");
	}
}
Get proxy class dynamically:
package com.reliable.demo1;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyInvocationHandler implements InvocationHandler {
	private Rent rent;
	public void setRent(Rent rent) {
		this.rent = rent;
	}
	//Generate an agent class, focusing on the second parameter to obtain the abstract role to be represented! It used to be a role, but now it can represent a kind of role
	public Object getProxy(){
		return Proxy.newProxyInstance(this.getClass().getClassLoader(),
				rent.getClass().getInterfaces(),this);
	}
	// Proxy: proxy class method: the method object of the calling handler of the proxy class
	// Process method calls on proxy instances and return results
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		seeHouse();
		//Core: essence is realized by reflection!
		Object result = method.invoke(rent, args);
		fare();
		return result;
	}
	//House viewing
	public void seeHouse(){
		System.out.println("Show the tenant");
	}
	//Intermediary fee
	public void fare(){
		System.out.println("Intermediary fee");
	}
}
Test call class:
package com.reliable.demo1;
//tenant
public class Client {
	public static void main(String[] args) {
		//Real role
		Host host = new Host();
		//Call handler for proxy instance
		ProxyInvocationHandler pih = new ProxyInvocationHandler();
		pih.setRent(host); //Put the real character in!
		Rent proxy = (Rent)pih.getProxy(); //Dynamically generate the corresponding proxy class!
		proxy.rent();
	}
}

Tags: SSM

Posted on Sun, 31 Oct 2021 11:01:27 -0400 by YourNameHere