Spring's IoC (Inversion of Control) 4-XML injection practice

In the previous article, I learned how to inject simple values. Since there are many types that can be injected, let's try it out here.
For example, define two classes, User.javaand UserAddress.java: define the following attributes
in the User.javaclass:

private int age;
private String name;
private String[] hobbeys;
private List<String> cards;
private Map<String, String> girlfirends;
private Set<String> houses;
private UserAddress address;

Then generate the corresponding setsum getmethod. In UserAddress.javathe definition:

private String address;

Similar production set, getand toStringmethods can be.
Then, start writing the corresponding beans.xmldocuments, may wish to refer to the official document: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-collection-elements
for writing convenience, you can beans.xmlRight-click on the top of the file Split Vertically, and the effect is as follows:
Insert picture description here
more convenient. Then you can start.

<?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="weizu" class="com.weizu.pojo.User">
        <property name="age" value="23"></property>
        <property name="name" value="张三"></property>
        <property name="hobbeys">
            <array>
                <value>抽烟</value>
                <value>喝酒</value>
            </array>
        </property>
        <property name="cards">
            <list>
                <value>123131231312</value>
                <value>123131231312</value>
            </list>
        </property>
        <property name="girlfirends">
            <map>
                <entry key="one" value="对象1"/>
                <entry key="two" value="对象2"/>
            </map>
        </property>
        <property name="houses">
            <set>
                <value>北京</value>
                <value>上海</value>
            </set>
        </property>
        <property name="address">
            <null/>
        </property>
    </bean>
</beans>

test:

import com.weizu.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class myTest {
    
    

    @Test
    public void Test(){
    
    
        // create and configure beans
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User userService = (User) context.getBean("weizu");
        System.out.println(userService.toString());
    }
}

result:
Insert picture description here


Reference video: https://www.bilibili.com/video/BV1WE411d7Dv?p=9&spm_id_from=pageDriver
Reference document: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html #beans-collection-elements

When there are multiple different beans-xxx.xmlfiles, you can define a total XML, and then use the importimport in it, such as:

<beans>
    <import resource="services.xml"/>
    <import resource="resources/messageSource.xml"/>
    <import resource="/resources/themeSource.xml"/>

    <bean id="bean1" class="..."/>
    <bean id="bean2" class="..."/>
</beans>

services.xmlMust be in a position classpath location, while the two rear position must be resourcesunder, these paths are relative to the path. Of course, this path can be specified as an absolute path, such as: file:C:/config/services.xmlor classpath:/config/services.xml.
In Spring IoCthe vessel, you can host multiple bean, at configuration time, some of the data may be configured as follows:

  • classQualification, the designated beancategory;
  • nameThe specified beaninstantiation name;
  • scopeSpecified beanrange of action, there are optional singleton, , prototype, request, session, application, websocketby default singleton, each represents a Spring IoCsingle container, the presence of beanthe object instance; prototyperepresents a beanpossible promising multiple instances; requestrepresents a single HTTPsingle request instance; sessionSimilarly also to a HTTPof SessionSingle instance; applicationmeans ServletContextsingle instance websocketin WebSocketmedium ; means single instance in medium;

The specific designation method is as follows:

<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

Guess you like

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