Spring达成Ioc的各个情势

一、IoC基础

  1.1、概念:

    一、IoC 控制反转(Inversion of
Control)

      IoC是壹种设计思想。

    2、DI 正视注入

      正视注入是贯彻IoC的1种艺术,也有人觉得DI只是IoC的另一种说法

    未有IoC的先后中我们运用面向对象编制程序对象的创始与指标间的注重性关系完全硬编码在程序中,对象的成立由程序本身决定,控制反转后将目的的创办转移给第叁方,所谓的主宰反转就是:获得依靠对象的诀要反转了。**    **

XML 1

     IoC是Spring框架的宗旨内容,使用各种措施周到的落到实处了IoC,能够使用XML配置,也得以应用评释,新版的spring也足以零配置达成IoC。spring容器在初叶化时先读取配置文件,依照计划文件或元数据创造对象存入容器中,程序选取时再从IoC容器中取出须求的靶子。

XML 2

     选取XML格局配置Bean的时候,Bean的定义务消防队息是和实现分离的,而选用表明的法子得以把二者合为一体,Bean的定义音讯间接以注解的花样定义在贯彻类中,从而落成了零配置的目标。

 

二、使用XML配置的章程贯彻IoC

  二.0、先创立多少个maven项目 

XML 3

     2.1、Maven项目标pom.xml配置如下:**   *XML,*

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zhangneng</groupId><!--组织名-->
    <artifactId>springProject</artifactId><!--项目名-->
    <version>1.0-SNAPSHOT</version><!--版本号-->
    <packaging>jar</packaging>
    <name>Spring051</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.0.RELEASE</spring.version>
    </properties>
    <dependencies>
        <!--下面是本项目的依赖jar包   去这个网站:http://mvnrepository.com-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <!--如果要全局使用则删掉下面这句-->
            <!--
            <scope>test</scope>
            -->
            <version>4.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version><!--一赖上文中的version-->
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.2.4</version>
        </dependency>
    </dependencies>
</project>

 ————————————————————————————————————————————————————————————————————————————

  2.2、常规的

    2.2.1、IBookDao.java   

package spring02;

/**图书数据访问接口*/
public interface IBookDao {
    /**添加新书*/
    void save(String name);
}

    2.2.2、MySQLBookDao.java    

package spring02;
/**接口的实现类*/
public class MySQLBookDao implements IBookDao {
    public void save(String name){
        System.out.println("添加图书"+name+"到MySQL数据库成功!");
    }
}

    2.2.3、OracleBookDao.java

package spring02;
/**接口的实现类*/
public class OracleBookDao implements IBookDao {
    public void save(String name){
        System.out.println("添加图书"+name+"到Oracle数据库成功!");
    }
}

    2.2.4、BookService.java

package spring02;

public class BookService {
    IBookDao bookDao;
    public BookService(){
       bookDao = new MySQLBookDao();
       //bookDao = new OracleBookDao();
    }
    /**新增一本书*/
    public void addNewBook(){
        String bookname="《Spring MVC学习指南》";
        bookDao.save(bookname);
    }
}

    2.2.5、测试类 BookServiceTest.java  

package spring02;

import org.junit.Test;

import static org.junit.Assert.*;

/**不用配置文件实现spring01里的效果,通过改变BookService中的BookService()方法实现*/
/**这样的弊端就是当代码部署到服务器上之后就不能进行控制了,所以我们弄了spring01的方式,通过配置文件来控制使用哪种数据库*/
public class BookServiceTest {
    BookService service = new BookService();
    @Test
    public void addNewBook() throws Exception {
        service.addNewBook();
    }

}

    2.2.6、效果:

    XML 4

————————————————————————————————————————————————————————————————————————————– 

  二.三、使用无参构造方法创设对象

    2.3.1、IBookDao.java    

package spring01;

/**图书数据访问接口*/
public interface IBookDao {
    /**添加新书*/
    void save(String name);
}

    2.3.2、MySQLBookDao.java

package spring01;
/**IBookDao的实现类*/
public class MySQLBookDao implements IBookDao {
    /**保存一本书到MySQL数据库*/
    public void save(String name){
        /*这里将处理与MySQL数据库连接的业务功能代码*/
        System.out.println("添加图书"+name+"到MySQL数据库成功!");
    }
}

    2.3.3、OracleBookDao.java

package spring01;

/**IBookDao的实现类*/
public class OracleBookDao implements IBookDao {
    /**保存一本书到Oracle数据库*/
    public void save(String name){
        /*这里将处理与Oracle数据库连接的业务功能代码*/
        System.out.println("添加图书"+name+"到Oracle数据库成功!");
    }
}

    2.3.4、BookService.java    

package spring01;

import javafx.application.Application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BookService {
    /**创建一个IBookDao对象属性bookDao*/
    /**bookDao一定要定义成对象类型,只有对象类型才可以接收*/
    IBookDao bookDao;

    /**通过无参构造方法来获取容器*/
    public BookService(){
        //创建容器
        ApplicationContext ctx=new ClassPathXmlApplicationContext("bookiocbean01.xml");
        /*
            从容器(bookiocbean01.xml)中获得id为bookdao的bean
            bookdao来自bookiocbean01.xml中的id属性的值
        */
        //通过IBook接口的bookDao属性来接收容器中的接口实现类
        //这里使用强转的形式实现,其实这样是不安全的。
        bookDao = (IBookDao) ctx.getBean("bookdao");
    }
    /**新增一本书*/
    public void addNewBook(){
        String bookname="《Spring MVC学习指南》";
        /**将书籍保存*/
        /*调用IBookDao对象的save()方法保存一本书*/
        bookDao.save(bookname);
    }
}

    2.3.5、测试类 BookService.java  

package spring01;

import org.junit.Test;

import static org.junit.Assert.*;

/**通过配置文件的形式来决定使用哪种数据库*/
public class BookServiceTest {
    BookService service = new BookService();
    //测试一下BookService中的addNewBook()方法,看是否可以成功添加书籍
    @Test
    public void addNewBook() throws Exception {
        service.addNewBook();
    }
}

    2.3.6、bookiocbean01.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">
    <!--将使用Oracle数据库 实现新增书籍接口的实现类 导入到这个配置文件(容器)中,且id属性值设置为bookdao,然后去BookService中创建这个容器-->
    <!--使用无参构造方法-->
    <bean id="bookdao" class="spring01.OracleBookDao"></bean>
</beans>

     2.3.7、效果:

XML 5

 


  二.肆、使用有参构造方法创立对象

    2.4.1、Person.java   

package spring03;
/**人*/
public abstract class Person {
    /**姓名*/
    public String name;
}

    2.4.2、Student.java

package spring03;

/**学生*/
public class Student extends Person{
    /**身高*/
    public int height;
    /**有参构造方法*/
    public Student(String name,int height){
        this.name=name;
        this.height=height;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", height=" + height +
                '}';
    }
}

    2.4.3、Person.java  

package spring03;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class School {
    public static void main(String[] args) {
        //ioc容器
        ApplicationContext ctx  =
                new ClassPathXmlApplicationContext("beans02.xml","bookiocbean01.xml");
        //从容器中获取对象
        Person tom = ctx.getBean("tom",Person.class);
        System.out.println(tom);
    }
}

    2.4.4、beans02.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="tom" class="spring03.Student">
        <constructor-arg name="name" value="张百川"></constructor-arg>
        <constructor-arg name="height" value="195"></constructor-arg>
    </bean>
</beans>

    2.4.5、运营结果:    XML 6

————————————————————————————————————————————————————————————————————————————– 

 2.伍、使用有参构造方法创设对象

    2.5.1、person.java    

package spring04;

/**人*/
public abstract class Person {
    /**姓名*/
    public String name;
    /**地址*/
    public Address address;
}

    2.5.2、Student.java  

package spring04;

/**
 * Created by Admin on 2017/11/6.
 */

/**学生*/
public class Student extends Person {
    /**身高*/
    public int height;
    /**有参构造方法*/
    public Student(String name,int height){
        this.name=name;
        this.height=height;
    }

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

    2.5.3、Address.java  

package spring04;

/**
 * Created by Admin on 2017/11/6.
 */
/**地址*/
public class Address {
    /**国家*/
    private String country;
    /**城市 */
    private String city;

    public Address() {
    }

    public Address(String country, String city) {
        this.country = country;
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

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

    2.5.4、School.java    

package spring04;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class School {
    public static void main(String[] args) {
        //ioc容器
        ApplicationContext ctx  =
                new ClassPathXmlApplicationContext("beans03.xml","bookiocbean01.xml");
        //从容器中获取对象
        Person rose = ctx.getBean("rose", Person.class);
        System.out.println(rose);
    }
}

    2.5.5、beans03.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="rose" class="spring04.Student">
        <constructor-arg index="0" value="张柏芝"></constructor-arg>
        <constructor-arg index="1" value="196"></constructor-arg>
    </bean>

</beans>

    贰.5.陆、运转效果 XML 7


  二.陆、通过品质赋值创立对象

    2.6.1、Person.java   

package spring05;

/**人*/
public abstract class Person {
    /**姓名*/
    public String name;
}

    2.6.2、Student.java    

package spring05;

/**学生*/
public class Student extends Person {
    /**身高*/
    public int height;
    /**有参构造方法*/
    public Student(String name,int height){
        this.name=name;
        this.height=height;
    }

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

    2.6.3、School.java   

package spring05;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

        //ioc容器
        ApplicationContext ctx  =
                new ClassPathXmlApplicationContext("beans04.xml","bookiocbean01.xml");
        //从容器中获取对象
        Person rose = ctx.getBean("rose",Person.class);
        Address zhuhai = ctx.getBean("zhuhai",Address.class);
        System.out.println(zhuhai);
        System.out.println(rose);
    }
}

    2.6.4、beans04.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="rose" class="spring05.Student">
        <constructor-arg index="0" value="张柏芝"></constructor-arg>
        <constructor-arg index="1" value="196"></constructor-arg>
    </bean>
    <bean name="zhuhai" class="spring05.Address">
        <property name="country" value="中国"></property>
        <property name="city" value="珠海"></property>
    </bean>
</beans>

    二.陆.五、运维效果:   

XML 8

————————————————————————————————————————————————————————————————————————————- 

 二.7、使用对象引用的诀要创设对象

    2.7.1、Person.java   

package spring06;

/**人*/
public abstract class Person {
    /**姓名*/
    public String name;
    /**地址*/
    public Address address;

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

    2.7.2、Student.java 

package spring06;

/**学生*/
public class Student extends Person {
    /**身高*/
    public int height;

    /**有参构造方法*/
    public Student(String name,int height){
        this.name=name;
        this.height=height;
    }
    /**有参构造方法*/
    public Student(String name,int height,Address address){
        this.name=name;
        this.height=height;
        this.address=address;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", height=" + height +
                '}'+address;
    }
}

    2.7.3、Address.java   

package spring06;

/**地址*/
public class Address {
    /**国家*/
    private String country;
    /**城市 */
    private String city;

    public Address() {
    }

    public Address(String country, String city) {
        this.country = country;
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

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

    2.7.4、School.java   

package spring06;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class School {
    public static void main(String[] args) {
        /**ioc容器*/
        ApplicationContext ctx  =
                new ClassPathXmlApplicationContext("beans05.xml","bookiocbean01.xml");
        //从容器中获取对象
        Person tom = ctx.getBean("tom",Person.class);
        Person rose = ctx.getBean("rose",Person.class);
        //Address zhuhai = ctx.getBean("zhuhai", Address.class);
        System.out.println(tom);
        System.out.println(rose);
    }
}

    2.7.5、beans05.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">
    <!--可以使用id或(和)name属性来指定bean的标识符-->
    <bean name="zhuhai" class="spring06.Address">
        <property name="country" value="中国"></property>
        <property name="city" value="珠海"></property>
    </bean>
    <bean id="tom" class="spring06.Student">
        <constructor-arg name="name" value="张柏川"></constructor-arg>
        <constructor-arg name="height" value="195"></constructor-arg>
        <constructor-arg name="address" ref="zhuhai"></constructor-arg>
    </bean>
    <bean id="rose" class="spring06.Student">
        <constructor-arg index="0" value="张柏芝"></constructor-arg>
        <constructor-arg index="1" value="196"></constructor-arg>
        <constructor-arg index="2" ref="zhuhai"></constructor-arg>
    </bean>
</beans>

    二.7.6、运维结果:

  XML 9


  二.八、对象的成效域–使用scope属性能够钦命成效域

    二.8.壹、从容器中取回的对象暗中认可是单例的:

       将贰.⑦的实例中的School.java类的代码改为下列代码则能够验证其默许为单例方式:   

package spring06;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class School {
    public static void main(String[] args) {
        /**ioc容器*/
        ApplicationContext ctx  =
                new ClassPathXmlApplicationContext("beans05.xml","bookiocbean01.xml");
        //从容器中获取对象
        Person rose1 = ctx.getBean("rose",Person.class);
        Person rose2 = ctx.getBean("rose",Person.class);

        System.out.println(rose1==rose2);//这句代码输出的结果为true证明默认为单例模式
        System.out.println(rose1!=rose2);
    }
}

    2.8.2、作用域(利用scope属性可以内定功用域

XML 10

    用scope=”prototype”给1个Bean定义七个指标实例的以身作则:

      一、将2.七示范中的beans05.xml中的内容改为如下代码:   

   <bean id="rose" class="spring02.Student" scope="prototype">
        <constructor-arg index="0" value="张柏芝"></constructor-arg>
        <constructor-arg index="1" value="196"></constructor-arg>
        <constructor-arg index="2" ref="zhuhai"></constructor-arg>
    </bean>

      贰、将二.7演示中的School.java类中的代码改为如下代码:      

package spring06;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class School {
    public static void main(String[] args) {
        /**ioc容器*/
        ApplicationContext ctx  =
                new ClassPathXmlApplicationContext("beans05.xml","bookiocbean01.xml");
          //从容器中获取对象
        Person tom=ctx.getBean("tom",Person.class);
        Person roseA=ctx.getBean("rose",Person.class);
        Person roseB=ctx.getBean("rose",Person.class);
        //Address zhuhai=ctx.getBean("zhuhai",Address.class);
        System.out.println(tom);
        System.out.println(roseA==roseB);
    }
}

      三、运维结果如下:
–从结果为false能够看到它不再是单例,而是多实例。

XML 11

————————————————————————————————————————————————————————————————————————————– 

  贰.玖、延迟初阶化Bean

     ApplicationContext落到实处的私下认可行为即使再运维时将富有 singleton bean提前实行实例化。
平时那样的超前实例化格局是好事,因为安排中要么运转环境的失实就会被当即发现,不然大概要花多少个钟头甚至几天。如若你不想
那样,你能够将单例bean定义为延迟加载幸免它提前实例化。延迟起头化bean会告诉Ioc容器在首先次索要的时候才实例化而不是在容器运营时就实例化。在XML配置文件中,延迟初叶化通过<bean/>元素的lazy-init质量实行控制,比如:

<!--延迟加载-->
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
<!--不延迟加载-->
<bean name="not.lazy" class="com.foo.AnotherBean"/>

———————————————————————————————————————————————————————————————————————————— 

  3.0、回调方法

   三.0.一、初叶化回调函数

     配置文件:–将二.7中的配置文件改为下边包车型地铁样子**   **

   <bean id="tom" class="spring02.Student" init-method="init" destroy-method="over">
        <constructor-arg name="name" value="张柏川"></constructor-arg>
        <constructor-arg name="height" value="195"></constructor-arg>
        <constructor-arg name="address" ref="zhuhai"></constructor-arg>
    </bean>

      Student类: –将二.七中的student类改为下边包车型地铁规范     

package spring02;

import java.io.File;

/**学生*/
public class Student extends Person {
    /**身高*/
    public int height;
    /**有参构造方法*/
    public Student(String name,int height){
        this.name=name;
        this.height=height;
    }
    /**有参构造方法*/
    public Student(String name,int height,Address address){
        this.name=name;
        this.height=height;
        this.address=address;
    }
    @Override
    public String toString() {
        return "Student{" + "height=" + height+",name="+name +'}'+address;
    }

    public void init(){
        System.out.println("对象被创建");
    }
    public void over(){
        System.out.println("对象被回收");
    }

}

      运转结果: 

XML 12

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

    

相关文章

网站地图xml地图