Two, Spring IOC container

(A) a thematic IOC

1, and the interface for programming interface

(1) Interface

    Abstraction <1> for communication of the intermediary

    <2> to provide their own entity to the outside of one kind of abstract description, a method for separating the internal and external communications operations, so that the interior can be modified without affecting the way interact with other entities outside

    <3> corresponding to the Java interface that is declaration that those methods are open to the public provided

    <4> In Java8, can have an interface method thereof

(2) oriented programming interface

    <1> in the structural design, and to distinguish between call relationship hierarchy, each layer only outward (upper layer) provides a set of functional interfaces, interfaces between the layers relies not only implementation class

    <2> The change does not affect the calling interface between the layers, which is particularly important in public service

    <3> "Oriented Programming Interface" in the "interface" is used to hide implementation and realization polymorphism assembly

    <4> Examples   

           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. What is the IOC

(1) IOC: inversion of control, control of the transfer, dependent on the application itself is not responsible for creating and maintaining objects but by the creation and maintenance of the external container

(2) DI (DI) which is one implementation

(3) Objective: To create objects and relationships between objects assembled

(4) Extended understand: In 2004, Martin Fowler discusses the same problem, since the IOC is Inversion of Control, so in the end is, after detailed analysis and demonstration through the "control those aspects of it were reversed?"

                    He got the answer: "the process of obtaining dependent object is reversed." After the control is reversed, the process of obtaining their dependent objects from the active management becomes injected from the IOC container. So he

                    "Inversion of control" takes a more appropriate name is "dependency injection (Dependency Injection)". His answer, in fact, shows how to achieve the IOC: the injection. The so-called dependency injection,

                    IOC is the container during operation, a certain dependency dynamically injected into the object.

 

(5) IOC estate agents

                            IOC estate agents

                                    <1> to find an intermediary <1> Get IOC container

                                    <2> an intermediary house <2> Container Object Returns

                                    <3> rent, stay <3> using the object

 

3, Spring's Bean Configuration

(1) The interface configuration mode just in 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 initialization

(1) Bean initialization container

<1>  org.springframework.beans

<2>  org.springframework.context

<3> BeanFactory provide basic arrangement and functions, loads and initializes Bean

<4> ApplicationContext save the Bean object and is widely used in Spring

 

(2) mode, ApplicationContext

 

<1> Local File

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

 

<2>Classpath

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

 

<3> Web application-dependent or 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 common mode injection

 

(1) Spring injection refers to the start time Spring bean container load configuration, complete assignments behavior variables

 

(2) Two commonly used injection method

<1> set value of the injected

<?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> configured injection

<?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>

Guess you like

Origin www.cnblogs.com/huabro/p/11974378.html