Intellij Spring - AOP 적용
AOP 적용
-
Dependency 추가
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> ... <properties> ... <org.aspectj-version>1.9.5</org.aspectj-version> </properties> <dependencies> ... <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${org.aspectj-version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${org.aspectj-version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjtools</artifactId> <version>${org.aspectj-version}</version> </dependency> </dependencies> </project>
-
-
xml 이용
-
applicationContext.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 https://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="logAop" class="com.ex.spring.advices.LogAop"/> <aop:config> <aop:aspect id="logger" ref="logAop"> <!-- <aop:pointcut id="publicM" expression="within(com.ex.spring.model.*)"/>--> <aop:pointcut id="publicM" expression="execution(* com.ex.spring.model..show*(..))"/> <aop:around pointcut-ref="publicM" method="loggerAop"/> </aop:aspect> </aop:config> <bean id="person" class="com.ex.spring.model.Person"> <property name="age" value="50"/> <property name="name" value="abc"/> </bean> </beans>
-
LogAop.java
package com.ex.spring.advices; import org.aspectj.lang.ProceedingJoinPoint; public class LogAop { public Object loggerAop(ProceedingJoinPoint joinPoint) throws Throwable{ String signatureStr = joinPoint.getSignature().toShortString(); System.out.println(signatureStr+"is start."); long st = System.currentTimeMillis(); try{ Object obj = joinPoint.proceed(); return obj; }finally { long et = System.currentTimeMillis(); System.out.println(signatureStr + " is finished."); System.out.println(signatureStr + " 경과시간 : "+(et-st)); } } }
-
Person.java
package com.ex.spring.model; public class Person{ int age; String name; public Person(int age, String name) { this.age = age; this.name = name; } public Person() { } public int getAge() { System.out.println("getAge()"); return age; } public void setAge(int age) { this.age = age; } public String getName() { System.out.println("getName()"); return name; } public void setName(String name) { this.name = name; } public void showInfo() { System.out.println("name : "+name+" age : "+age); } }
-
MController.java
package com.ex.spring.controller; import com.ex.spring.model.Person; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import java.util.Locale; @Controller public class MController { @Autowired private Person person; @RequestMapping("/index") public ModelAndView home(Locale locale) { ModelAndView mv = new ModelAndView(); mv.addObject("person", person); mv.setViewName("index"); return mv; } }
-
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Index.jsp</title> </head> <body> Name : ${person.name} <br/> Age : ${person.age} <br/> ${person.showInfo()} </body> </html>
-
-
@Aspect 이용
-
applicationContext.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 https://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy/> <bean id="annotationLogAop" class="com.ex.spring.advices.AnnotationLogAop"/> <bean id="person" class="com.ex.spring.model.Person"> <property name="age" value="50"/> <property name="name" value="abc"/> </bean> </beans>
<aop:aspectj-autoproxy/>
를 추가 -
AnnotationLogAop.java
package com.ex.spring.advices; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class AnnotationLogAop { @Pointcut("within(com.ex.spring.model.*)") public void pointcutMethod() { } @Around("pointcutMethod()") public Object loggerAop(ProceedingJoinPoint joinPoint) throws Throwable { String signatureStr = joinPoint.getSignature().toShortString(); System.out.println(signatureStr + "is start."); long st = System.currentTimeMillis(); try { Object obj = joinPoint.proceed(); return obj; } finally { long et = System.currentTimeMillis(); System.out.println(signatureStr + " is finished."); System.out.println(signatureStr + " 경과시간 : " + (et - st)); } } @Before("within(com.ex.spring.model.*)") public void beforeAdvice() { System.out.println("beforeAdvice()"); } }
-
-
@Component 이용
-
applicationContext.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.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <aop:aspectj-autoproxy/> <context:component-scan base-package="com.ex.spring.advices" /> <bean id="person" class="com.ex.spring.model.Person"> <property name="age" value="50"/> <property name="name" value="abc"/> </bean> </beans>
-
AnnotationLogAop.java
package com.ex.spring.advices; import org.aspectj.lang.ProceedingJoinPoint; 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.Component; @Component @Aspect public class AnnotationLogAop { @Pointcut("within(com.ex.spring.model.*)") public void pointcutMethod() { } @Around("pointcutMethod()") public Object loggerAop(ProceedingJoinPoint joinPoint) throws Throwable { String signatureStr = joinPoint.getSignature().toShortString(); System.out.println(signatureStr + "is start."); long st = System.currentTimeMillis(); try { Object obj = joinPoint.proceed(); return obj; } finally { long et = System.currentTimeMillis(); System.out.println(signatureStr + " is finished."); System.out.println(signatureStr + " 경과시간 : " + (et - st)); } } @Before("within(com.ex.spring.model.*)") public void beforeAdvice() { System.out.println("beforeAdvice()"); } }
-
신입SW인력을 위한 실전 자바(Java) 스프링(Spring) 동영상과정 제 10강 AOP-I
신입SW인력을 위한 실전 자바(Java) 스프링(Spring) 동영상과정 제 10강 AOP-II
Leave a comment