spring-DI

1.什么是DI

2.依赖注入的类型和方式

3.使用spring注入数据

4.自动注入

5.使用注解完成IOC和DI功能

1.DI介绍

DI—Dependency Injection,即“依赖注入”:

组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到

组件之中。

Java Dependency Injection设计模式允许我们删除硬编码的依赖关系,并使我们的应用程序

松散耦合,可扩展和可维护。我们可以在Java中实现依赖注入,以将依赖解析从编译时移至运行

时。
        总之一句话:给对象中的属性赋值。

所谓依赖注入,是指程序运行过程中,如果需要调用另一个对象协助时,无须在代码中创建

被调用者,而是依赖于外部的注入。Spring的依赖注入对调用者和被调用者几乎没有任何要求,完

全支持对POJO之间依赖关系的管理。

2.依赖注入的类型和方式

基本数据类型, 字符串类型,引用类型,集合类型。

- 通过构造方法注入     
- setter方法注入      
- 接口注入

3.使用spring-DI注入数据

3.1 基本数据类型(String,Interger)

(1)Student类

public class Student {
    private Integer age;
    private String name;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
        System.out.println("====调用了setAge====");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("====调用了setName====");
    }

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

(2)配置文件

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

    <bean id="stu01" class="com.wx.demo01.Student">
        <property name="name" value="张三" />
        <property name="age" value="18" />
    </bean>
</beans>

(3)测试

public class Test01 {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("spring01.xml");
        Student stu01 = (Student) app.getBean("stu01");
        System.out.println(stu01);
    }
}

 3.2 注入类对象类型

 (1)StuClass类

public class StuClass {
    private Integer cid;
    private String cname;

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    public String toString() {
        return "StuClass{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                '}';
    }
}

(2)StuClass变为Student的属性,自动生成其set方法,修改toString(),输出

private Integer age;
private String name;
private StuClass sc;

 3.3 注入集合类型

 1、注入List集合类型

(1)将list集合添加为Student类属性

private Integer age;
private String name;
private StuClass sc;
private List<String> hobby;

public void setHobby(List<String> hobby){
        this.hobby =hobby;
    }

 (2)修改配置文件

 <bean id="stu01" class="com.wx.demo01.Student">
        <property name="name" value="张三" />
        <property name="age" value="18" />
        <property name="sc" ref="stuClass" />
        <property name="hobby" >
            <list>
                <value>唱歌</value>
                <value>爬山</value>
                <value>游泳</value>
            </list>
        </property>
 </bean>

2、注入Map集合类型

(1)添加类属性

private Integer age;
private String name;
private StuClass sc;
private List<String> hobby;
private Map<String string> map;

@Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", hobby=" + hobby +
                ", sc=" + sc +
                ", map=" + map +
                '}';
    }

(2)配置文件

<bean id="stu01" class="com.wx.demo01.Student">
        <property name="name" value="张三" />
        <property name="age" value="18" />
        <property name="sc" ref="stuClass" />
        <property name="hobby" >
            <list>
                <value>唱歌</value>
                <value>爬山</value>
                <value>游泳</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="name" value="张三"/>
                <entry key="age" value="18"/>
                <entry key="sex" value="男"/>
            </map>
        </property>
    </bean>

3 、注入set集合

(1)添加类属性

private Integer age;
private String name;
private StuClass sc;
private List<String> hobby;
private Map<String string> map;
private Set<String> set;

@Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                ", sc=" + sc +
                ", hobby=" + hobby +
                ", set=" + set +
                ", map=" + map +
                '}';
    }

(2) 配置文件

<property name="set">
   <set>
      <value>abc</value>
      <value>abc</value>
      <value>bcd</value>
      <value>bcd</value>
   </set>
</property>

4.自动注入

public class UserController {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void fun(){
        userDao.show();
    }
}
public interface UserDao {

    public void show();
}

public class UserDao01 implements UserDao {
    public void show() {
        System.out.println("userDao01-----how方法");
    }
}

public class UserDao02 implements UserDao{
    public void show() {
        System.out.println("userDao02=====show方法");
    }
}

 5.使用注解完成IOC和DI功能

(1)包扫描配置文件

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

    <context:component-scan base-package="com.wx.demo01"/>
</beans>

(2)创建controller

/*
    controller 表示控制层注解,如果没有设置bean的id 默认为类的名称首字母小写 相当于在spring配置文件中的
            <bean id="" class="com.wx.demo01.UserController">
    Autowire 按照类型自动注入 如果需要指定名称注入需要配合Qualifier注解使用

    Qualifier 相当于<property name="ud" ref="user01">
* */
@Controller
public class UserController {

    @Autowired
    @Qualifier(value = "user01")
    private UserDao ud;

    public void fun(){
        ud.show();
    }
}

(3)创建userdao及其实现类

public interface UserDao {
    public void show();
}

@Repository(value = "user01")
public class UserDao01 implements UserDao {
    public void show() {
        System.out.println("======userDao01=====");
    }
}

@Repository(value = "user02")
public class UserDao02 implements UserDao {
    public void show() {
        System.out.println("======userDao02=====");
    }
}

(4)测试

public class Test02 {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("user01.xml");
        UserController uc = (UserController) app.getBean("userController");
        uc.fun();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值