依赖注入(构造器注入、set方式注入、p、c命名空间注入)

1.构造器注入

实体类

package com.yl;

public class Const {
    private  Integer sid;
    private String name;
    private String age;
    private  String course;

    public Const(Integer sid, String name, String age, String course) {
        this.sid = sid;
        this.name = name;
        this.age = age;
        this.course = course;
    }

    @Override
    public String toString() {
        return "Const{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", course='" + course + '\'' +
                '}';
    }
}

xml1通过名字注入

<?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-2.5.xsd">
    <bean id="const" class="com.yl.Const">
        <constructor-arg name="sid" value="1"/>
        <constructor-arg name="name" value="雨露"/>
        <constructor-arg name="age" value="22"/>
        <constructor-arg name="course" value="java"/>
    </bean>
</beans>

测试类

public class TestSpring {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("const.xml");
        Const aConstconst = context.getBean("const",Const.class);
        System.out.println(aConstconst);
    }
}

 输出

Const{sid=1, name='雨露', age='22', course='java'}

 xml2通过下标(从零开始)

<?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-2.5.xsd">
    <bean id="const1" class="com.yl.Const">
        <constructor-arg index="0" value="22"/>
        <constructor-arg index="1" value="雨露22index"/>
        <constructor-arg index="2" value="222index"/>
        <constructor-arg index="3" value="java2index"/>
    </bean>
</beans

测试类

public class TestSpring1 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("const1.xml");
        Const aConstconst = context.getBean("const1",Const.class);
        System.out.println(aConstconst);
    }
}

 输出:

Const{sid=22, name='雨露22index', age='222index', course='java2index'}

xml3通过类型注入

<?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-2.5.xsd">
    <bean id="const2" class="com.yl.Const">
        <constructor-arg type="java.lang.Integer" value="222"/>
        <constructor-arg type="java.lang.String" value="雨露leixing"/>
        <constructor-arg type="java.lang.String" value="222leixing"/>
        <constructor-arg type="java.lang.String" value="java2leixing"/>
    </bean>
</beans>

测试类

public class TestSpring2 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("const2.xml");
        Const aConstconst = context.getBean("const2",Const.class);
        System.out.println(aConstconst);
    }
}

 输出:

Const{sid=222, name='雨露leixing', age='222leixing', course='java2leixing'}

2.Set方式注入

依赖注入:set注入

依赖:bean对象的创建依赖于spring容器

注入:bean对象中的所有属性由容器注入

环境搭建:

1.复杂类型

package com.yl;

public class Address {
    private String address;

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }

    public String getAddress() {
        return address;
    }

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

2.真实测试对象

package com.yl;

import java.util.*;

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;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }

    public String getName() {
        return name;
    }

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

    public Address getAddress() {
        return address;
    }

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

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }
}

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
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <bean id="student" class="com.yl.Student">
        <!--第一种:普通值注入,value-->
        <property name="name" value="雨露"></property>
    </bean>
</beans>

 测试类

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student =(Student) context.getBean("student");
        System.out.println(student.getName());
    }
}

完善注入信息

<?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-2.5.xsd">
    <bean id="address" class="com.yl.Address"/>
    <bean id="student" class="com.yl.Student">
        <!--第一种:普通值注入,value-->
        <property name="name" value="雨露"></property>
        <!--第二种:Bean注入,ref-->
        <property name="address" ref="address"/>
        <!--数组注入,ref-->
        <property name="books" >
            <array>
                <value>火星救援</value>
                <value>流浪地球</value>
                <value>伤心者</value>
            </array>
        </property>
        <!--list注入,ref-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>魔方</value>
                <value>跑步</value>
            </list>
        </property>
        <!--Map注入,ref-->
        <property name="card">
            <map>
                <entry key="安其拉" value="心灵骇客"/>
                <entry key="赵云" value="引擎之心"/>
            </map>
        </property>
        <!--Set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>
        <!--NULL-->
        <property name="wife">
            <null/>
        </property>
        <property name="info">
            <props>
                <prop key="学号">1427064122</prop>
                <prop key="专业">机械设计制造及其自动化</prop>
            </props>
        </property>
    </bean>
</beans>

MyTest

import com.yl.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student =(Student) context.getBean("student");
        System.out.println(student.toString());
    }
}

 输出:

Student{name='雨露', address=Address{address='null'},
 books=[火星救援, 流浪地球, 伤心者], hobbys=[听歌, 魔方, 跑步], 
card={安其拉=心灵骇客, 赵云=引擎之心}, games=[LOL, COC, BOB], 
wife='null', info={学号=1427064122, 专业=机械设计制造及其自动化}}

3.拓展方式注入

使用@Test时,pom.xml中需要加入junit配置

        <dependency>
            <groupId>org.apache.isis.viewer</groupId>
            <artifactId>junit</artifactId>
            <version>0.2.0-incubating</version>
        </dependency>

1.c命名空间

xml,需要增加xmlns:c="http://www.springframework.org/schema/c"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"
       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-2.5.xsd">
    <!--c命名空间注入,通过constructor-args注入-->
    <bean id="user2" class="com.yl.User" c:age="222" c:name="宇鹏"/>
</beans>

相当于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"
       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-2.5.xsd">
    <bean id="user2" class="com.yl.User">
        <constructor-arg index="0" value="雨露无敌"></constructor-arg>
        <constructor-arg index="1" value="222"></constructor-arg>
    </bean>
</beans>

测试

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
        User user = context.getBean("user2", User.class);
        System.out.println(user);
    }

输出:

User{name='雨露无敌', age=222}

 

2.p命名空间

xml,需要增加xmlns:p="http://www.springframework.org/schema/p"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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <!--p命名空间注入,可以直接注入属性的值,properties-->
<bean id="user" class="com.yl.User" p:name="雨露" p:age="22">

</bean>
</beans>

User

package com.yl;

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

 测试类:

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student =(Student) context.getBean("student");
        System.out.println(student.toString());
    }
}

输出:

User{name='雨露', age=22}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值