依赖的注入注入方式

我们这里注入的方式有三种方法
构造方法(在我们之前进行传递参数的时候使用过)、set注入(重点)、拓展方法。

set注入

首先是我们创建一个实体类Student,重点是我们要注意这里的属性设置,以及get、set方法。

package aw.dao;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String [] book;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties properties;

    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[] getBook() {
        return book;
    }

    public void setBook(String[] book) {
        this.book = book;
    }

    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 getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

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

Address类

package aw.dao;

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 + '\'' +
                '}';
    }
}

xml的配置,重点是看一下配置,以及标签使用。

<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="add" class="aw.dao.Address">
        <property name="address" value="菏泽"/>
    </bean>

   <bean id="student" class="aw.dao.Student">
<!--       通过set进行注入-->
       <property name="name" value="阿威"/>
<!--       通过ref对象进行注入-->
       <property name="address" ref="add"/>
<!--    通过array数组进行注入-->
       <property name="book" >
           <array>
               <value>高数</value>
               <value>英语</value>
               <value>程序设计</value>
           </array>
       </property>
<!--       通过map集合进行注入-->
       <property name="card" >
           <map >
               <entry key="身份证" value="1111111111111111111"/>
               <entry key="银行卡" value="1111111111111111111"/>
           </map>
       </property>
<!--        通过set集合进行注入-->
       <property name="games" >
           <set>
               <value>lol</value>
               <value>OverWatch</value>
               <value>csgo</value>
           </set>
       </property>
<!--        通过list集合-->
       <property name="hobbys" >
           <list>
               <value>听歌</value>
               <value>打篮球</value>
           </list>
       </property>
<!--       通过 property文件-->
       <property name="properties">
           <props>
               <prop key="学号">2019130287</prop>
               <prop key="姓名">阿威</prop>
               <prop key="性别">男</prop>
           </props>
       </property>

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

   </bean>

</beans>

测试类

public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext4.xml");

        Student student = (Student) context.getBean("student");
        System.out.println(student);
    }
//Student{name='阿威', address=Address{address='菏泽'}, book=[高数, 英语, 程序设计], hobbys=[听歌, 打篮球], card={身份证=1111111111111111111, 银行卡=1111111111111111111}, games=[lol, OverWatch, csgo], wife='null', properties={学号=2019130287, 性别=男, 姓名=阿威}}

拓展的使用

我们这里的拓展使用主要是引入外部文件,我们可以在官方文档中进行获取

p命名空间

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

实体类

package aw.dao;

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

    public User(){
        System.out.println("使用无参构造方法");
    }

    public User(String args){
        System.out.println(args+"正在使用有参构造方法");
    }

    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;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

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

这样我们在bean中就可以这样直接命名

<bean id="user" class="aw.dao.User"  p:name="阿威" p:age="20" p:sex="男"/>

测试类

 @Test
    public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext5.xml");
        User user = (User) context.getBean("user");
        System.out.println(user);

    }
}

c命名空间 这里主要是对构造方法进行设计的。

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

他们两个一可以结合使用,这里重点是要有构造方法,否则不能使用。

<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">

    <bean id="user" class="aw.dao.User" c:args="阿牛" p:name="阿威" p:age="20" p:sex="男"/>
</beans>
    public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext5.xml");
        User user = (User) context.getBean("user");
        System.out.println(user);

    }
}
//阿牛正在使用有参构造方法
//User{name='阿威', age=20, sex='男'

对于p c 的使用我们仅作为了解,重点是对set注入的使用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值