Spring(三):依赖注入

15 篇文章 0 订阅

构造器注入

set方式注入 重点

依赖注入(Dependency Injection,DI)。

依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .

注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

引用对象类:Address
public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}
创建实体类:student
public class Student {
 
     private String name;
     private Address address;
     private String[] books;
     private List<String> hobbys;
     private Map<String,String> card;
     private Set<String> games;
     private String wife;
     private Properties info;
 
     public void setName(String name) {
         this.name = name;
    }
 
     public void setAddress(Address address) {
         this.address = address;
    }
 
     public void setBooks(String[] books) {
         this.books = books;
    }
 
     public void setHobbys(List<String> hobbys) {
         this.hobbys = hobbys;
    }
 
     public void setCard(Map<String, String> card) {
         this.card = card;
    }
 
     public void setGames(Set<String> games) {
         this.games = games;
    }
 
     public void setWife(String wife) {
         this.wife = wife;
    }
 
     public void setInfo(Properties info) {
         this.info = info;
    }
 
     public void show(){
         System.out.println("name="+ name
                 + ",address="+ address.getAddress()
                 + ",books="
        );
         for (String book:books){
             System.out.print("<<"+book+">>\t");
        }
         System.out.println("\n爱好:"+hobbys);
 
         System.out.println("card:"+card);
 
         System.out.println("games:"+games);
 
         System.out.println("wife:"+wife);
 
         System.out.println("info:"+info);
 
    }
 }
所有的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="addr" class="com.Devin.pojo.Address"/>

    <bean id="student" class="com.Devin.pojo.Student">
       <!-- 第一种:普通值注入-->
        <property name="name" value="张书榜"/>

        <!-- 第二种 Bean注入 ,ref-->
        <property name="address" ref="addr"/>

        <!--数组注入,ref-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>三国演义</value>
                <value>西游记</value>
            </array>
        </property>

        <!--List注入-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>看电影</value>
                <value>听歌</value>
            </list>
        </property>

        <!--Map注入-->
        <property name="card">
            <map>
                <entry key="身份" value="学生"/>
                <entry key="手机号" value="15937351890"/>
                <entry key="学号" value="160210535"/>
            </map>
        </property>

        <!--set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>DNF</value>
                <value>CF</value>
                <value>COC</value>
            </set>
        </property>

        <!--null值注入-->
        <property name="wife">
            <null/>
        </property>

        <!--Properties注入-->
        <property name="info">
            <props>
                <prop key="学号">140720</prop>
                <prop key="手机号">1593735</prop>
                <prop key="姓名">Devin</prop>
                <prop key="姓别"></prop>
            </props>
        </property>



    </bean>



</beans>
测试:.
import com.Devin.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTast {
    //获取ApplicationContext
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        Student address = (Student) context.getBean("student");

        System.out.println(address.toString());
    }
}


其他方式注入

P命名空间:需要在头文件中加入约束文件

/*
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
*/



<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--p命名空间注入,可以直接注入属性的值:property,属性依然要设置set方法-->
        <bean id="user" class="com.Devin.pojo.User" p:name="书榜" p:age="17"/>
</beans>

c命名空间:需要在头文件中加入约束文件

/*
导入约束 : xmlns:c="http://www.springframework.org/schema/c"
*/

<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
        

        <!--c'命名空间,通过构造器注入:construct-args 属性依然要设置set方法-->
        <bean id="user2" class="com.Devin.pojo.User" c:age="17" c:name="Devin"/>



</beans>

测试:

	@Test
    public void Test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("UserBaens.xml");
        User user = (User) context.getBean("user");
        System.out.println(user);
    }
    @Test
    public void Test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("UserBaens.xml");
        User user = (User) context.getBean("user2");
        System.out.println(user);
    }
注意点:

P命名和C命名不能直接用,需要导入xml约束

xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

本文章来源于B站(狂神说) B站地址:https://space.bilibili.com/95256449

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值