Spring (DI, AOP) understanding (1)

I feel that my spring does not understand well. So I started learning again.

This article is mainly to understand DI (dependency injection), Aop (cutting aspect)

1.DI (Dependency injection, no comments are involved here. Just use the xml file and Bean method to register pojo,)

   Dependency injection is to give the spring framework (control inversion) the right to create a bean object and then use Applicationcontext.getBean () to get the object.

   Container: The spring container creates objects, manages the objects, and is responsible for managing the life cycle of the objects. There are two types of BeanFactiory in the container .. ApplicationContext. Many will not be explained, BeanFactiory is relatively low, and ApplicaitonContext is used here

   ApplicaitonContext:

         1 FileSystemXmlApplicationContext: Create an ApplicationContext instance with an XML configuration file based on the file system.

         2 ClassPathXmlApplicationContext: ApplicationContext instance created with the XML configuration file under the class loading path

         3 XmlWebApplicationContext: load and create an ApplicationContext instance from one or more xml configuration files under the web application

         4 AnnotationConfigApplicationContext loads Spring ApplicaitonContext instances from one or more java-based configuration classes

         5 AnnotationConfigWebApplicationContext creates a spring web application context based on one or more java configuration classes

 2. Aop: Aspect-oriented programming. (Extract highly reusable modules, and then configure the extracted modules (such as log modules) to cover other services that require log modules)

       A project assumes that there are instructor services, student services, billing services ... and also log module, security module, transaction module

       Instructor service, student service, billing service, each service needs to be coupled with the log module, security module, transaction module ... If this is the case, the code in each service cannot focus on solving the problem of this service . Need to call the log module, security module, transaction module code.

       In this way, the reusability of the code will be low and the coupling will be high.

 Create a Person class ... there is a doSomething method

package com.start.demo;
/**
 * 男孩,女孩,工程师,程序员....
 */
public class Person {
    private String name;
    private int age;
    private Play play;

    public Person(Play p) {
        this.play = p;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    void doSomething() {
        System.out.println(" we can  play " + play.getType() +"together");
    }
}

   Create a Play class type to indicate what can be done

package com.start.demo; 

/ ** 
 * Various activities, play games, read books, watch TV ... 
 * / 
public  class Play {
     private String type = "computer game" ; 

    public String getType () {
         return type; 
    } 

    public  void setType (String type) {
         this .type = type; 
    } 
}

    Test a class of Aop

package com.start.demo; 

/ ** 
 * This class is used to test Aop. Before and after the cut point, the corresponding methods are called respectively 
 * / 
public  class Asker {
     private String name;
      void doSomethingBefore () { 
        System.out. println ( "what can we do" ); 
    } 
    void   doSomethingAfter () { 
        System.out.println ( "that's fine" ); 
    } 
}

 

Assembly 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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
     <!--装配bean-->
    <bean id="person" class="com.start.demo.Person">
        <constructor-arg ref="play"/>
    </bean>
    <bean id="play" class="com.start.demo.Play"/>
    <bean id="asker" class="com.start.demo.Asker"/>
    <aop:config>
        <aop:aspect ref="asker">
            <!-- pointcut 的id 不能与 配置切点bean的id一样了,会报错.-->
            <aop:pointcut id="personPointcut"
                          expression="execution(* *.doSomething())"/><!--execution(* * .doSomething(..)) 要是有参数,需要在方法体里面加 ..-->
            <aop:before pointcut-ref="personPointcut" method="doSomethingBefore"/>
            <aop:after pointcut-ref="personPointcut" method="doSomethingAfter"/>
        </aop:aspect>
    </aop:config>

</beans>

  Assemble objects with javaBean

package com.start.confBean;

import com.start.demo.Person;
import com.start.demo.Play;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PersonConf {
    @Bean
    public Person person() {
        return new Person(play());
    }

    @Bean
    public Play play() {
        return new Play();
    }
}

Test the Main class

   

package com.start.demo;

import com.start.confBean.PersonConf;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestMain {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext appcontext = new ClassPathXmlApplicationContext("beans.xml");    //从beans.xml 文件获取ApplicaitonContext 
//        AnnotationConfigApplicationContext annotationApplicationContext = new AnnotationConfigApplicationContext(PersonConf.class);// The PersonConf.java file gets the ApplicaitonContext 
 //         Person person1 = annotationApplicationContext.getBean (Person.class);   // Get the bean and directly specify the bean as Person
 //         Person person = (Person) appcontext.getBean ("person"); / / Get the bean person as the id 
        Person pers in the configuration file = (Person) appcontext.getBean ("person" ); 
        pers.doSomething (); 
    } 
}

Although these codes are very simple, they let me understand the role of DI and Aop

Guess you like

Origin www.cnblogs.com/ly12/p/12722110.html