Spring实现Ioc的又法

一、IoC基础

  1.1、概念:

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

      IoC是相同种植设计思想。

    2、DI 依赖注入

      依赖注入是促成IoC的等同种植方式,也有人以为DI只是IoC的旁一样种植说法

    没有IoC的程序中我们下面向对象编程对象的缔造与对象中的仗关系完全硬编码在先后中,对象的创立由程序自己决定,控制反转后以目标的创建转移给第三着,所谓的支配反转就是:获得依靠对象的法反转了。**    **

XML 1

     IoC是Spring框架的核心内容,使用多艺术全面的落实了IoC,可以使用XML配置,也得以应用注解,新本子的spring也足以零配置实现IoC。spring容器在初始化时先读取配置文件,根据配置文件要长数据创建对象存入容器被,程序下时重从IoC容器中取出要之靶子。

XML 2

     采用XML方式安排Bean的时候,Bean的定义信息是同兑现分离之,而采取注解的办法可管二者合为一体,Bean的概念信息
直接为注解的样式定义在贯彻类似吃,从而达到了零配置的目的。

 

仲、使用XML配置的道贯彻IoC

  2.0、先创造一个maven项目 

XML 3

     2.1、Maven项目的pom.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、使用无参构造方法创建对象

    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、使用有参构造方法创建对象

    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.5、使用有参构造方法创建对象

    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>

    2.5.6、运行效果 XML 7


  2.6、通过性赋值创建对象

    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>

    2.6.5、运行效果:   

XML 8

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

 2.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>

    2.7.6、运行结果:

  XML 9


  2.8、对象的作用域–使用scope属性可以指定作用域

    2.8.1、从容器被取得回之目标默认是单例的:

       将2.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 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”给一个Bean定义多只目标实例的言传身教:

      1、将2.7演示中的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>

      2、将2.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);
    }
}

      3、运行结果如下:
–从结果吗false可以见到它们不再是单例,而是基本上实例。

XML 11

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

  2.9、延迟初始化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、回调方法

   3.0.1、初始化回调函数

     配置文件:–将2.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类: –将2.7饱受之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地图