六依赖注入

6.1.构造器注入

6.2.set注入【重点】

依赖注入

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

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

环境搭建

(1).复杂类型

package com.sqt.pojo;

public class Address {
   private String Address;

   public String getAddress() {
      return Address;
   }

   public void setAddress(String address) {
      Address = address;
   }

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

(2).真实的测试对象

package com.sqt.pojo;

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;

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

    @Override
    //这里的adress是一个对象需要toString
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}

一,注入

<?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="address" class="com.sqt.pojo.Address">
           <property name="address" value="流程"></property>
       </bean>
       <bean id="student" class="com.sqt.pojo.Student">
           <property name="name" value="撒啊"/>
<!--    第二种bean注入,ref-->
           <property name="address" ref="address"/>
<!--           数组注入-->
           <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>
           <property name="card">
               <map>
                   <entry key="身份中" value="12345687654"/>
                   <entry key="银航卡" value="23567890"/>
               </map>
           </property>
           <property name="games">
               <set>
                   <value>Lol</value>
                   <value>coc</value>
                   <value>bob</value>
               </set>
           </property>
           <property name="wife" >
                   <null/>
           </property>
           <property name="info">
               <props>
                   <prop key="学号">456789</prop>
                   <prop key="姓名">faasd</prop>
                   <prop key="性别">男</prop>
                   <prop key="学号">45678902</prop>
               </props>
           </property>
       </bean>

</beans>

简单值测试

import com.sqt.pojo.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
//        Student{name='撒啊',
//        address=Address{Address='流程'},
//        books=[红楼梦, 三国, 西游记, 水户传],
//        hobbys=[敲代码, 听歌, 写作业],
//        card={身份中=12345687654, 银航卡=23567890},
//        games=[Lol, coc, bob],
//        wife='null',
//        info={学号=45678902, 性别=男, 姓名=faasd}}
    }
}

6.3扩展方式注入

pojo

package com.sqt.pojo;

public class User {
    private String name;
    private int age;
//c命名空间需要有参构造器
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public User() {
    }

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

userbeans.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.xsd">
<!--    p命名空间注入,可以注入属性的值-->
    <bean id="user" class="com.sqt.pojo.User" p:name="请撒" p:age="55">

    </bean>
    <bean id="user2" class="com.sqt.pojo.User" c:name="刘彩" c:age="77"/>


</beans>

测试类

import com.sqt.pojo.Student;
import com.sqt.pojo.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Mytest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
//        Student{name='撒啊',
//        address=Address{Address='流程'},
//        books=[红楼梦, 三国, 西游记, 水户传],
//        hobbys=[敲代码, 听歌, 写作业],
//        card={身份中=12345687654, 银航卡=23567890},
//        games=[Lol, coc, bob],
//        wife='null',
//        info={学号=45678902, 性别=男, 姓名=faasd}}
    }
    @Test
    public  void test2(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
        //User user = (User) context.getBean("user");
        //在此后面加上User.class就不用强置转换了
        User user = context.getBean("user2", User.class);
        System.out.println(user);
    }

}

注意点p与c命名空间都必须在xml文件放入约束否则无法使用,

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

而且c命名空间都必须在实体类中去使用有参和无参构造方法才能使用

6.4.bean的作用域

在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象 .

图片

(一)单例模式--也是spring的默认的模式

 <bean id="user2" class="com.sqt.pojo.User" c:name="刘彩" c:age="77" scope="singleton"/>

 (二)原型模式--每一次从容器中get都对产生一个新对象,多线程使用

 <bean id="user2" class="com.sqt.pojo.User" c:name="刘彩" c:age="77" scope="prototype"/>

(三)其余的request,session,application,都是web中开发使用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值