자바 과정 11회차/spring mvc1 게시판
0920 AOP실습 Anno
y2on
2019. 1. 5. 01:20
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <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"> <modelVersion>4.0.0</modelVersion> <groupId>AOPAnno</groupId> <artifactId>AOPAnno</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.1</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.8.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.0.8.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> </dependencies> </project> | cs |
cats.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package com.aopex; public class Cat { private String name; private int age; private String color; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Cat [name=" + name + ", age=" + age + ", color=" + color + "]"; } public void catInfo() { System.out.println("이름:" + name); System.out.println("나이:" + age); System.out.println("컬러:" + color); } } | cs |
bean.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?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-4.3.xsd"> <aop:aspectj-autoproxy/> <bean id="logAOP" class="com.aopex.LogAOP"/> <bean id="myCat" class="com.aopex.Cat"> <property name="name" value="야옹이"/> <property name="age" value="2"/> <property name="color" value="노랑"/> </bean> </beans> | cs |
9행 : 어노테이션 적용하기 위해 추가
logaop.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package com.aopex; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class LogAOP { @Around("within(com.aopex.*)") public Object loggerAop(ProceedingJoinPoint joinpoint)throws Throwable { String signatureStr = joinpoint.getSignature().toShortString(); System.out.println(signatureStr + "시작"); System.out.println("실행전:" + System.currentTimeMillis()); try { Object obj = joinpoint.proceed(); // 핵심기능 실행 return obj; }finally { System.out.println("실행후:" + System.currentTimeMillis()); System.out.println(signatureStr + "종료"); } } } | cs |
7행 : @Aspect 어노테이션
16행 : 출력
mainClass.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.aopex; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.GenericXmlApplicationContext; public class mainClass { public static void main(String[] args) { // TODO Auto-generated method stub AbstractApplicationContext ctx = new GenericXmlApplicationContext("classpath:bean.xml"); Cat myC = ctx.getBean("myCat", Cat.class); myC.catInfo(); } } | cs |
* 실행값
이름 : 야옹이
나이 : 2
컬러 : 노랑
실행 전 : 현재시간 1231533
Cat.catInfo() 종료