Spring03-DI

Spring 依赖注入

依赖注入

指bean对象依赖Spring,bean依赖的资源由Spring容器注入

构造器注入

构造器注入
可参见上一篇博客,分为有参构造器和无参构造器

set注入

要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is .

新建两个类Address类和Student类

public class Address {

    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
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);

    }
}

1.基本类型

<bean id="student" class="com.jojo3.Student">
        <property name="name" value="zhangsan"></property>
</bean>

2.引用类型

<bean id="student" class="com.jojo3.Student">
        <property name="name" value="zhangsan"></property>
        <property name="address" ref="address"></property>
</bean>

3.数组

<bean id="student" class="com.jojo3.Student">
        <property name="name" value="zhangsan"></property>
        <property name="address" ref="address"></property>
        <property name="books">
            <array>
                <value>jojo1</value>
                <value>jojo2</value>
                <value>jojo3</value>
            </array>
        </property>
    </bean>

4 List

<property name="hobbys">
            <list>
                <value>乔鲁诺</value>
                <value>乔巴拿</value>
                <value>乔太郎</value>
            </list>
 </property>

5.Set

<property name="games">
           <set>
               <value>1</value>
               <value>2</value>
               <value>3</value>
           </set>
</property>

6.Map

<property name="card">
           <map>
               <entry key="jojo1" value="123"></entry>
               <entry key="jojo2" value="456"></entry>
               <entry key="jojo3" value="789"></entry>
           </map>
</property>

7.property

<property name="info">
           <props>
               <prop key="jojo1">12</prop>
               <prop key="jojo2">45</prop>
               <prop key="jojo3">76</prop>
           </props>
 </property>

8.null

<property name="wife"><null></null></property>

进行测试

 public void test6(){
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans3.xml");
       Student student = applicationContext.getBean("student",Student.class);
       student.show();
   }
 
 输出:
 name=zhangsan,address=null,books=
<<jojo1>>	<<jojo2>>	<<jojo3>>	
爱好:[乔鲁诺, 乔巴拿, 乔太郎]
card:{jojo1=123, jojo2=456, jojo3=789}
games:[1, 2, 3]
wife:null
info:{jojo3=76, jojo2=45, jojo1=12}

p命名和c命名注入
p命名

新建一个User类
public class User {
    String name;

    public void setName(String name) {
        this.name = name;
    }

    public User() {
    }

    public void show(){
        System.out.println(name);
    }
}

配置xml,需注意要添加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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="user" class="com.jojo4.User" p:name="张三"></bean>
</beans>

进行测试

public void test7(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans4.xml");
        User user = applicationContext.getBean("user",User.class);
        user.show();
    }

c命名

新建一个User2类和Cat类
public class User2 {
    String name;
    Cat cat;

//    public void setName(String name) {
//        this.name = name;
//    }
//
//    public void setCat(Cat cat) {
//        this.cat = cat;
//    }

    public User2(String name, Cat cat) {
        this.name = name;
        this.cat = cat;
    }

    public void show(){
        System.out.println(name);
        cat.shout();
    }
}
public class Cat {

    String name;

    public void setName(String name) {
        this.name = name;
    }

    public void shout(){
        System.out.println(name+"miaomiaomiao");
    }
}

配置xml

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


    <bean id="user" class="com.jojo4.User" p:name="张三"></bean>

    <bean id="cat" class="com.jojo4.Cat" p:name="wangwang"></bean>

    <bean id="user2" class="com.jojo4.User2" c:name="jojo" c:cat-ref="cat"></bean>
</beans>
测试
public void test8(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans4.xml");
        com.jojo4.User2 user2 = applicationContext.getBean("user2", com.jojo4.User2.class);
        user2.show();
    }

输出:
jojo
wangwangmiaomiaomiao

注意点:可以看到,p命名空间时,如果注释掉set方法,那么配置xml时将会爆红,而c命名空间,注释掉set方法不影响其注入

Bean的作用域

在这里插入图片描述
Spring容器中bean的作用域主要有如上几种,可在xml中进行配置

<bean id="user2" class="com.jojo4.User2" c:name="jojo" c:cat-ref="cat" scope="singleton"></bean>

使用scope可配置作用域
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值