Preliminary exploration of Spring

Keywords: Java Spring

  1. Format update of beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""


2. bean creation in beans.xml

The parameterless constructor is recognized by default when creating bean s, so we need to attach corresponding attributes to enable the editor to recognize other constructor methods.

We mainly use three methods to create bean s. (example below)

<!--use Spring create object-->
<!--    Parameterless construction is used by default-->
    <bean id="hello" class="com.huang.pojo.hello">
<!--        value Is the property value for which the object was created-->
        <property name="str" value="Spring111"/>
    <!--        <property name="str" ref=""/> ref quote Spring Created objects in-->
    <bean class="com.huang.pojo.User" id="UserData">
<!--        <constructor-arg type="java.lang.String" value="Huang feiwen"/> Using type recognition constructors-->
<!--        Through the corresponding index Serial number to identify the corresponding constructor-->
        <constructor-arg index="0" value="Huang feiwen"/>
<!--    Using parameters name To identify the corresponding constructor-->
    <bean id="UserDao" class="com.huang.pojo.User">
        <constructor-arg name="name" value="Huang feiwu"/>

3. Dependency injection

    Dependency injection is divided into constructor injection, set injection and expansion injection.

    Constructor injection: create different classes through different constructors to generate injection.

    set injection: injection is generated by assigning values to the attributes in the created class.

    Extension injection: p naming and c naming to generate injection. (p naming and c naming cannot be used directly, and xml constraints need to be imported)

c named import code is as follows:


p name the import code as follows:


    4. Automatic assembly of bean


All bean IDS need to be unique, and the bean needs to be consistent with the value of the set method of the automatically injected attribute. (automatic assembly by corresponding name)


The class of all beans needs to be unique, and the bean needs to be consistent with the type value of the automatically injected attribute. (the corresponding object type must be unique, otherwise an error will occur)

5. Development using annotations

Injection is applicable to simple configuration, complex configuration or configuration file for later maintenance.

To use notes:

1. Import constraints.

2. Configuration annotation support

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""


@Autowire: automatic assembly, which can be understood as autowire:byType.
@Scope: configuration mode.
@Autowired(required = false) indicates that this object can be empty, otherwise it cannot be empty.
@Nullable: used to store the parameters of the method, indicating that the parameters can be null.
@Qualifier(value = ""): it can be understood as autowire: byname, and value means Name.
@Component: the component is placed on the class, which indicates that the class is managed by Spring, which is equivalent to: < bean id = "XXXX" class = "XXX" / >, and the default class name is the lowercase of the class name.
@value is used to assign values to attributes in the class.

The corresponding examples are as follows:
public class User {
//    Equivalent to < property name = "name" value = "Zhang San" >
    @Value("Zhang San")
    public String name;

@Component has some derived annotations. In our web development, it will be layered with MVC three-tier architecture.

  1. Dao[@Repository]
  2. Service[@Service]
  3. Controller[@Controller]

The functions of these four annotations are the same. They all represent a class registered in Spring and assembled bean s.

If the @ AutoWired auto assembly environment is complex and the auto assembly cannot be completed through an annotation [@ AutoWired], we can use @ Qualifier(value = "XXX") to match the use of @ AutoWired and specify a unique bean object injection!

Examples are as follows:

    private Cat cat;
    @Qualifier(value = "dog222")
    private Dog dog;

@Resource can also realize automatic assembly. The assembly process is: first view the corresponding name, and then view the class. If the two attributes do not match, an error will be reported.

(emphasis) the difference between @ Resource and @ AutoWired:

1. They are all assembled automatically and can be placed in the attribute field.

2.@AutoWired is implemented by byType, and this object must exist.

3.@Resource is implemented by byName by default. If the name cannot be found, it is implemented by byType! If neither can be found, an error will be reported.

6. Configure Spring using java

The classes used are:

AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext("class");


//The annotation here means that this class has been taken over by Spring and registered in the container
//Equivalent to: < bean id = "user" class = "user" > < property name = "name" value = "Zhang San" > < / bean >
@Configuration//This will also be taken over by Spring, because @ Configuration is originally @ Component,@Configuration indicates that this is a Configuration class, and its function is the same as that of bean.xml
public class User {
    private String name;

    public String getName() {
        return name;

    @Value("Zhang San")
    public void setName(String name) { = name;

    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
//Register a bean, which is equivalent to the bean tag
//The name of this method is equivalent to the id in the bean
//The return value of this method is equivalent to the class in the bean
    public User getUser(){
        return new User();//Returns the object to be injected into the bean container

So this configuration class can do the same thing as in bean.xml.

@ComponentScan: used to scan classes.

Posted by seavolvox on Sun, 10 Oct 2021 02:34:29 -0700