Spring-AOP 슬라이스 + Aspectj 프레임 사용
먼저 두 개의 Jar 패키지를 가져와야 합니다. 1.aspectjrt.jar 2. aspectjweaver-XXX.jar
Aspectj에서 지원하는 5가지 메모:
형식 1: Spring, 슬라이스:Audience Advice 클래스와 메모 결합
@Aspect
public class AudienceAdvice {
/** * 1> execution() : * 2> * *..UserService.*(..): * (1) * : --> :public public void * (2) *. : * (3) .UserService : UserService * (4) .* : * (5) (..) : * * : UserServiceImpl , AOP * 3> , , * * : init() after() * */
@Pointcut("execution(* *..UserService.*(..))")
public void init(){};
/** * */
@After("init()")
public void after(){
System.out.println("after");
}
/** * */
@Before("init()")
public void before(){
System.out.println("before");
}
/** * */
@Around("init()")
public void around(ProceedingJoinPoint jp){
System.out.println("before");
try {
//
jp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("after");
}
}
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:util="http://www.springframework.org/schema/util" 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.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context 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">
<!-- service -->
<bean id="userServiceImpl" class="www.change.tm.springblank.service.impl.UserServiceImpl">
</bean>
<!-- , -->
<bean id="advice" class="www.change.tm.springblank.aspectj.AudienceAdvice" />
<!-- aop , -->
<aop:aspectj-autoproxy />
</beans>
UserServiceImpl 클래스
public class UserServiceImpl implements UserService{
@Override
public void save() {
}
}
Test 섹션
public class UserServiceTest {
@Test
public void saveTest(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spr*.xml");
UserService userDao =
applicationContext.getBean("userServiceImpl", UserService.class);
userDao.save();
applicationContext.close();
}
}
형식2: XML 구성Aspectj를 사용하여 Spring 슬라이스와 결합:Audience Advice
public class AudienceAdvice {
public void init(){};
/** * */
public void after(){
System.out.println("after");
}
/** * */
public void before(){
System.out.println("before");
}
/** * */
public void around(ProceedingJoinPoint jp){
System.out.println("before");
try {
jp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("after");
}
}
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:util="http://www.springframework.org/schema/util" 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.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context 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">
<bean id="userServiceImpl" class="www.change.tm.springblank.service.impl.UserServiceImpl">
</bean>
<!-- -->
<bean id="advice" class="www.change.tm.springblank.aspectj.AudienceAdvice" />
<!-- XML -->
<aop:config>
<!-- -->
<aop:aspect ref="advice">
<!-- -->
<aop:pointcut expression="execution(* *..UserService.*(..))" id="mypoint"/>
<!-- method : (AudienceAdvice ) pointcut-ref : , -->
<!-- -->
<!-- <aop:before method="before" pointcut-ref="mypoint"/> -->
<!-- -->
<aop:around method="around" pointcut-ref="mypoint"/>
</aop:aspect>
</aop:config>
</beans>
UserServiceImpl 서비스 계층:
public class UserServiceImpl implements UserService{
public UserServiceImpl() {
System.out.println(" ");
}
@Override
public void save() {
System.out.println("save");
}
}
테스트 섹션:
public class UserServiceTest {
@Test
public void saveTest(){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spr*.xml");
UserService userDao =
applicationContext.getBean("userServiceImpl", UserService.class);
userDao.save();
applicationContext.close();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.