첫 번째 유형 : 주석 구성 AOP
주석 구성 AOP (Sagne Class 라이브러리를 사용하여 구현)는 대략 세 단계로 나뉩니다.
1. 주석 @aspect를 사용하여 섹션을 정의하고 항목 지점 (@PointCut) 및 알림 유형 (@Before,@after,@After,@AfterWrowing,@atround)을 정의하십시오.
2. 가로 채워야하는 수업을 개발하십시오.
3. 섹션을 XML로 구성하십시오. 이 경우 Spring AOP 컨테이너가 관리합니다.
또한 Space의 JAR 패키지를 참조해야합니다. Sagneweaver.jar Sagnert.jar
예:
user.javapackage com.bjsxt.model; () {public void setpassword (string password) {interface com.bjsxt.dao; {public void save (사용자)};
인터페이스 구현 :
com.bjsxt.dao.impl org.springframework.com.bjsxt.dao.userdao; void save (user user) {system.out.println ( "user save11d!");
운영 클래스 :
com.bjsxt.service; com.bjsxt.dao.userdao import com.bjsxt.model.user; } public void add (user) {userdao getUserDao ()} @Resource; userdao;} public void destroy () {System.out.println ( "Destroy");
AOP에 가입하십시오
com.bjsxt.aop; 측면; import org.aspectj.lang.annotation . bjsxt.service ..*. add (..)) ") public void mymethod () {}; /*@prever ("execution (public void com.bjsxt.dao.impl.userdaoimpl.save (com.bjs xt .Model .user) ")*/ @before ("myMethod () ") public void prever () {system.out.println ("method staet ");} @after ("myMethod () ") public void () {System.out.println ( "Method After")} @AfterReturning ( "execution (public*com.bjsxt.dao ..*.*(..)). .println ( "After -Returning")} @AfterThrowing ( "execution (public*com.bjsxt.dao ..*.*(..))") {System.out.println ");}}
구성 파일
<? xml 버전 = "1.0"encoding = "utf-8"?> <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/sche ma/aop "xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schttp : //www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.spramframework.org/sc hema/aop/spring-aop -3.1 .xsd "> <!-마지막 2 줄을 추가하려면-> <context : annotation-config /> <context : component-scan base-package ="com.bjsxt " /> <!-자동 스캔 -> <aop : SALSE-AUTOPROXY/> <!-이 줄을 추가하려면-> </beans>
테스트 클래스 :
com.bjsxt.service; 공개 클래스의 반대 userServiceTest {@test public void testAdd ()는 {classPathXmlApplicationContext CTX를 던집니다. println (service.add);
결과:
com.bjsxt.service.userService $$ EnhancerByCglib $$ 7B201784Method 통계 Save11D! 메소드 후 ### 이후의 메소드 후.
알아채다:
@Aspect :이 클래스는 패싯 클래스임을 의미합니다.
@componet : 스프링을 탄젠트 클래스로 관리해야하기 때문에이 클래스 초기화는 초기화 중에 스프링 관리에 추가되어야합니다.
@befoe : 진입 지점의 논리 (조언)
실행… : 점 절단 구문
두 번째 유형 : XML 구성 AOP
위와 동일 : 구성 파일 만 다릅니다.
<? xml 버전 = "1.0"encoding = "utf-8"?> <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/sche ma/aop "xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schttp : //www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.spramframework.org/sc hema/aop/spring-aop -3.1 .xsd "> <!-마지막 2 줄을 추가하려면-> <context : annotation-config /> <context : component-scan base-package ="com.bjsxt " /> <bean id ="loginterceptor "> < /bean> <aop : config> <aop : pointcut expression ="execution (public * com.bjsxt.service .. *. add (..)) "id ="servicepointcut " /> <aop : agage id = "lo gaspect"ref = "loginterceptor"> <aop : 이전 메소드 = "pointcut-ref ="pointcut-Ref = "servicepointcut"/> </aop : 측면> </aop : config> </beans>
다음 <beans>는 스프링 구성 태그이며 콩의 몇 가지 중요한 특성입니다.
xmlns :
기본 XML 문서 구문 분석 형식, 즉 스프링 콩입니다. 주소는 http://www.springframework.org/schema/beans입니다.
이 속성을 설정하면 Bean으로 선언 된 모든 속성은 <bean> 등과 같은 <>를 통해 직접 사용할 수 있습니다.
xmlns : xsi :
XML은 URL을 통해 볼 수있는 사양입니다.
XMLNS : AOP :
이것이 핵심 요점이며, 여기에서 사용해야 할 의미 론적 사양이며, 이는 탄젠트 지향 AOP와 관련이 있습니다.
xmlns : tx :
봄의 트랜잭션 관련 구성 컨텐츠.
XML 파일은 기본 시맨틱 구문 분석 사양 만 선언 할 수 있습니다.
예를 들어, 위의 XML에서는 하나의 콩만이 기본값이며, 다른 속성을 사용하려면 AOP와 같은 특정 태그를 통해 사용해야합니다. . 예를 들어, AOP : 위의 구성.
마찬가지로, 기본 XMLNS가 AOP 관련 시맨틱 구문 분석 사양으로 구성되면 XML에서 구성과 같은 태그를 직접 쓸 수 있습니다.
위는 Spring AOP의 두 가지 구성 방법입니다. 나중에 당신과 공유 할 Spring AOP 구성 방법에 대한 기사가 더 있습니다.