程序员干货JavaEE 学习专题SpringFramework

spring IOC之xml

2018-11-05  本文已影响1人  咸鱼有梦想呀

1.IOC

控制反转-Inversion Of Control
IOC意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。

①IOC控制什么?
IOC容器控制对象。传统Java SE程序设计,直接在对象内部通过new进行创建对象,是程序主动去创建依赖对象;而IOC是有专门一个容器来创建这些对象,即由IOC容器来控制对象的创建。

②什么是反转?
传统应用程序是由我们自己在对象中主动控制去直接获取依赖对象,也就是正转;而反转则是由容器来帮忙创建及注入依赖对象;IOC容器帮我们查找及注入依赖对象,对象只是被动的接受依赖对象,所以是反转。依赖对象的获取被反转了。

IOC反转

工厂为我们查找或者创建对象。是被动的。这种被动接收的方式获取对象的思想就是控制反转。它的作用只有一个:削减计算机程序的耦合

2.IOC开发环境搭建

需要的jar包
导包 约束
<?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">

    <!-- 配置资源:把对象的创建交给Spring管理 -->
    <bean id="customerService" class="com.edu.services.Impl.CustomerServiceImpl"></bean>
    <bean id="customerDao" class="com.edu.dao.Impl.ICustomerDaoImpl"></bean>
</beans>
package com.edu.ui;

import com.edu.services.ICustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args){
        //1.获取容器
        /*
         * ClassPathXmlApplicationContext:只能加载类路径下的配置文件
         * FileSystemXmlApplicationContext: 可以加磁盘任意位置的配置文件
          * */
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根据bean的id获取对象
        ICustomerService cs = (ICustomerService) ac.getBean("customerService");
        System.out.println(cs);
    }
}
运行结果

上述测试的所有类

目录结构

CustomerServiceImpl.java

package com.edu.services.Impl;

import com.edu.services.ICustomerService;

public class CustomerServiceImpl implements ICustomerService {

    @Override
    public void saveCustomer() {
        System.out.println("业务调用持久层");
    }
}

ICustomerService.java

package com.edu.services;

public interface ICustomerService {
    /**
     * 保存客户
     */
    void saveCustomer();
}

3.bean标签

作用:
用于配置对象让spring来创建的。
属性:

bean的两种创建规则

Spring中工厂的类结构图

蓝颜色的是接口,深绿是实现类,浅绿是抽象类。

bean的三种实例化方式

没有无参构造报异常

创建一个StaticFactory.java

package com.edu.factory;

import com.edu.services.ICustomerService;
import com.edu.services.Impl.CustomerServiceImpl;

public class StaticFactory {
    public static ICustomerService getCustomerServices(){
        return new CustomerServiceImpl();
    }
}

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

    <!-- 配置资源:把对象的创建交给Spring管理 -->
    <bean id="customerService" class="com.edu.services.Impl.CustomerServiceImpl"></bean>

    <!-- 配置使用静态工厂创建bean对象 -->
    <bean id="staticCustomerServices" class="com.edu.factory.StaticFactory" factory-method="getCustomerServices"></bean>

</beans>
配置使用静态工厂创建bean对象

client.java测试

package com.edu.ui;

import com.edu.services.ICustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args){
        //1.获取容器
        /*
         * ClassPathXmlApplicationContext:只能加载类路径下的配置文件
         * FileSystemXmlApplicationContext: 可以加磁盘任意位置的配置文件
          * */
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根据bean的id获取对象
//        ICustomerService cs = (ICustomerService) ac.getBean("customerService");
        ICustomerService cs = (ICustomerService) ac.getBean("staticCustomerServices");

        cs.saveCustomer();
    }
}
运行结果

创建一个InstanceFactory.java

package com.edu.factory;

import com.edu.services.ICustomerService;
import com.edu.services.Impl.CustomerServiceImpl;

public class InstanceFactory {
    public ICustomerService getCustomerServices(){
        return new CustomerServiceImpl();
    }
}

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

    <!-- 配置资源:把对象的创建交给Spring管理 -->
    <bean id="customerService" class="com.edu.services.Impl.CustomerServiceImpl"></bean>

    <!-- 配置使用静态工厂创建bean对象 -->
    <bean id="staticCustomerServices" class="com.edu.factory.StaticFactory" factory-method="getCustomerServices"></bean>

    <!-- 配置使用实例工厂创建bean对象 -->
    <bean id="instanceFactory" class="com.edu.factory.InstanceFactory"></bean>
    <bean id="instanceCustomerServices" factory-bean="instanceFactory" factory-method="getCustomerServices"></bean>


</beans>
配置使用实例工厂创建bean对象

client.java测试

package com.edu.ui;

import com.edu.services.ICustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args){
        //1.获取容器
        /*
         * ClassPathXmlApplicationContext:只能加载类路径下的配置文件
         * FileSystemXmlApplicationContext: 可以加磁盘任意位置的配置文件
          * */
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        //2.根据bean的id获取对象
//        ICustomerService cs = (ICustomerService) ac.getBean("customerService");
//        ICustomerService cs = (ICustomerService) ac.getBean("staticCustomerServices");
        ICustomerService cs = (ICustomerService) ac.getBean("instanceCustomerServices");

        cs.saveCustomer();
    }
}
运行结果

其中第一种方法最常用

bean的作用范围
可以通过配置的方式来调整作用范围
通过配置bean标签的scope属性

默认单例

修改bean.xml

修改scope 修改为多例

各创建一次,所以不相等

bean的生命周期


4.Spring的依赖注入

依赖注入是spring框架核心IOC的具体实现方式。简单的说,就是坐等框架把对象传入,而不用我们自己去获取。

Spring的依赖注入的三种方式

CustomerServiceImpl.java

package com.edu.services.Impl;

import com.edu.services.ICustomerService;

import java.util.Date;

public class CustomerServiceImpl implements ICustomerService {

    private String a;
    private Integer b;
    private Date c;

    public CustomerServiceImpl(String a, Integer b, Date c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public void saveCustomer() {
        System.out.println("业务调用持久层"+a+","+b+","+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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 使用构造函数的方式,给service中的属性传值
    要求:
        类中需要提供一个对应参数列表的构造函数。
    涉及的标签:
        constructor-arg
            属性:
                index:指定参数在构造函数参数列表的索引位置
                type:指定参数在构造函数中的数据类型
                name:指定参数在构造函数中的名称                  用这个找给谁赋值
                
                =======上面三个都是找给谁赋值,下面两个指的是赋什么值的==============
                
                value:它能赋的值是基本数据类型和String类型
                ref:它能赋的值是其他bean类型,也就是说,必须得是在配置文件中配置过的bean
     -->

    <bean id="customerService" class="com.edu.services.Impl.CustomerServiceImpl">
        <constructor-arg name="a" value="com.edu.services"></constructor-arg>
        <constructor-arg name="b" value="3306"></constructor-arg>
        <constructor-arg name="c" ref="now"></constructor-arg>
    </bean>
    <bean id="now" class="java.util.Date"></bean>
</beans>

client.java界面运行

package com.edu.ui;

import com.edu.services.ICustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main(String[] args){

        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        ICustomerService cs = (ICustomerService) ac.getBean("customerService");
        cs.saveCustomer();
    }
}
注入结果
package com.edu.services.Impl;

import com.edu.services.ICustomerService;

import java.util.Date;

public class CustomerServiceImpl implements ICustomerService {

    private String a;
    private Integer b;
    private Date c;

    public void setA(String a) {
        this.a = a;
    }

    public void setB(Integer b) {
        this.b = b;
    }

    public void setC(Date c) {
        this.c = c;
    }

    @Override
    public void saveCustomer() {
        System.out.println("业务调用持久层"+a+","+b+","+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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 通过配置文件给bean中的属性传值:使用set方法的方式
    涉及的标签:
        property
        属性:
            name:找的是类中set方法后面的部分
            ref:给属性赋值是其他bean类型的
            value:给属性赋值是基本数据类型和string类型的
    实际开发中,此种方式用的较多。
-->

    <bean id="customerService" class="com.edu.services.Impl.CustomerServiceImpl">
        <property name="a" value="com.edu.services"></property>
        <property name="b" value="3307"></property>
        <property name="c" ref="now"></property>
    </bean>
    <bean id="now" class="java.util.Date"></bean>

</beans>
运行结果

注入的三种数据类型

CustomerServiceImpl.java

package com.edu.services.Impl;

import com.edu.services.ICustomerService;

import java.util.*;


public class CustomerServiceImpl implements ICustomerService {

    private String[] myStrs;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String,String> myMap;
    private Properties myProps;

    public void setMyStrs(String[] myStrs) {
        this.myStrs = myStrs;
    }

    public void setMyList(List<String> myList) {
        this.myList = myList;
    }

    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }

    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }

    public void setMyProps(Properties myProps) {
        this.myProps = myProps;
    }

    @Override
    public void saveCustomer() {
        System.out.println(Arrays.toString(myStrs));
        System.out.println(myList);
        System.out.println(mySet);
        System.out.println(myMap);
        System.out.println(myProps);
    }
}

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

    <!-- 复杂类型
        结构相同,标签可以互换
    -->
    <bean id="customerService" class="com.edu.services.Impl.CustomerServiceImpl">
        <property name="myStrs">
            <array>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </array>
        </property>

        <property name="myList">
            <list>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </list>
        </property>

        <property name="mySet">
            <set>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </set>
        </property>

        <property name="myMap">
            <map>
                <entry key="testA" value="AAA"></entry>
                <entry key="testB" value="BBB"></entry>
                <entry key="testC" value="CCC"></entry>
            </map>
        </property>

        <property name="myProps">
            <props>
                <prop key="testD">DDD</prop>
                <prop key="testE">EEE</prop>
            </props>
        </property>
    </bean>

</beans>
运行结果
上一篇 下一篇

猜你喜欢

热点阅读