JAVAEE - spring 02: Configuring spring, sts plug-ins, junit integration testing and aop demonstration with annotations

Keywords: Java Spring xml encoding Junit

Configuring spring with annotations

1. Steps

1.1 Guide 4+2+spring-aop

1.2 Introducing new namespaces (constraints) for the main configuration file

1.3 Turn on the use of annotations instead of configuration files

1.4 Configuration using annotations in classes

2. Register objects into containers

//<bean name="user" class="cn.itcast.bean.User"  />
//@Component("user")
//    @Service("user") // service layer
//    @Controller("user") // web layer
    @Repository("user")// dao layer

3. Scope of Modifying Objects

//The scope of the specified object
@Scope(scopeName="singleton")

4. Value type injection

The encapsulation is destroyed by reflective Field assignment:

    @Value("tom")    
    private String name;

It is recommended to use the set method to assign values.

    @Value("tom")    
    public void setName(String name) {
        this.name = name;
    }

5. Reference type injection

    //@Autowired //automatic assembly
    //problem:If multiple objects of the same type are matched.You will not be able to choose which object to inject..
    //@Qualifier("car2")//Use@Qualifier Annotate spring Containers automatically assemble objects of which name
    private Car car;

Recommendation:

    @Resource(name="car")//Manual injection,Specify which name to inject into the object
    private Car car;

6. Initialization | Destruction Method

    @PostConstruct //Called after an object is created.init-method
    public void init(){
        System.out.println("I'm the initialization method.!");
    }
    @PreDestroy //Called before destruction.destory-method
    public void destory(){
        System.out.println("I'm the way to destroy it.!");
    }

 

II. STS Plug-in

1. Manual Installation of Plug-ins (Low Success Rate)

  

Step 1:

  

Step 2:

  

Step 3:

  

2. Elipse to install plug-ins directly with spring

   

 

3. spring and junit Integration Testing

1. Guide 4+2+aop+test

2. Configuration Notes

//Help us create containers
@RunWith(SpringJUnit4ClassRunner.class)
//Specify which configuration file to use when creating containers
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
    //Name it user Object injection into u Variable
    @Resource(name="user")
    private User u;

3. Testing

    @Test
    public void fun1(){
        
        System.out.println(u);
        
    }

 

4. aop in spring

1. Introduction to AOP

  

  

  

2. The concept of aop in spring

  

3. The Principle of Spring Implementing aop

3.1 Dynamic proxy (priority)

Proxy objects must implement interfaces in order to generate proxy objects. Without interfaces, dynamic proxy technology cannot be used.

3.2 cglib proxy (no interface)

Third-party proxy technology, cglib proxy, can generate proxy for any class. The principle of proxy is to inherit proxy for the target object. If the target object is modified by final, the class can not be proxyed by cglib.

4.aop noun learning

  

 

5. aop Demonstration in spring

1. Steps (xml configuration)

1.1 Guide 4+2

spring's aop package:

    spring-aspects-4.2.4.RELEASE.jar

    spring-aop-4.2.4.RELEASE.jar

spring requires third-party aop packages:

    com.springsource.org.aopalliance-1.0.0.jar

    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

1.2 Preparing target objects

public class UserServiceImpl implements UserService {
    @Override
    public void save() {
        System.out.println("Save user!");
        //int i = 1/0;
    }
    @Override
    public void delete() {
        System.out.println("delete user!");
    }
    @Override
    public void update() {
        System.out.println("Update user!");
    }
    @Override
    public void find() {
        System.out.println("Find user!");
    }
}

1.3 Preparatory Notice

//Notification class
public class MyAdvice {
    
    //Before advice    
//        |-Called before the target method runs
    //after returning advise(If an exception occurs, it will not be called)
//        |-Called after the target method runs
    //Around Advice
//        |-Called before and after the target method
    //Abnormal interception notification
//        |-If an exception occurs,Would call
    //after returning advise(Called regardless of whether an exception occurs)
//        |-Called after the target method runs
//----------------------------------------------------------------
    //Before advice
    public void before(){
        System.out.println("This is the advance notice.!!");
    }
    //after returning advise
    public void afterReturning(){
        System.out.println("This is a post notice.(If an exception occurs, it will not be called)!!");
    }
    //Around Advice
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("This is the part before the circular notification.!!");
        Object proceed = pjp.proceed();//Call the target method
        System.out.println("This is the part after the circular notification.!!");
        return proceed;
    }
    //Exception notification
    public void afterException(){
        System.out.println("Something happened.!There's an anomaly.!!");
    }
    //after returning advise
    public void after(){
        System.out.println("This is a post notice.(Exceptions will also be invoked)!!");
    }
}

1.4 configuration for weaving, weaving notifications into the target object

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

<!-- Dead work: Import aop(constraint)Namespace -->
<!-- 1.Configuring target objects -->
    <bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean>
<!-- 2.Configuration Notification Object -->
    <bean name="myAdvice" class="cn.itcast.d_springaop.MyAdvice" ></bean>
<!-- 3.Configuration weaves notifications into the target object -->
    <aop:config>
        <!-- Configuration entry point 
            public void cn.itcast.service.UserServiceImpl.save() 
            void cn.itcast.service.UserServiceImpl.save()
            * cn.itcast.service.UserServiceImpl.save()
            * cn.itcast.service.UserServiceImpl.*()
            
            * cn.itcast.service.*ServiceImpl.*(..)
            * cn.itcast.service..*ServiceImpl.*(..)
        -->
        <aop:pointcut expression="execution(* cn.itcast.service.*ServiceImpl.*(..))" id="pc"/>
        <aop:aspect ref="myAdvice" >
            <!-- Designated name is before Method as pre-notification -->
            <aop:before method="before" pointcut-ref="pc" />
            <!-- Postposition -->
            <aop:after-returning method="afterReturning" pointcut-ref="pc" />
            <!-- Around Advice -->
            <aop:around method="around" pointcut-ref="pc" />
            <!-- Abnormal interception notification -->
            <aop:after-throwing method="afterException" pointcut-ref="pc"/>
            <!-- Postposition -->
            <aop:after method="after" pointcut-ref="pc"/>
        </aop:aspect>
    </aop:config>
</beans>

2. Steps (Annotation Configuration)

The previous steps 1, 2, 3 are the same as the xml configuration

2.4 configuration for weaving, weaving notifications into the target object

   applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

<!-- Dead work: Import aop(constraint)Namespace -->
<!-- 1.Configuring target objects -->
    <bean name="userService" class="cn.itcast.service.UserServiceImpl" ></bean>
<!-- 2.Configuration Notification Object -->
    <bean name="myAdvice" class="cn.itcast.e_annotationaop.MyAdvice" ></bean>
<!-- 3.Open and complete weaving with annotations -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

Notification category:

//Notification class
@Aspect
//Represents that this class is a notification class
public class MyAdvice {
    @Pointcut("execution(* cn.itcast.service.*ServiceImpl.*(..))")
    public void pc(){}
    //Before advice
    //Specify that this method is a pre-notification,And develop entry points
    @Before("MyAdvice.pc()")
    public void before(){
        System.out.println("This is the advance notice.!!");
    }
    //after returning advise
    @AfterReturning("execution(* cn.itcast.service.*ServiceImpl.*(..))")
    public void afterReturning(){
        System.out.println("This is a post notice.(If an exception occurs, it will not be called)!!");
    }
    //Around Advice
    @Around("execution(* cn.itcast.service.*ServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("This is the part before the circular notification.!!");
        Object proceed = pjp.proceed();//Call the target method
        System.out.println("This is the part after the circular notification.!!");
        return proceed;
    }
    //Exception notification
    @AfterThrowing("execution(* cn.itcast.service.*ServiceImpl.*(..))")
    public void afterException(){
        System.out.println("Something happened.!There's an anomaly.!!");
    }
    //after returning advise
    @After("execution(* cn.itcast.service.*ServiceImpl.*(..))")
    public void after(){
        System.out.println("This is a post notice.(Exceptions will also be invoked)!!");
    }
}

Posted by y4m4 on Sun, 06 Jan 2019 00:51:09 -0800