spring —— IoC 容器(一)

IoC 不是一种技术,而是一种设计思想,旨在降低代码之间的耦合性。Spring 通过 IoC 容器管理所有 Java 对象的实例化和初始化,控制对象与对象之间的依赖关系。

一、基于 XML 管理 bean

(一)通过 XML 获取 bean

public class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public User(){
       
    }

    public void setName(String name) {        
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
public class TestUser {
    @Test
    public void userTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        User user = (User) context.getBean("user");
        System.out.println(user);
    }
}
<?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="user" class="com.fourth.user.User">       
    </bean>
</beans>

(二)使用 property 注入属性 

<?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="user" class="com.fourth.user.User">
        <property name="name" value="tom"></property>
        <property name="age" value="20"></property>
    </bean>
</beans>

 此时在 TestUser 中获取bean,其实是通过无参构造和 set 方法注入。

(三)使用 constructor-arg 注入属性

<?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="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
    </bean>
</beans>

 此时在 TestUser 中获取bean,其实是通过有参构造进行注入。

(四)注入对象类型属性

1、外部 bean 注入

package com.fourth.user;

public class Person {
    private String pid;

    public Person(){}

    public void setPid(String pid) {
        this.pid = pid;
    }

    public Person(String pid) {
        this.pid = pid;
    }
}
package com.fourth.user;

public class User {
    private String name;
    private int age;
    private Person person;

    public User(String name, int age, Person person) {
        this.name = name;
        this.age = age;
        this.person = person;
    }

    public void setName(String name) {
       this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setPerson(Person person) {
        this.person = person;
    }
}
<?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="person" class="com.fourth.user.Person">
        <property name="pid" value="10010"></property>
    </bean>

    <bean id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="person" ref="person"></constructor-arg>
    </bean>
</beans>

外部 bean 注入对象类型属性,通过 ref 进行引入。

2、内部 bean 注入 

<?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="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="person">
            <bean id="person" class="com.fourth.user.Person">
                <property name="pid" value="10010"></property>
            </bean>
        </constructor-arg>
    </bean>
</beans>

 3、级联属性注入

<?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="person" class="com.fourth.user.Person">
        <constructor-arg name="pid" value="tom"></constructor-arg>
    </bean>

    <bean id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="person" ref="person"></constructor-arg>
        <property name="person.pid" value="jerry"></property>
    </bean>
</beans>

或者: 

<?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="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="person" >
            <bean id="person" class="com.fourth.user.Person">
                <constructor-arg name="pid" value="tom"></constructor-arg>
            </bean>
        </constructor-arg>
        <property name="person.pid" value="jerry"></property>
    </bean>
</beans>

  级联属性注入,首先通过外部 bean 或者内部 bean 进行引入,然后通过 value 更改对象内部的属性。

(五)注入数组类型属性

package com.fourth.user;

public class User {
    private String name;
    private int age;
    private String[] hobby;

    public User(String name, int age, String[] hobby) {
        this.name = name;
        this.age = age;
        this.hobby = hobby;
    }

    public User() {
        
    }

    public void setName(String name) {
       this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public String[] getHobby() {
        return hobby;
    }
}
package com.fourth.test;

import com.fourth.user.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;

public class TestUser {
    @Test
    public void userTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        User user = (User) context.getBean("user");
        System.out.println(Arrays.toString(user.getHobby()));
    }
}
<?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="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="hobby" >
            <array>
                <value>eating</value>
                <value>sleeping</value>
                <value>playing</value>
            </array>
        </constructor-arg>
    </bean>
</beans>

 本案例中,hobby 是一个 String 类型数组,对其进行属性注入通过 array 标签进行。

(六)注入 list 类型属性

package com.fourth.user;

import java.util.List;

public class User {
    private String name;
    private int age;
    private List<Person> personList;

    public User(String name, int age, List<Person> personList) {
        this.name = name;
        this.age = age;
        this.personList = personList;
    }

    public User() {
        
    }

    public void setName(String name) {
       this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }

    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public List<Person> getPersonList() {
        return personList;
    }
}
package com.fourth.user;

public class Person {

    private String pid;

    public Person(){}

    public void setPid(String pid) {
        this.pid = pid;
    }

    public Person(String pid) {

        this.pid = pid;
    }

    public String getPid() {
        return pid;
    }
}
package com.fourth.test;

import com.fourth.user.Person;
import com.fourth.user.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUser {
    @Test
    public void userTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        User user = (User) context.getBean("user");
        for(Person person:user.getPersonList()){
            System.out.println(person.getPid());
        }
    }
}
<?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="person1" class="com.fourth.user.Person">
        <property name="pid" value="10011"></property>
    </bean>
    <bean id="person2" class="com.fourth.user.Person">
        <property name="pid" value="10086"></property>
    </bean>

    <bean id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="personList" >
            <list>
                <ref bean="person1"></ref>
                <ref bean="person2"></ref>
            </list>
        </constructor-arg>
    </bean>
</beans>

(七)注入 map 类型属性 

package com.fourth.user;

import java.util.Map;

public class User {
    private String name;
    private int age;
    private Map<String,Person> personMap;

    public User(String name, int age, Map<String,Person> personMap) {
        this.name = name;
        this.age = age;
        this.personMap = personMap;
    }

    public User() {
        
    }

    public void setName(String name) {
       this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setPersonMap(Map<String,Person> personMap) {
        this.personMap = personMap;
    }

    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public Map<String,Person> getPersonMap() {
        return personMap;
    }
}
package com.fourth.user;

public class Person {

    private String pid;

    public Person(){}

    public void setPid(String pid) {
        this.pid = pid;
    }

    public Person(String pid) {

        this.pid = pid;
    }

    public String getPid() {
        return pid;
    }
}
package com.fourth.test;

import com.fourth.user.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUser {
    @Test
    public void userTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        User user = (User) context.getBean("user");
        for(String key:user.getPersonMap().keySet()){
            System.out.println(user.getPersonMap().get(key).getPid());
        }
    }
}
<?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="person1" class="com.fourth.user.Person">
        <property name="pid" value="10011"></property>
    </bean>
    <bean id="person2" class="com.fourth.user.Person">
        <property name="pid" value="10086"></property>
    </bean>

    <bean id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="personMap" >
            <map>
                <entry>
                    <key>
                        <value>first</value>
                    </key>
                    <ref bean="person1"></ref>
                </entry>
                <entry>
                    <key>
                        <value>second</value>
                    </key>
                    <ref bean="person2"></ref>
                </entry>
            </map>
        </constructor-arg>
    </bean>
</beans>

(八)util 形式注入属性:对数组、list、map 的补充(1) 

以 map 类型为例:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="person1" class="com.fourth.user.Person">
        <property name="pid" value="10011"></property>
    </bean>
    <bean id="person2" class="com.fourth.user.Person">
        <property name="pid" value="10086"></property>
    </bean>

    <bean id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="personMap" ref="map"></constructor-arg>
    </bean>

    <util:map id="map">
        <entry>
            <key>
                <value>first</value>
            </key>
            <ref bean="person1"></ref>
        </entry>
        <entry>
            <key>
                <value>second</value>
            </key>
            <ref bean="person2"></ref>
        </entry>
    </util:map>
    
</beans>

 (九)p 命名空间注入属性:对数组、list、map 的补充(2)

 同样以 map 类型为例:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="person1" class="com.fourth.user.Person">
        <property name="pid" value="10011"></property>
    </bean>
    <bean id="person2" class="com.fourth.user.Person">
        <property name="pid" value="10086"></property>
    </bean>

    <bean id="user" class="com.fourth.user.User" p:name="tom" p:age="20" p:personMap-ref="map">
    </bean>

    <util:map id="map">
        <entry>
            <key>
                <value>first</value>
            </key>
            <ref bean="person1"></ref>
        </entry>
        <entry>
            <key>
                <value>second</value>
            </key>
            <ref bean="person2"></ref>
        </entry>
    </util:map>

</beans>

 等于是不再用 property 或者 constructor-arg 注入属性,而是用 p 进行注入。

(十)自动装配

package com.fourth.auto;

public class Dao {
    public void runDao(){
        System.out.println("Hello World");
    }
}
package com.fourth.auto;

public class Service {
    private Dao dao;

    public void setDao(Dao dao) {
        this.dao = dao;
    }

    public void runService(){
        dao.runDao();
    }
}
package com.fourth.auto;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestService {
    private Service service;

    public void setService(Service service) {
        this.service = service;
    }

    public void runTestService(){
        service.runService();
    }
    @Test
    public void testMethod(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean-auto.xml");
        TestService testService = context.getBean("testService", TestService.class);
        testService.runTestService();
    }
}
<?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="dao" class="com.fourth.auto.Dao" ></bean>
    <bean id="service" class="com.fourth.auto.Service" autowire="byType"></bean>
    <bean id="testService" class="com.fourth.auto.TestService" autowire="byType"></bean>
</beans>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值