롸?
Spring application - AOP 어노테이션 기반 간단예제 본문
1. Main
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("init2.xml");
LogicImpl impl = context.getBean("logicImpl", LogicImpl.class);
impl.selectDataProcess(); //AOP 적용 로직
System.out.println();
impl.updateDataPart(); //AOP 미적용 로직
}
}
2. init2.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:component-scan base-package="pack2" />
</beans>
3. aspect
@Component
@Aspect
public class Aspect1 {
@Around("execution(public void selectDataProcess())")
public Object aspectProceed(ProceedingJoinPoint joinPoint) throws Throwable{
String methodName = joinPoint.getSignature().toString();
System.out.println(methodName + " 시작 전에 작업을 수행");
Object object = joinPoint.proceed();
System.out.println(methodName + " 처리 후에 작업을 수행");
return object;
}
}
- @Around : 대상 객체의 메서드 실행 전, 후 또는 예외 발생 시점에 공통 기능을 실행
- @After는 대상 객체의 메서드 실행 후, @Before는 대상 객체의 메서드 실행 전에 공통 기능을 실행
- execution : Advice를 적용할 메서드를 명시
더보기
기본 형식 : execution(수식어패턴? 리턴타입패턴 클래스이름패턴?이름패턴(파라미터패턴)
- 수식어패턴 : public, private 등등의 수식어를 명시, 생략 가능
- 리턴타입 : 리턴 타입을 명시
- 클래스이름, 이름패턴 : 클래스 이름 및 메서드이름을 패턴으로 명시
- 파라미터패턴 : 매칭될 파라미터에 대해 명시
- '*' : 모든 값을 표현
- '..' : 0개 이상을 의미
- 위 코드와 같이 public void selectDataProcess() 라고만 쓰면 그 메소드에만 적용하는 것.
4. dao
@Repository("articleDao")
public class ArticleDao{
public void selectAll() {
System.out.println("DB를 조회하는 메소드");
}
}
5. impl
@Service
public class LogicImpl{
//핵심 로직
@Autowired
@Qualifier("articleDao")
private ArticleDao articleDao;
public LogicImpl() {}
public void selectDataProcess() {
articleDao.selectAll();
}
public void updateDataPart() {
System.out.println("aspect 미적용 메소드");
}
}
6. pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>aa</groupId>
<artifactId>teesst</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- Generic properties -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>5.0.0.RELEASE</spring-framework.version>
</properties>
<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- AOP (https://search.maven.org/artifact/org.aspectj/aspectjweaver/1.9.5/jar) -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
</dependencies>
</project>
7. 실행 결과
참고
'Framework > Spring' 카테고리의 다른 글
스프링 MVC의 흐름 (0) | 2020.04.07 |
---|---|
Spring application - Annotation기반 DI (0) | 2020.04.06 |
Spring application - xml기반 DI (0) | 2020.04.06 |
AOP (Aspect Oriented Programming) 용어 (0) | 2020.04.05 |
DI(Dependency Injection) 개념 (0) | 2020.04.03 |
Comments