Spring annotation Learning (combination of ioc and aop)

Source: Internet
Author: User

First introduce the jar package

Aspectjrt. jar

Aspectjweaver. jar

1. dao

package com.dao;public interface OkpDao {public void save();public void update();}

2. impl

package com.dao.impl;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;import com.dao.OkpDao;@Component("okpDaoImpl")public class OkpDaoImpl implements OkpDao{public void save() {System.out.println("OkpDaoImpl.save()");}public void update() {System.out.println("OkpDaoImpl.update()");}}

3. service

Package com. service; import javax. annotation. postConstruct; import javax. annotation. preDestroy; import javax. annotation. resource; import org. springframework. beans. factory. annotation. autowired; import org. springframework. beans. factory. annotation. qualifier; import org. springframework. stereotype. component; import com. dao. okpDao; @ Componentpublic class OkpService {private OkpDao okpDao; public OkpDao getOkpDao () {return okpDao;} @ Resource (name = "okpDaoImpl") public void setOkpDao. okpDao = okpDao;} public void save () {this. okpDao. save ();} public void update () {this. okpDao. update () ;}@ PostConstructpublic void init () {System. out. println ("Run before container creation") ;}@ PreDestroypublic void destory () {System. out. println ("executed after container destruction ");}}

4. face Cutting

Package com. service; import org. aspectj. lang. annotation. after; import org. aspectj. lang. annotation. afterReturning; import org. aspectj. lang. annotation. aspect; import org. aspectj. lang. annotation. before; import org. aspectj. lang. annotation. pointcut; import org. springframework. stereotype. component; @ Aspect @ Componentpublic class SaveInter {@ Pointcut ("execution (* com. service. okpService. *(..)) ") public void method () {}; @ Before (" method () ") public void before () {System. out. println ("pre-notification") ;}@ AfterReturning ("method ()") public void doAfter () {System. out. println ("post notification") ;}@ After ("method ()") public void after () {System. out. println ("final notification ");}}

@ Aspect face Cutting class

@ Component Load the class to the spring container

@ Poincut defines a plane

@ Before: Before the method in the execution plane is executed

@ AfterReturning: Execution completed

@ After is equivalent to finally in try {} catch {} finally {} in the method. It is executed before the method is executed.

5. Testing

package com.service;import org.junit.Test;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.ClassPathXmlApplicationContext;public class OkpServiceTest {@Testpublic void testSave() {BeanFactory bf=new ClassPathXmlApplicationContext("applicationContext.xml");OkpService okp=(OkpService) bf.getBean("okpService");okp.save();okp.update();}}

6. Configuration File

                        

To use the Aop annotation, add <aop: aspectj-autoproxy> </aop: aspectj-autoproxy> to the xml configuration file.

Running result:

Log4j: WARN No appenders cocould be found for logger (org. springframework. context. support. ClassPathXmlApplicationContext ).
Log4j: WARN Please initialize the log4j system properly.
Run
Pre-notification
OkpDaoImpl. save ()
Final notification
Post notification
Pre-notification
OkpDaoImpl. update ()
Final notification
Post notification

 

 

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.