Spring-依赖注入(xml)

1.1 项目结构

1.2 构造方法

就是通过构造方法,给类的成员变量赋值

持久层(Dao)

CustomerDao

package cn.guardwhy.dao;
/**
 * 客户dao接口
 */
public interface CustomerDao {
    /**
     * 保存客户操作
     */
    void saveCustomer();
}

ConstructDaoImpl

package cn.guardwhy.dao.impl;

import cn.guardwhy.dao.CustomerDao;

import java.util.Date;
/**
 * 构造方法注入数据
 */
public class ConstructDaoImpl implements CustomerDao {
    // 1.类的成员变量
    private int id;
    private String name;
    private Integer age;
    private Date birthday;

    // 2.构造方法
    public ConstructDaoImpl(int id, String name, Integer age, Date birthday) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

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

业务层(Service)

CustomerService

package cn.guardwhy.service;
/**
 * 客户service接口
 */
public interface CustomerService {
    /**
     * 保存客户操作
     */
    void saveCustomer();
}

CustomerServiceImpl

package cn.guardwhy.service.impl;

import cn.guardwhy.dao.CustomerDao;
import cn.guardwhy.dao.impl.CustomerDaoImpl;
import cn.guardwhy.service.CustomerService;
/**
 * 客户service实现类
 */
public class CustomerServiceImpl implements CustomerService {
    // 定义客户dao
    private CustomerDao customerDao;

    /**
     * 保存客户操作
     */
    @Override
    public void saveCustomer() {
        customerDao.saveCustomer();
    }
}

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

    <!--配置客户service-->
    <bean id="customerService"  class="cn.guardwhy.service.impl.CustomerServiceImpl"></bean>

    <!--配置客户dao-->
    <bean id="customerDao" class="cn.guardwhy.dao.impl.CustomerDaoImpl"
          scope="singleton" init-method="init" destroy-method="destroy"
    ></bean>

    <!--构造方法注入数据
        标签:
            constructor-arg:指定通过构造方法给成员变量赋值
        属性:
            index:成员变量在构造方法参数列表中的索引
            name:成员变量的名称(与index使用一个即可,实际项目中用的更多)
            type:成员变量的类型(默认即可)
            value:给java简单类型成员变量赋值(八种基本类型+字符串String)
            ref:给其他bean类型成员变量赋值
    -->
    <bean id="constructDao" class="cn.guardwhy.dao.impl.ConstructDaoImpl">
        <constructor-arg index="0" name="id" type="int" value="1"></constructor-arg>
        <constructor-arg name="name" value="kobe"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="birthday" ref="now"></constructor-arg>
    </bean>
    <!--配置java.util.Date-->
    <bean id="now" class="java.util.Date"></bean>
</beans>

表现层(Controller)

CustomerController

package cn.guardwhy.controller;

import cn.guardwhy.dao.CustomerDao;
import cn.guardwhy.service.CustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 客户controller
 */
public class CustomerController {

    public static void main(String[] args) {
        // 1.创建spring IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        // 2.从容器中获取service对象
        CustomerService customerService = (CustomerService) context.getBean("customerService");
        // 3.构造方法注入数据,保存用户
        CustomerDao constructDao = (CustomerDao) context.getBean("constructDao");
        constructDao.saveCustomer();
    }
}

执行结果

1.3 Set方法

Set方法注入,就是通过set方法,给类的成员变量赋值

持久层(Dao)

SetDaoImpl

package cn.guardwhy.dao.impl;

import cn.guardwhy.dao.CustomerDao;

import java.util.Date;

/**
 * set方法注入数据
 */
public class SetDaoImpl implements CustomerDao {
    // 1.类的成员变量
    private int id;
    private String name;
    private Integer age;
    private Date birthday;

    // 2.set方法
    public void setId(int id) {
        this.id = id;
    }

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

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

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

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

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

    <!--配置客户service-->
    <bean id="customerService"  class="cn.guardwhy.service.impl.CustomerServiceImpl"></bean>

    <!--配置客户dao-->
    <bean id="customerDao" class="cn.guardwhy.dao.impl.CustomerDaoImpl"
          scope="singleton" init-method="init" destroy-method="destroy">
    </bean>

    <!--set方法注入数据
        标签:
            property:指定通过set方法,给类的成员变量赋值
        属性:
            name:指定成员变量名称
            value:给java简单类型成员变量赋值(八种基本类型+字符串String)
            ref:给其他bean类型成员变量赋值
    -->
    <bean id="setDao" class="cn.guardwhy.dao.impl.SetDaoImpl">
        <property name="id" value="2" ></property>
        <property name="name" value="Curry"></property>
        <property name="age" value="18"></property>
        <property name="birthday" ref="now"></property>
    </bean>
    <!--配置java.util.Date-->
    <bean id="now" class="java.util.Date"></bean>
</beans>

表现层(Controller)

CustomerController

package cn.guardwhy.controller;

import cn.guardwhy.dao.CustomerDao;
import cn.guardwhy.service.CustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 客户controller
 */
public class CustomerController {

    public static void main(String[] args) {
        // 1.创建spring IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        // 2.从容器中获取service对象
        CustomerService customerService = (CustomerService) context.getBean("customerService");

        // 3.set方法注入数据
        CustomerDao setDao = (CustomerDao) context.getBean("setDao");
        setDao.saveCustomer();
    }
}

执行结果

1.4 p名称空间

p名称空间注入:就是set方法注入。其本质在于简化配置

配置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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--配置客户service-->
    <bean id="customerService"  class="cn.guardwhy.service.impl.CustomerServiceImpl"></bean>

    <!--配置客户dao-->
    <bean id="customerDao" class="cn.guardwhy.dao.impl.CustomerDaoImpl"
          scope="singleton" init-method="init" destroy-method="destroy"
    ></bean>
    <!--p名称空间注入
        第一步:导入p名称空间
            xmlns:p="http://www.springframework.org/schema/p"
        第二步:
            p:属性名称 ==>给java简单类型成员变量赋值
            p:属性名称-ref ==>给其他bean类型成员变量赋值
    -->
    <bean id="pDao" class="cn.guardwhy.dao.impl.SetDaoImpl"
        p:id="3" p:name="James" p:age="35" p:birthday-ref="now"
    >
    </bean>

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

表现层(Controller)

CustomerController

package cn.guardwhy.controller;

import cn.guardwhy.dao.CustomerDao;
import cn.guardwhy.service.CustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 客户controller
 */
public class CustomerController {

    public static void main(String[] args) {
        // 1.创建spring IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        // 2.从容器中获取service对象
        CustomerService customerService = (CustomerService) context.getBean("customerService");

        // 3.p名称注入数据
        CustomerDao pDao = (CustomerDao) context.getBean("pDao");
        pDao.saveCustomer();
    }
}

执行结果

1.5 C名称空间

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

    <!--配置客户service-->
    <bean id="customerService"  class="cn.guardwhy.service.impl.CustomerServiceImpl"></bean>

    <!--配置客户dao-->
    <bean id="customerDao" class="cn.guardwhy.dao.impl.CustomerDaoImpl"
          scope="singleton" init-method="init" destroy-method="destroy"
    ></bean>

        <!--c名称空间注入
            第一步:导入c名称空间
                xmlns:c="http://www.springframework.org/schema/c"
            第二步:
                c:属性名称 ==>给java简单类型成员变量赋值
                c:属性名称-ref ==>给其他bean类型成员变量赋值
        -->
    <bean id="cDao" class="cn.guardwhy.dao.impl.ConstructDaoImpl"
        c:id="4" c:name="Rondo" c:age="21" c:birthday-ref="now"
    >
    </bean>

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

表现层(Controller)

CustomerController

package cn.guardwhy.controller;

import cn.guardwhy.dao.CustomerDao;
import cn.guardwhy.service.CustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 客户controller
 */
public class CustomerController {

    public static void main(String[] args) {
        // 1.创建spring IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        // 2.从容器中获取service对象
        CustomerService customerService = (CustomerService) context.getBean("customerService");
        // 3.c名称空间注入数据
        CustomerDao cDao = (CustomerDao) context.getBean("cDao");
        cDao.saveCustomer();
    }
}

执行结果

1.6 集合属性注入

持久层(Dao)

CollectionDaoImpl

package cn.guardwhy.dao.impl;

import cn.guardwhy.dao.CustomerDao;

import java.util.*;

/**
 * 集合属性成员变量赋值
 */
public class CollectionDaoImpl implements CustomerDao {
    // 集合类型成员变量
    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;
    }

    /**
     * 保存客户操作
     */
    @Override
    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);
    }
}

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

    <!--配置客户service-->
    <bean id="customerService"  class="cn.guardwhy.service.impl.CustomerServiceImpl"></bean>

    <!--配置客户dao-->
    <bean id="customerDao" class="cn.guardwhy.dao.impl.CustomerDaoImpl"
          scope="singleton" init-method="init" destroy-method="destroy"
    ></bean>
    <!--集合属性注入
        1.List结构:
                array/list/set
        2.Map结构:
                map/prop
        3.数据结构一致,标签可以互换
    -->
    <bean id="CollectionDao" class="cn.guardwhy.dao.impl.CollectionDaoImpl">
        <!--array-->
        <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="map-k1" value="map-v1"></entry>
                <entry key="map-k1" value="map-v1"></entry>
            </map>
        </property>

        <!--prop-->
        <property name="prop">
            <props>
                <prop key="prop-k1"> prop-v1</prop>
                <prop key="prop-k1"> prop-v2</prop>
            </props>
        </property>
    </bean>
</beans>

表现层(Controller)

CustomerController

package cn.guardwhy.controller;

import cn.guardwhy.dao.CustomerDao;
import cn.guardwhy.service.CustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 客户controller
 */
public class CustomerController {

    public static void main(String[] args) {
        // 1.创建spring IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        // 2.从容器中获取service对象
        CustomerService customerService = (CustomerService) context.getBean("customerService");
        // 3.集合类型成员变量注入数剧
        CustomerDao collectionDao = (CustomerDao) context.getBean("CollectionDao");
        collectionDao.saveCustomer();
    }
}

执行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值