JAVAEE -- spring02: Configure spring, sts plug-in, junit integration test, and aop demonstration using annotations, javaeespring02

Source: Internet
Author: User

JAVAEE -- spring02: Configure spring, sts plug-in, junit integration test, and aop demonstration using annotations, javaeespring02
1. Configure spring with annotation 1. Step

1.1 export package 4 + 2 + spring-aop

4 representative:

2 stands:

Log package: com.springsource.org. apache. commons. logging-1.1.1.jar

Optional: com.springsource.org. apache. log4j-1.2.15.jar)

1.2 introduce a new namespace (constraint) for the main configuration file)

1.3 enable annotation instead of configuration file

1.4 complete the configuration using annotations in the class

2. register the object to the container
// <Bean name = "user" class = "cn. itcast. bean. user "/> // @ Component (" user ") // @ Service (" user ") // service layer // @ Controller (" user ") // web layer @ Repository ("user") // dao Layer
3. modify the scope of the object
// Specify the Scope of the object @ Scope (scopeName = "singleton ")
4. Value Type Injection

Through the Field assignment of reflection, encapsulation is broken:

    @Value("tom")        private String name;

It is recommended to assign values using the set method .:

    @Value("tom")        public void setName(String name) {        this.name = name;    }
5. Injection of reference types
// @ Autowired // automatic assembly // problem: if multiple objects of the same type are matched. you cannot select which object to inject. // @ Qualifier ("car2") // use the @ Qualifier annotation to tell the spring container which object is the name of the private Car car automatically assembled;

Recommended Methods:

@ Resource (name = "car") // Manual injection, which specifies the object private Car car to be injected;
6. Initialization | destruction method
@ PostConstruct // call. init-method public void init () {System. out. println ("I am the initialization method! ") ;}@ PreDestroy // call. destory-method public void destory () {System. out. println (" I am the destroy method! ");}
Ii. STS plug-in 1. Manually install the plug-in (low success rate)

  

Step 1:

  

Step 2:

  

Step 3:

  

2. Install the plug-in eclipse directly using spring

 

Iii. Integration Testing of spring and junit 1. guide package 4 + 2 + aop + test 2. Configuration Annotation
// Create container @ RunWith (SpringJUnit4ClassRunner. class) // specifies the configuration file @ ContextConfiguration ("classpath: applicationContext. xml ") public class Demo {// inject the object named user into the u variable @ Resource (name =" user ") private User u;
3. Test
    @Test    public void fun1(){                System.out.println(u);            }

 

Iv. Introduction to the idea of aop in spring 1.

2. aop concept in spring

3. How spring implements aop

3.1 dynamic proxy (priority)

An interface must be implemented to generate a proxy object. If no interface is available, dynamic proxy technology cannot be used.

3.2 cglib proxy (no interface)

Third-party proxy technology, cglib proxy. You 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, then this class cannot be cglib proxy.

4. aop noun Learning

  

 

5. aop demonstration in spring 1. Step (xml configuration) 1.1 guide package 4 + 2

Spring aop package:

Spring-aspects-4.2.4.RELEASE.jar

Spring-aop-4.2.4.RELEASE.jar

Spring requires a third-party aop package:

Com.springsource.org. aopalliance-1.0.0.jar

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

1.2 prepare the target object
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 a user! ");}}
1.3 preparation notice
// Notification class public class MyAdvice {// pre-notification // |-call the post-notification before the target method runs (if an exception occurs, the post-notification will not be called) // |-called after the target method is run // surround notification // |-called before and after the target method // exception interception notification // |-if an exception occurs, the post-notification will be called. // The post-notification will be called no matter whether an exception occurs or not. // |-call the post-notification after the target method is run. // the pre-notification public void before () {System. out. println ("this is a pre-notification !! ");} // Post notification public void afterReturning () {System. out. println (" this is post notification (if an exception occurs, it will not be called )!! ");} // The surround notification public Object around (ProceedingJoinPoint pjp) throws Throwable {System. out. println (" This is part before the surround notification !! "); Object proceed = pjp. proceed (); // call the target method System. out. println (" This is part after the notification !! "); Return proceed;} // public void afterException () {System. out. println (" an error occurred! An exception occurred !! ");} // Post-Notification public void after () {System. out. println (" this is post-notification (it will be called if an exception occurs )!! ");}}
1.4 configure to weave 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/conte Xt 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 "> <! -- Preparations: import the aop (constraint) namespace --> <! -- 1. Configure the target object --> <bean name = "userService" class = "cn. itcast. service. UserServiceImpl"> </bean> <! -- 2. Configure the notification object --> <bean name = "myAdvice" class = "cn. itcast. d_springaop.MyAdvice"> </bean> <! -- 3. Configure to route notifications to the target object --> <aop: config> <! -- Configure the 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 "> <! -- Specify the before method as the pre-notification --> <aop: before method = "before" pointcut-ref = "pc"/> <! -- Post --> <aop: after-returning method = "afterReturning" pointcut-ref = "pc"/> <! -- Surround notification --> <aop: around method = "around" pointcut-ref = "pc"/> <! -- Exception interception notification --> <aop: after-throwing method = "afterException" pointcut-ref = "pc"/> <! -- Post --> <aop: after method = "after" pointcut-ref = "pc"/> </aop: aspect> </aop: config> </beans>
2. Step 2, step 3 (annotation configuration) preceding step 2 and Step 3 are woven in the same way as the xml configuration. The notification is woven 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/conte Xt 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 "> <! -- Preparations: import the aop (constraint) namespace --> <! -- 1. Configure the target object --> <bean name = "userService" class = "cn. itcast. service. UserServiceImpl"> </bean> <! -- 2. Configure the notification object --> <bean name = "myAdvice" class = "cn. itcast. e_annotationaop.MyAdvice"> </bean> <! -- 3. Enable annotation-based weaving --> <aop: aspectj-autoproxy> </beans>

Notification Type:

// Notification class @ Aspect // indicates that this class is a notification class public class MyAdvice {@ Pointcut ("execution (* cn. itcast. service. * ServiceImpl. *(..)) ") public void pc () {}// pre-notification // specifies that this method is a pre-notification and specifies the entry point @ Before (" MyAdvice. pc () ") public void before () {System. out. println ("this is a pre-notification !! ");} // Post notification @ AfterReturning (" execution (* cn. itcast. service. * ServiceImpl. *(..)) ") public void afterReturning () {System. out. println ("this is a post-notification (it will not be called if an exception occurs )!! ");} // Surround notification @ Around (" execution (* cn. itcast. service. * ServiceImpl. *(..)) ") public Object around und (ProceedingJoinPoint pjp) throws Throwable {System. out. println ("This is part before the notification !! "); Object proceed = pjp. proceed (); // call the target method System. out. println (" This is part after the notification !! "); Return proceed;} // exception notification @ AfterThrowing (" execution (* cn. itcast. service. * ServiceImpl. *(..)) ") public void afterException () {System. out. println ("an accident! An exception occurred !! ");} // Post notification @ After (" execution (* cn. itcast. service. * ServiceImpl. *(..)) ") public void after () {System. out. println ("this is a post-notification (it will also be called if an exception occurs )!! ");}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.