SpringIOC of Day21SSM

Introduction to Spring

  • (1) What is Spring?
    Spring is a hierarchical Java SE/EE application full-stack lightweight open source framework
    " full-stackService Dao web
    " 轻量级add modules on demand
    " 开源source code can be obtained
    with IoC- (Inverse Of Control: inverse control) and AOP- (Aspect Oriented Programming: aspect-oriented programming)
    is the core
  • (2) What are the characteristics?
    Provides the presentation layer SpringMVC
    persistence layer Spring JDBC
    can also integrate many well-known third-party frameworks and libraries in the open source world.
    Business layer transaction management AOP
    facilitates decoupling and simplifies the development of IOC
    Java source code is a classic learning example
    逐渐成为使用最多的 Java EE 企业应用开源框架

Spring architecture system

  • (1) Test: Used for testing
  • (2) Core container:核心容器is used to install Java Bean objects
  • (3)AOP:切面编程
  • (4) Aspects: Provides integration with AspectJ
  • (5) Data access: data access. Used to access and operate our database. Support the operation of the persistence layer. jdbcTemplate mybatis
  • (6) Web: Used to support the data display layer and http request
  • (7) Transactions: Used to support transaction processing. Used to solve the transaction processing problem of the business layer. Programmatic transaction management and declarative transaction management.
    Insert picture description here

Spring's IOC theory***

  • (1) What is IOC
    控制反转- (Inversion of Control,缩写为IoC)
    "The original new object is converted into this way, spring creates objects through reflection
    " The objects created by spring are placed in a container, and whoever needs it is injected into it-(get the object and Assign to reference)
    简单说:把创建对象和管理对象的权利交给spring
    Insert picture description here

Introduction to Spring's IOC-Environment Setup

  • (1) Create Project maven
  • (2) Create the module module maven
  • (3) Configuration dependency
<!--spring依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.9.RELEASE</version>
        </dependency>

Introduction to Spring IOC-Code Writing

  • (1) Define the Person class
  • (2) Manually complete the creation and assignment
  • (3) Create and assign by spring
    "Create container object
    " Read the configuration file
    new ClassPathXmlApplicationContext("applicationContext.xml");
    "Find getBean() from the container

Test01SpringIoc

public class Test01SpringIoc {
    
    
    @Test
    public void test01(){
    
    
        //1:创建ioc 容器对象  暂时看成map
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        //2:给定配置文件的名称 applicationContext.xml
        //3:调用容器的getBean方法获取id对应的对象
        Person person = (Person) context.getBean("person");
        System.out.println(person);
    }
}

applicationContext.xml

<?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">

<!--    要让 spring容器给我创建一个person对象-->
<!--    配置类名,用于反向创建对象-->
<!--    同时给一个编号方便查找-->
    <bean id="person" class="com.wzx.domain.Person" />
</beans>

Introduction to Spring's IOC-Question Answer

  • (1) Method difference
context.getBean("id值", 类型.class);//无需转型
context.getBean("id值");//需转型
  • (2) The attribute
    id of the bean tag: the identification ID of the bean tag. In theory, you can write
    class casually : which class of object you want to create for you on Spring, you need to write the full path name of the class

There are two ways of assignment


 		 Person person1 =  new Person();
        //设置方法
        person1.setId(1);
        System.out.println(person1);
        //构造方法赋值
        Person person2 =  new Person(1,"jack",20,new Date());
        System.out.println(person2);

name: the name of the member variable
value: the value of the member variable
A property tag will finally call a corresponding set method

 <bean id="person2" class="com.wzx.domain.Person" >
        <property name="id" value="10"/>
        <property name="name" value="rose"/>
        <property name="age" value="20"/>
    </bean>

Create an object through the constructor

<!--    Person person2 =  new Person(1,"jack",20,new Date());-->
<!--    System.out.println(person2);-->
    <bean id="date1" class="java.util.Date"/>
    <bean id="person3" class="com.wzx.domain.Person" >
            <constructor-arg name="id" value="10"/>
            <constructor-arg name="name" value="hello"/>
            <constructor-arg name="age" value="20"/>
            <constructor-arg name="birthday" ref="date1"/>
    </bean>
  • Configure the parameters of the constructor
  • If there are four constructor-arg, it means a four-parameter construction method.
  • Value can be assigned to basic type data and String, but for other objects, use ref
  • Indicates to find an existing object in the current container

Introduction to Spring's IOC-Static Factory Object Creation

  • (1) What is a static factory
    XxxFactory.get();
    (2) Obtain bean objects by calling static methods
public class PersonFactory {
    
    
    public static Person getBean() {
    
    
        return new Person();//静态方法返回创建的对象
    }
}

(3) factory-method
specifies the method of obtaining the static factory of the object

<!--    Person person1 = PersonFactory.getBean();-->
    <bean class="com.wzx.demo02.PersonFactory" factory-method="getBean" id="person4"/>

Instance factory

  • (1) What is an instance factory
    XxxFactory
  • (2) Obtain the bean object by calling the member method of the
    factory object XxxFactory factory = new XxxFactory(); //Instantiate the factory object
    factory .yyy() //Get the object
  • (3) factory-bean creates factory objects
  • (4) Factory-method call method to obtain bean object
<!--    PersonFactory2 factory2 = new PersonFactory2(); 创建工厂-->
<!--    Person person1 = factory2.getBean();调用工厂的方法-->
    <bean class="com.wzx.demo03.PersonFactory2" id="factory2"/>
    <bean factory-bean="factory2" factory-method="getBean" id="person5"/>

Introduction to Spring's IOC-singleton and multiple instances

》What is a singleton?
There is only one object in memory 每次获取到该对象的地址值一样.
"What is multi-instance?
In memory 每个对象都是一个新的对象,他们的地址值都不同.

  • (1) Question: Every time we get an object, does Spring create a new object or always return the same object to us?
  • (2) Answer: The objects created by spring are singletons by default. (The objects returned are the same each time)
        scope="singleton" 单例(默认值)
        scope="prototype" 多例
        scope="request" 创建的对象放到request域中
        scope="session" 创建对象放到session对象

Multi-instance

    <bean id="person" class="com.wzx.domain.Person" scope="prototype"/>

Single instance

    <bean id="person" class="com.wzx.domain.Person" scope="singleton"/>

Spring life cycle (understand)

  • (1) Life cycle
    creation method init
    destruction method destruction
    common method service
  • (2) Attribute
    init-method This method will automatically execute
    destroy-method when the object is initialized. This method will be automatically called when the object is about to be destroyed
    (3) Test
    context.close()关闭容器
public class Person{
    
    
   public void init(){
    
    
        System.out.println("哇哇...");
    }
    public void eat(){
    
    
        System.out.println("吃食堂...");
    }
    public void destory(){
    
    
        System.out.println("呜呜...");
    }
}
   <bean id="person6" class="com.wzx.domain.Person"
          init-method="init"
          destroy-method="destory"
    />

Spring dependency injection DI-set method

(1) What is dependency injection
DI (dependency injection) 依赖注入
含义:就是给对象的属性设置值. It
is to set the value of the property of the object with the
set method to set the value of the object. The
constructor sets the value when the object is initialized.
(2)
Set the property in the set method of the property tag (master)
Let spring adjust the set method, premise There must be a set method in the conditional class

name: represents the set method to remove the set, the first letter is lowercase setName Name name
value: the value of the basic type or string type, specifically for the attribute setting
ref (reference): the id of the reference object, injected as an object type
Insert picture description here

Spring dependency injection-injection of complex types

(1) What is a complex type?
The simple is the basic type and string
Aarry Array List collection Map collection Set collection Properties collection
(2) How to set values ​​for these properties
Use the corresponding subtags
array, list, map, set, props

  //集合类型属于复杂类型
    private String[] arr; //女朋友们
    public void setArr(String[] arr) {
        this.arr = arr;
    }

    public String[] getArr() {
        return arr;
    }

    //List
    private List<String> list;//前女朋友们
    public void setList(List<String> list) {
        this.list = list;
    }

    //set
    private Set<String> set;//前女朋友们
    public void setSet(Set<String> set) {
        this.set = set;
    }

    //map
    private Map<String,String> map;//前女朋友们
    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    //properties
    private Properties properties;//前女朋友们
    public void setProperties(Properties properties) {
        this.properties = properties;
    }

  <bean id="person7"
          class="com.wzx.domain.Person">
        <property name="name" value="jack"/>
        <property name="arr">
            <array>
                <value >rose</value>
                <value >rose</value>
                <value >rose</value>
            </array>
        </property>
        <property name="list">
            <list>
                <value >rose1</value>
                <value >rose2</value>
                <value >rose3</value>
            </list>
        </property>
        <property name="set">
            <set>
                <value >rose</value>
                <value >rose</value>
                <value >rose3</value>
            </set>
        </property>
        <property name="map">
            <map>
               <entry key="10010" value="rose1"/>
               <entry key="10086" value="rose2"/>
               <entry key="110" value="rose3"/>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="10010">rose1</prop>
                <prop key="10086">rose2</prop>
                <prop key="110">rose3</prop>
            </props>
        </property>
    </bean>

Guess you like

Origin blog.csdn.net/u013621398/article/details/108962467