SPR3: Spring configuration file

3.1 alias label

3.1.1 introduction

Similar to aliasing entity classes in MyBatis, we can alias beans in Spring. However, it doesn't play a big role in practical use, because the bean tag provides the name attribute for aliasing and is more advanced.

3.1.2 examples

Spring 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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="hello" class="com.yun.pojo.Hello">
        <property name="str" value="How do you do, Spring"/>
    </bean>
    <bean id="hello2" class="com.yun.pojo.world">
       <constructor-arg name="str" value="This is name mode"/>
        <constructor-arg name="test" value="This too"/>
    </bean>
    
    <alias name="hello2" alias="world"/>

</beans>


Test call:

package com.yun;

import com.yun.pojo.Hello;
import com.yun.pojo.world;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

        world w = (world) applicationContext.getBean("world");
        System.out.println(w);
    }
}

Summary: alias the Spring configuration file. The alias can then be used when using the getBean() statement.

3.2 bean Tags

3.2.1 introduction

The bean tag is used to register the entity class with Spring and put the container to call later. The main attributes used are id, name and class. id is the name, name is the alias, and class is the class address.

3.2.2 examples

Spring 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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="hello" name="word world1,world2;world3" class="com.yun.pojo.Hello">
    </bean>
</beans>

Tip: name can have multiple aliases, which can be separated by spaces, commas, semicolons, etc.

Test interface:

package com.yun;

import com.yun.pojo.Hello;
import com.yun.pojo.world;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

        Hello hello = (Hello) applicationContext.getBean("world3");
        System.out.println(hello);
    }
}

Summary: it is the same as the alias tag, but more advanced.

3.3 import label

3.3.1 introduction

The import tag is mainly used to import other xml files in the Spring configuration file. When a project is under team development, there may be multiple Spring configuration files. At this time, you can use the import tag in an xml to import other configuration files. And Spring will automatically select the bean s. The official name of the Spring configuration file is applicationContext.xml.

3.3.2 examples

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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="hello" class="com.yun.pojo.Hello">
        <constructor-arg name="str" value="Inner layer bean"/>
    </bean>
</beans>

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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="hello" class="com.yun.pojo.Hello">
        <constructor-arg name="str" value="Outer layer bean"/>
    </bean>

    <import resource="beans.xml"/>
</beans>

Test interface:

package com.yun;

import com.yun.pojo.Hello;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        Hello hello = (Hello) applicationContext.getBean("hello");
        System.out.println(hello);
    }
}

Summary: there are two configuration files in this case. Finally, import one file into another file. Moreover, the bean names in the two configuration files are consistent with the entity classes. The only difference is that when passing parameters to the constructor, the values passed are different. When using the import statement, resulting in the existence of two beans with the same name, Spring will automatically select the latest one, that is, the lowest one in the configuration file.

At the end, the bean tag attributes are not just these

Tags: Java Spring

Posted on Wed, 08 Sep 2021 05:42:13 -0400 by kidintraffic