浅谈Spring03—依赖注入

8、引入依赖注入

8.1 创建工程

在这里插入图片描述

8.1.1 CustomerDao

package com.zhc1024.dao;

public interface CustomerDao {
    void save();
}

8.1.2 CustomerDaoImpl

package com.zhc1024.dao.impl;

import com.zhc1024.dao.CustomerDao;

public class CustomerDaoImpl implements CustomerDao {

    public CustomerDaoImpl (){
        System.out.println("我是构造方法");
    }

    public void save() {
        System.out.println("保存成功!");
    }
}

8.1.3 CustomerService

package com.zhc1024.service;

public interface CustomerService {
    void save();
}

8.1.4 CustomerServiceImpl

package com.zhc1024.service.impl;


import com.zhc1024.dao.CustomerDao;
import com.zhc1024.dao.impl.CustomerDaoImpl;
import com.zhc1024.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {

    private CustomerDao customerDao = new CustomerDaoImpl();

    public void save() {
        customerDao.save();
    }
}

8.1.5 CustomerController

package com.zhc1024.controller;

import com.zhc1024.service.CustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CustomerController {

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

        CustomerService service = (CustomerService) ac.getBean("service");

        service.save();

    }
}

8.1.6 运行结果

在这里插入图片描述
上面是正常的程序,下面我们根据Spring的思想:IOC帮我们实例对象。我们在Service实现类那里做一下修改,看看运行结果

8.1.7 修改CustomerServiceImpl

package com.zhc1024.service.impl;


import com.zhc1024.dao.CustomerDao;
import com.zhc1024.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {

    //我不new了,按照Spring的思想是容器给我提供对象,我只是声明而已
    private CustomerDao customerDao;

    public void save() {
        customerDao.save();
    }
}

运行结果

在这里插入图片描述

这是因为我们还有把对象放到IOC容器里面,也就是说还没有和Spring扯上关系,所以它自然不会帮我们创建和管理对象了,接下来我们就学习一下Spring的注入依赖

9、依赖注入(构造方法注入)

9.1 改造CustomerDaoImpl
package com.zhc1024.dao.impl;

import com.zhc1024.dao.CustomerDao;

import java.util.Date;

public class CustomerDaoImpl implements CustomerDao {

    private int id;
    private String name;
    private int age;
    private Date birthday;


    public CustomerDaoImpl(int id, String name,int age, Date birthday) {
        this.id = id;
        this.name = name;
        this.age =age;
        this.birthday = birthday;
    }

    /**
     * 保存客户
     */
    public void save() {
        System.out.println("id="+id+",name="+name+",age="+age+",birthday="+birthday);
    }
}

9.2 改造bean.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.xsd">

                <!--
                       constructor-arg标签的属性及其作用:
                                index:成员变量在构造方法参数列表中的索引
                                name:成员变量的属性名称
                                type:成员变量的类型
                                value:给java简单类型的成员变量赋值(八种简单类型和String类型)
                                ref:引用 其他bean类型的值
                -->
        <bean id="customer" class="com.zhc1024.dao.impl.CustomerDaoImpl">
                <constructor-arg index="0" value="1"/><!--可以使用下表表示-->
                <constructor-arg name="name" value="张三"/><!--也可以使用name-value的形式-->
                <constructor-arg name="age" value="18"/>
                <constructor-arg name="birthday" ref="date"/><!--ref表示引用-->
        </bean>

        <!--配置 时间-->
        <bean id="date" class="java.util.Date"/>
</beans>
9.3 改造controller和运行结果

在这里插入图片描述

10、依赖注入(set方法注入)

10.1 改造CustomerDaoImpl
package com.zhc1024.dao.impl;

import com.zhc1024.dao.CustomerDao;

import java.util.Date;

public class CustomerDaoImpl implements CustomerDao {

    private int id;
    private String name;
    private int age;
    private Date birthday;


    public void setId(int id) {
        this.id = id;
    }

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

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

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public CustomerDaoImpl(int id, String name, int age, Date birthday) {
        this.id = id;
        this.name = name;
        this.age =age;
        this.birthday = birthday;
    }

    /**
     * 保存客户操作
     */
    public void save() {
        System.out.println("id="+id+",name="+name+",age="+age+",birthday="+birthday);
    }
}

10.2 修改bean.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.xsd">

        <!--
                set注入:
                        property:指定通过set方法赋值
                        value:给java简单类型的成员变量赋值(八种简单类型和String类型)
                        ref:引用 其他bean类型的值
        -->
        <bean id="customer" class="com.zhc1024.dao.impl.CustomerDaoImpl">
                <property name="id" value="1"/>
                <property name="age" value="18"/>
                <property name="birthday" ref="date"/>
                <property name="name" value="李四"/>
         </bean>


        <!--配置 时间-->
        <bean id="date" class="java.util.Date"/>
</beans>
10.3 运行结果

在这里插入图片描述

10.4 简化set注入方式

简化set注入方式可以使用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">

        <!--
            导入p名称空间
            xmlns:p="http://www.springframework.org/schema/p"

               p:属性名="属性值"
        -->
        <bean id="customer" class="com.zhc1024.dao.impl.CustomerDaoImpl"
            p:id="1" p:age="18" p:birthday-ref="date" p:name="王五"
        />


        <!--配置 时间-->
        <bean id="date" class="java.util.Date"/>
</beans>
10.5 运行结果

在这里插入图片描述

10.6 简化构造方法注入方式

修改bean.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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

        <!--
               第一步:导入c名称空间
            xmlns:c="http://www.springframework.org/schema/c"
        第二步:
            c:属性名称 ==>给java简单类型成员变量赋值
            c:属性名称-ref ==>给其他bean类型成员变量赋值
        -->
        <bean id="customer" class="com.zhc1024.dao.impl.CustomerDaoImpl"
              c:id="1" c:age="18" c:birthday-ref="date" c:name="赵六">
        </bean>

        <!--配置 时间-->
        <bean id="date" class="java.util.Date"/>
</beans>

修改CustomerDaoImpl

package com.zhc1024.dao.impl;

import com.zhc1024.dao.CustomerDao;

import java.util.Date;

public class CustomerDaoImpl implements CustomerDao {

    private int id;
    private String name;
    private int age;
    private Date birthday;


    public CustomerDaoImpl(int id, String name, int age, Date birthday) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    /**
     * 保存客户操作
     */
    public void save() {
        System.out.println("id="+id+",name="+name+",age="+age+",birthday="+birthday);
    }
}

10.7运行结果

在这里插入图片描述

11、两种依赖注入的区别

set注入:spring在实例化一个对象的时候先把该对象实例化,然后再找依赖于这个对象的其他对象实例化;

构造函数注入:在实例化一个对象的时候先把其他依赖于该对象的对象实例化,再把该对象实例化

12、集合属性依赖注入

12.1 修改CustomerDaoImpl
// 集合类型成员变量
    private String[] array;
    private List<String> list;
    private Set<String> set;

    private Map<String,String> map;
    private Properties prop;

    //===========================================set方法
    public void setArray(String[] array) {
        this.array = array;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public void setProp(Properties prop) {
        this.prop = prop;
    }

    /**
     * 保存操作
     */
    public void saveCustomer() {
        System.out.println(array !=null? Arrays.asList(array):"");
        System.out.println(list);
        System.out.println(set);
        System.out.println(map);
        System.out.println(prop);
    }
12.2 修改bean.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.xsd">

        <bean id="customer" class="com.zhc1024.dao.impl.CustomerDaoImpl">
                <!--数组赋值-->
                <property name="array">
                        <array>
                                <value>array1</value>
                                <value>array2</value>
                        </array>
                </property>

                <!--list集合赋值-->
                <property name="list">
                        <list>
                                <value>list1</value>
                                <value>list2</value>
                        </list>
                </property>

                <!--set集合赋值-->
                <property name="set">
                        <set>
                                <value>set1</value>
                                <value>set2</value>
                        </set>
                </property>

                <!--map集合赋值-->
                <property name="map">
                        <map>
                                <entry key="1" value="1"/>
                                <entry key="2" value="2"/>
                        </map>
                </property>

                <!--prop-->
                <property name="prop">
                        <props>
                                <prop key="p1">p1</prop>
                                <prop key="p2">p2</prop>
                        </props>
                </property>
        </bean>
</beans>
12.3 运行结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架可以通过依赖注入来管理对象之间的关系。泛型依赖注入Spring 4版本引入的一个新特性,它允许我们使用泛型类型作为依赖注入的目标类型。 在Spring中,我们可以使用`@Autowired`注解来实现依赖注入。在Spring 4之前的版本中,我们只能使用具体的类型来指定注入的目标类型。但是在Spring 4中,我们可以使用泛型类型来指定注入的目标类型。 泛型依赖注入的好处之一是可以减少重复的代码。例如,我们可以定义一个通用的泛型接口或抽象类,然后在具体的类中使用泛型类型来指定依赖注入的目标类型。这样,我们可以减少重复的配置代码,并提高代码的可维护性和灵活性。 另一个好处是增加了类型安全性。使用泛型类型来指定依赖注入的目标类型可以在编译时检查类型是否匹配,避免在运行时出现类型转换错误或异常。 下面是一个示例代码,演示如何在Spring 4中使用泛型依赖注入: ```java public interface GenericDao<T> { // ... } @Component public class UserDao implements GenericDao<User> { // ... } @Service public class UserService { @Autowired private GenericDao<User> userDao; // ... } ``` 在上面的示例中,我们定义了一个泛型接口`GenericDao`,并在具体的实现类`UserDao`中使用了具体的类型`User`来指定泛型类型。然后,我们在`UserService`中使用泛型类型`User`来注入`UserDao`。 通过使用泛型依赖注入,我们可以更方便地管理依赖关系,并且减少了重复的配置代码。这是Spring 4版本引入的一个有用的特性,可以在开发中提高效率和代码质量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值