Spring (2)-dependency injection DI, Bean scope

1 Dependency Injection (DI)

Dependency Injection (DI)
dependency: refers to the creation of the Bean object depends on the container
injection: refers to all the attributes in the Bean object, injected by the container

1 Constructor injection

2Set injection

1 Environment to build
Address.java

package com.zs.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Address {
    
    
    private String address;
}

Student.java

package com.zs.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
    
    
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;
}

beans.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">
    <bean id="student" class="com.zs.pojo.Student">
<!--        常量注入-->
        <property name="name" value="zs"/>
    </bean>

</beans>

test

package com.zs.test;

import com.zs.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    
    @org.junit.Test
    public void test() {
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student);
    }
}

Test output
Insert picture description here

<?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.zs.pojo.Address"/>
    <bean id="student" class="com.zs.pojo.Student">
<!--        常量注入-->
        <property name="name" value="zs"/>
        <property name="address" ref="address"/>
        <property name="books">
            <array>
                <value>西游记</value>
                <value>红楼梦</value>
                <value>水浒传</value>
            </array>
        </property>

        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>看电影</value>
                <value>爬山</value>
            </list>
        </property>

        <property name="card">
            <map>
                <entry key="中国邮政" value="456456456465456"/>
                <entry key="建设" value="1456682255511"/>
            </map>
        </property>

        <property name="games">
            <set>
                <value>LOL</value>
                <value>BOB</value>
                <value>COC</value>
            </set>
        </property>

        <property name="wife">
            <null/>
        </property>

        <property name="info">
            <props>
                <prop key="学号">20190604</prop>
                <prop key="性别"></prop>
                <prop key="姓名">小明</prop>
            </props>
        </property>
    </bean>
</beans>
Student(name=zs, address=Address(address=null), books=[西游记, 红楼梦, 水浒传], hobbys=[听歌, 看电影, 爬山], card={
    
    中国邮政=456456456465456, 建设=1456682255511}, games=[LOL, BOB, COC], wife=null, info={
    
    学号=20190604, 性别=, 姓名=小明})

3p naming and c naming injection

User.java

package com.zs.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    
    
    private String name;
    private int age;
}

P namespace injection: Need to add constraint file to header file
Import constraint:xmlns:p="http://www.springframework.org/schema/p"

beans.xml

 <!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.zs.pojo.User" p:name="zs" p:age="24"/>

c Namespace injection: Need to add constraint files in the header file

xmlns:c="http://www.springframework.org/schema/c"
    <!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
    <bean id="user" class="com.zs.pojo.User" c:name="zc" c:age="25"/>

Scope of 4Bean

1 Singleton
When the scope of a bean is Singleton, there will only be one shared bean instance in the Spring IoC container, and all requests to the bean, as long as the id matches the bean definition, will only return the same instance of the bean. Singleton is a singleton type, that is, when the container is created, a bean object is automatically created at the same time. No matter whether you use it or not, it exists, and the object obtained every time is the same object. Note that Singleton scope is the default scope in Spring . To define a bean as a singleton in XML, you can configure it like this:

 <bean id="ServiceImpl" class="cn.zs.service.ServiceImpl" scope="singleton">

2 Prototype
When the scope of a bean is Prototype, it means that a bean definition corresponds to multiple object instances . Prototype is a prototype type. It is not instantiated when we create the container. Instead, an object is created when we get the bean, and the object we get is not the same object every time . According to experience, prototype scope should be used for stateful beans , and singleton scope should be used for stateless beans . Define the bean as a prototype in XML, which can be configured like this:

 <bean id="account" class="com.foo.DefaultAccount" scope="prototype"/>  
  或者
 <bean id="account" class="com.foo.DefaultAccount" singleton="false"/>

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/112919641