Spring's IoC (Inversion of Control) 4-Bean automatic assembly

In Part I use the @Autowiredto simplify development, but in fact can also simplify:
In Part I, the xmldesignated class configuration, as follows:

<bean id="address" class="com.weizu.pojo.UserAddress" p:address="西安"/>
<bean id="weizu" class="com.weizu.pojo.User" p:name="张三">
    <property name="age" value="21"></property>
</bean>

1. @Component

At this time, no need, we directly use componentto specify the address of annotation scanning, this time is xmlas follows:

<?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:component-scan base-package="com.weizu.pojo"/>
    <context:annotation-config/>
</beans>

Then, specify in User.javaandUserAddress.java@Component

package com.weizu.pojo;

import org.springframework.stereotype.Component;

/**
 * @Component等价于
 * <bean id="address" class="com.weizu.pojo.UserAddress" p:address="西安"/>
 */
@Component
public class UserAddress {
    
    
    public String address = "西安";

    @Override
    public String toString() {
    
    
        return "UserAddress{" +
                "address='" + address + '\'' +
                '}';
    }
}
package com.weizu.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class User {
    
    
    public int age = 12;
    public String name = "张三";
    @Autowired
    private UserAddress address;

    @Override
    public String toString() {
    
    
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", address=" + address +
                '}';
    }
}

Then test:

public class myTest {
    
    

    @Test
    public void Test(){
    
    
        // create and configure beans
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        // 此时在beans.xml中没有指定id,由于@Component是其等价写法,默认的id也就是类的名字的小写
        User userService = (User) context.getBean("user");
        System.out.println(userService.toString());
    }
}

Since it beans.xmlis not specified idin and @Componentis equivalent, the default idis the lowercase of the name of the class, so it is written here user.
Insert picture description here
Use @Componentplaced on the class, indicating that this class is Springmanaged, that is bean.

2. @Value

Use @Valuecan specify the value of the attribute, can be placed in a defined position, can also be placed on the setmethod. For the Userclass, as follows:

@Component
public class User {
    
    
    @Value("14")
    private int age;
    @Value("张三")
    private String name;
    @Autowired
    private UserAddress address;

    @Override
    public String toString() {
    
    
        return "User{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", address=" + address +
                '}';
    }
}

The effect remains the same:
Insert picture description here
among them @Value("14"), it is equivalent to<property name="age" value="14"></property>

3. Derivative annotations

Insert picture description here


Reference video: https://www.bilibili.com/video/BV1WE411d7Dv?p=14

Guess you like

Origin blog.csdn.net/qq_26460841/article/details/115052484