二、Spring IOC容器

(一)专题一 IOC

1、接口及面向接口编程

(1)接口

    <1>用于沟通的中介物的抽象化

    <2>实体把自己提供给外界的一种抽象化说明,用以由内部操作分离出外部沟通方法,使其能被修改内部而不影响外界其它实体与其交互的方式

    <3>对应Java接口即声明,声明了那些方法是对外公开提供的

    <4>在Java8中,接口可以拥有方法体

(2)面向接口编程

    <1>结构设计中,分清层次及调用关系,每层只向外(上层)提供一组功能接口,各层间仅依赖接口而非实现类

    <2>接口实现的变动不影响各层间的调用,这一点在公共服务中尤为重要

    <3>“面向接口编程”中的“接口”是用于隐藏具体实现和实现多态性的组件

扫描二维码关注公众号,回复: 8054699 查看本文章

    <4>例子   

           public interface OneInterface{

        String hello(String word);

}

          public class OneInterfaceImpl implements OneInterface{

      public String hello(String word){

      return "Word form interface \"OneInterface\":"+word;

}

}

          public static void main(String[]args){

     OneInterface oif = new OneInterfaceImpl();

     System.out.println(oif.hello("word."));

}

2、什么是IOC

(1)IOC:控制反转,控制权的转移,应用程序本身不负责依赖对象的创建和维护而是由外部容器负责创建和维护

(2)DI(依赖注入)是其一种实现方式

(3)目的:创建对象并且组装对象之间的关系

(4)扩展理解:2004年,Martin Fowler探讨了同一个问题,既然IOC是控制反转,那么到底是“那些方面的控制被反转了呢?”,经过详细地分析和论证后,

                    他得出了答案:“获得依赖对象的过程被反转了”。控制被反转之后,获得依赖对象的过程由自身管理变为了由IOC容器主动注入。于是,他给

                    “控制反转”取了一个更合适的名字叫做“依赖注入(Dependency Injection)”。他的这个答案,实际上给出了实现IOC的方法:注入。所谓依赖注入,

                    就是由IOC容器在运行期间,动态地将某种依赖关系注入到对象之中。

(5)IOC房屋中介

                            房屋中介                                                       IOC

                                    <1>找中介                                            <1>找IOC容器

                                    <2>中介介绍房子                                 <2>容器返回对象

                                    <3> 租房、入住                                    <3>使用对象

3、Spring的Bean配置

(1)刚才的接口在Spring中的配置方式

<?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 = "oneInterface" class = "com.imooc.ioc.interfaces.OneInterfaceImpl"></bean>

</beans>

4、Bean的初始化

(1)Bean容器初始化

<1>  org.springframework.beans

<2>  org.springframework.context

<3>  BeanFactory提供配置结构和基本功能,加载并初始化Bean

<4>  ApplicationContext保存了Bean对象并在Spring中被广泛使用

(2)方式,ApplicationContext

<1>本地文件

          FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("F:/workspace/appcontext.xml");

<2>Classpath

          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");

<3>Web应用中依赖servlet或Listener

          <listener>

               <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class>

          </listener>

                            <servlet>

                                <servlet-name> context </servlet-name>

                                <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class>

                                <load-on-startup> 1 </load-on-startup>

                            </servlet>

5、Spring的常用注入方式

(1)Spring注入是指在启动Spring容器加载bean配置的时候,完成对变量的赋值行为

(2)常用的两种注入方式

<1>设值注入

<?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 = "injectionService" class = "com.imooc.ioc.injection.service.InjectionServiceImpl">

<property name = "injectionDAO" ref = "injectionDAO"/>

</bean>

<bean id = "injectionDAO" class = "com.imooc.ioc.injection.dao.InjectionDAOImpl"></bean>

</beans>

<2>构造注入

<?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 = "injectionService" class = "com.imooc.ioc.injection.service.InjectionServiceImpl">

<constructor-arg name = "injectionDAO" ref = "injectionDAO"/>

</bean>

<bean id = "injectionDAO" class = "com.imooc.ioc.injection.dao.InjectionDAOImpl"></bean>

</beans>

猜你喜欢

转载自www.cnblogs.com/huabro/p/11974378.html