Spring boot AOP 가 XML 프로필 을 통 해 설명 하 는 방법
14325 단어 SpringbootAOPXML프로필
앞의 두 편의 블 로그 와 예제 에서 우 리 는 주석 설정 을 통 해 절단면 을 설명 하 는 방법 을 보 여 주 었 습 니 다.다음은 XML 파일 에서 절단면 을 설명 하 는 방법 을 보 여 주 었 습 니 다.다음은 XML 에서 AOP 를 설명 하 는 일반적인 요 소 를 보 여 줍 니 다.
AOP 설정 요소
용도.
aop:advisor
AOP 알림 기 정의
aop:after
AOP 백업 알림 정의(알림 이 실 행 됐 든 안 됐 든)
aop:after-returning
AOP 반환 알림 정의
aop:after-throwing
AOP 이상 알림 정의
aop:around
AOP 서 라운드 알림 정의
aop:aspect
절단면 정의
aop:aspectj-autoproxy
@AspectJ 주해 구동 의 절단면 사용 하기
aop:before
AOP 사전 알림 정의
aop:config
상단 의 AOP 설정 요소 입 니 다.대부분의aop:*요 소 는 반드시aop:config요소 에 포함 되 어야 한다.
aop:declare-parents
투명 한 방식 으로 통 지 된 대상 에 게 추가 인 터 페 이 스 를 도입 합 니 다.
aop:pointcut
접점 정의
XML 설정 파일 의 접점 표시 기
XML 설정 파일 에서 접점 표시 기 표현 식 은 주 해 를 통 해 설정 한 쓰기 와 기본적으로 일치 합 니 다.앞에서 언급 한 것 과 구별 합 니 다.즉,XML 파일 에 서 는'and','or','not'를 사용 하여'그리고','또는','비'의 관 계 를 표시 해 야 합 니 다.
XML 파일 설정 AOP
새 OrderXmlAop.java:
package com.example.demo.aop;
public class OrderXmlAop {
/**
* @description
*/
public void doBefore(){
System.out.println(" !");
}
/**
* @description ( )
*/
public void doAfter(){
System.out.println("after!");
}
/**
* @description ( )
*/
public void doAfterReturning(){
System.out.println(" :AfterReturning");
}
/**
* @description ( )
*/
public void doAfterThrowing(){
System.out.println(" :AfterThrowing");
}
}
리 소스 디 렉 터 리 에 새 프로필 aoporder.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="wmzService" class="com.example.demo.service.impl.WMZServiceImpl"></bean>
<bean id="zsService" class="com.example.demo.service.impl.ZSServiceImpl"></bean>
<!-- -->
<bean id="OrderXmlAop" class="com.example.demo.aop.OrderXmlAop"></bean>
<!-- Aop -->
<aop:config proxy-target-class="true">
<!-- -->
<aop:aspect ref="OrderXmlAop">
<!-- : -->
<aop:before pointcut="execution(public * com.example.demo.service.TakeawayService.*(..)))" method="doBefore"/>
<!-- : -->
<aop:after pointcut="execution(public * com.example.demo.service.TakeawayService.*(..)))" method="doAfter"/>
<!-- -->
<aop:after-returning pointcut="execution(public * com.example.demo.service.TakeawayService.*(..)))" method="doAfterReturning"/>
<!-- -->
<aop:after-throwing pointcut="execution(public * com.example.demo.service.TakeawayService.*(..)))" method="doAfterThrowing"/>
</aop:aspect>
</aop:config>
</beans>
새로 짓다 TakeXmlController.java
package com.example.demo.controller;
import com.example.demo.entity.Response;
import com.example.demo.entity.ResponseResult;
import jdk.internal.org.objectweb.asm.tree.analysis.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.service.TakeawayService;
@RestController
@RequestMapping("/api")
public class TakeXmlController {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("aoporder.xml");
@RequestMapping("/orderxml")
public ResponseResult Ordexml()
{
/**
** getBean(name) name aoporder.xml bean id
* : <bean id="wmzService" class="com.example.demo.service.impl.WMZServiceImpl"></bean>
* TakeawayService wmzService=(TakeawayService)context.getBean("wmzService");
*/
TakeawayService wmzService=(TakeawayService)context.getBean("wmzService");
String wmz= wmzService.Order(12);
System.out.println(wmz);
TakeawayService zsService=(TakeawayService)context.getBean("zsService");
String zs=zsService.Order(4396);
System.out.println(zs);
return Response.makeOKRsp(wmz+";"+zs);
}
}
실행 결과:성명 서 라운드 알림
OrderXmlAop.java 수정:
package com.example.demo.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class OrderXmlAop {
/**
* @description
*/
public void doBefore(){
System.out.println(" !");
}
/**
* @description ( )
*/
public void doAfter(){
System.out.println("after!");
}
/**
* @description ( )
*/
public void doAfterReturning(){
System.out.println(" :AfterReturning");
}
/**
* @description ( )
*/
public void doAfterThrowing(){
System.out.println(" :AfterThrowing");
}
/**
* @description ( )
*/
public void doAround(ProceedingJoinPoint pj) {
try {
System.out.println("Around ");
pj.proceed();
System.out.println("Around ");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
aoporder.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="wmzService" class="com.example.demo.service.impl.WMZServiceImpl"></bean>
<bean id="zsService" class="com.example.demo.service.impl.ZSServiceImpl"></bean>
<!-- -->
<bean id="OrderXmlAop" class="com.example.demo.aop.OrderXmlAop"></bean>
<!-- Aop -->
<aop:config proxy-target-class="true">
<!-- -->
<aop:aspect ref="OrderXmlAop">
<!-- -->
<aop:around pointcut="execution(public * com.example.demo.service.TakeawayService.*(..)))" method="doAround"/>
<!-- : -->
<aop:before pointcut="execution(public * com.example.demo.service.TakeawayService.*(..)))" method="doBefore"/>
<!-- : -->
<aop:after pointcut="execution(public * com.example.demo.service.TakeawayService.*(..)))" method="doAfter"/>
<!-- -->
<aop:after-returning pointcut="execution(public * com.example.demo.service.TakeawayService.*(..)))" method="doAfterReturning"/>
<!-- -->
<aop:after-throwing pointcut="execution(public * com.example.demo.service.TakeawayService.*(..)))" method="doAfterThrowing"/>
</aop:aspect>
</aop:config>
</beans>
실행 결과:결 과 는 우리 가 예상 한 것 과 일치 합 니 다.서 라운드 알림 은 xml 설정 을 통 해 성공 적 으로 설정 되 었 습 니 다.
XML 파일 설정 설명 접점
위의 예 에서 우 리 는 절 점 표현 식 이 여러 번 반복 되 는 것 을 발 견 했 습 니 다.그러면 aspectj 설정 과 같이 절 점 을 따로 설명 하고 나중에 재 활용 할 수 있 습 니까?답 은 물론 입 니 다.다음 과 같이 aoporder.xml 을 수정 합 니 다.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="wmzService" class="com.example.demo.service.impl.WMZServiceImpl"></bean>
<bean id="zsService" class="com.example.demo.service.impl.ZSServiceImpl"></bean>
<!-- -->
<bean id="OrderXmlAop" class="com.example.demo.aop.OrderXmlAop"></bean>
<!-- Aop -->
<aop:config proxy-target-class="true">
<!-- -->
<aop:pointcut id="point" expression="execution(public * com.example.demo.service.TakeawayService.*(..)))"/>
<!-- -->
<aop:aspect ref="OrderXmlAop">
<!-- -->
<aop:around pointcut-ref="point" method="doAround"/>
<!-- : -->
<aop:before pointcut-ref="point" method="doBefore"/>
<!-- : -->
<aop:after pointcut-ref="point" method="doAfter"/>
<!-- -->
<aop:after-returning pointcut-ref="point" method="doAfterReturning"/>
<!-- -->
<aop:after-throwing pointcut-ref="point" method="doAfterThrowing"/>
</aop:aspect>
</aop:config>
</beans>
수정 후 실행 결과:XML 파일 은 알림 전달 매개 변수 로 설정 되 어 있 습 니 다.
OrderXmlAop.java 수정
public String doAround(ProceedingJoinPoint pj,double price) {
try {
System.out.println("Around ");
pj.proceed();
if(price>=4396)
{
System.out.println("zs 4399, ");
return " ";
}
System.out.println("Around ");
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return " ";
}
aoporder.xml 수정
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="wmzService" class="com.example.demo.service.impl.WMZServiceImpl"></bean>
<bean id="zsService" class="com.example.demo.service.impl.ZSServiceImpl"></bean>
<!-- -->
<bean id="OrderXmlAop" class="com.example.demo.aop.OrderXmlAop"></bean>
<!-- Aop -->
<aop:config proxy-target-class="true">
<!-- -->
<aop:pointcut id="point" expression="execution(com.example.demo.service.TakeawayService.Order(double)) and args(price) and bean(zsService)"/>
<!-- -->
<aop:aspect ref="OrderXmlAop">
<!-- -->
<aop:around pointcut-ref="point" method="doAround"/>
</aop:aspect>
</aop:config>
</beans>
총결산본 고 는 주로 XML 프로필 을 통 해 Spring AOP 를 사용 하여 프로 그래 밍 을 하 는데 지난 편의 주해 방식 과 연결 되 어 입문 한 것 이 많 고 적 을 것 이 라 고 생각 합 니 다.aop 에 대해 세 편의 블 로 그 를 통 해 간단 한 묘 사 를 한 것 에 대해 모두 가 인상 을 가지 고 AOP 의 프로 그래 밍 사상 을 기록 한 다음 에 Spring 에서 AOP 의 관련 개념 을 소개 하 였 습 니 다.그리고 주석 방식 과 XML 프로필 두 가지 방식 으로 Spring AOP 를 사용 하여 프로 그래 밍 합 니 다.그래서 op 에 대한 블 로 그 는 여기까지 입 니 다.누군가가 물 어 볼 것 입 니 다.op 안의 대리 님 은 여러 가지 가 있 습 니 다.만약 에 정말 op 을 처음부터 끝까지 다시 시작 하면 이 시 리 즈 는 따로 칼럼 을 제시 할 수 있 습 니 다.그래서 뒤의 블 로 그 는 연결 수 라 이브 러 리,기록 로그,swagger 문서 에 접속 하 는 등 기능 을 중심 으로 전개 되 었 을 것 입 니 다.이 과정 에서 제 가 잘못 사용 한 부분 이 있 거나 표현 에 문제 가 있 으 면 제때에 알려 주 십시오.본인 이 제일 먼저 고 칠 것 입 니 다.마지막 으로 주말 잘 보 내세 요.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[MeU] Hashtag 기능 개발➡️ 기존 Tag 테이블에 존재하지 않는 해시태그라면 Tag , tagPostMapping 테이블에 모두 추가 ➡️ 기존에 존재하는 해시태그라면, tagPostMapping 테이블에만 추가 이후에 개발할 태그 기반 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.