Spring 주해 방식 AOP demo 구현
dist\spring.jar
lib\jakarta-commons\commons-logging.jar
절단면 프로 그래 밍(AOP)을 사용 했다 면 다음 jar 파일 이 필요 합 니 다.
lib/aspectj/aspectjweaver.jar 와 aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
JSR-250 의 주 해 를 사용 했다 면@Resource/@PostConstruct/@PreDestroy 와 같이 다음 jar 파일 이 필요 합 니 다.
lib\j2ee\common-annotations.jar
2、Person.java
package cn.ehoo.bean;
/**
* @author whp
* @Email [email protected]
* @Jan 4, 2011
*
*/
public class Person {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2.PersonService.java 와 PersonServiceBean.java
PersonService.java
package cn.ehoo.service;
import cn.ehoo.bean.Person;
/**
*@author whp
*@Email [email protected]
*@Jan 4, 2011
*
*/
public interface PersonService {
public void save(Person person);
}
PersonServiceBean.java
package cn.ehoo.service.impl;
import org.springframework.stereotype.Service;
import cn.ehoo.bean.Person;
import cn.ehoo.service.PersonService;
/**
* @author whp
* @Email [email protected]
* @Jan 4, 2011
*
*/
@Service("personService")
public class PersonServiceBean implements PersonService {
private String user;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public PersonServiceBean() {
}
public PersonServiceBean(String user) {
super();
this.user = user;
}
public void save(Person person) {
System.out.println(" PerServiceBean save ");
//throw new RuntimeException("======");
}
}
3、MyInterceptor.java
package cn.ehoo.service.impl;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Controller;
import cn.ehoo.bean.Person;
/**
* @author whp
* @Email [email protected]
* @Jan 4, 2011
*
*/
@Aspect//
@Controller// spring
public class MyInterceptor {
@Pointcut("execution(* cn.ehoo.service.impl.PersonServiceBean.*(..))")//
private void anyMethod() {
}//
@Before("anyMethod()&& args(userName)")// args(userName) Person .
public void doAccessCheck(Person userName) {
System.out.println(userName);//
System.out.println(" ");
}
@AfterReturning(pointcut="anyMethod()")// ,
public void doReturnCheck() {
System.out.println(" ");
}
@After("anyMethod()")// finally 。
public void doReleaseAction() {
System.out.println(" ");
}
@AfterThrowing(pointcut="anyMethod()", throwing="ex")//
public void doExceptionAction(Exception ex) {
System.out.println(" ");
}
@Around("anyMethod()")// doBasicProfiling pjp
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
System.out.println(" ");
Object retule =pjp.proceed();
System.out.println(" ");
return retule;
}
}
4 src 에서 beans.xml 를 설정 합 니 다.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:component-scan base-package="cn.ehoo" />
<aop:aspectj-autoproxy />
</beans>
4.테스트 클래스
package junit.test;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.ehoo.bean.Person;
import cn.ehoo.service.PersonService;
/**
* @author whp
* @Email [email protected]
* @Jan 4, 2011
*
*/
public class AOPTest {
static PersonService personService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
AbstractApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
personService = (PersonService) cxt.getBean("personService");
}
/**
* @author whp
* @Email [email protected]
* @Jan 4, 2011
*
*/
public static void main(String[] args) {
try {
setUpBeforeClass();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void saveTest() {
personService.save(new Person());
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.