폴더구조
WriteAction.java
package spring6;
public class WriteAction {
public WriteAction() {
// 겟빈이 디폴트로 부르는 기본 생성자
System.out.println("디폴트 생성자 호출");
}
public void sayHello(String name){
System.out.println(name + "님 안녕하세요");
}
}
Config.java 어노테이션 설정 파일
package spring6;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public WriteAction action1(){
return new WriteAction();
}
}
ApplicationMain.java
package spring6;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ApplicationMain {
public static void main(String[] args) {
// 어노테이션 기법으로 스프링 빈객체를 만듦
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
WriteAction action1 =(WriteAction)ctx.getBean("action1");
action1.sayHello("홍길동");
}
}
2. 어노테이션 옵션추가
Config.java
package spring6;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public WriteAction action1(){
return new WriteAction();
}
@Bean(name="action3") //메서드의 이름을 바꿈
public WriteAction action2(){
return new WriteAction();
}
}
ApplicationMain.java
package spring6;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ApplicationMain {
public static void main(String[] args) {
// 어노테이션 기법으로 스프링 빈객체를 만듦
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
WriteAction action1 =(WriteAction)ctx.getBean("action1");
action1.sayHello("홍길동");
WriteAction action3 =(WriteAction)ctx.getBean("action3"); //바뀐 메서드이름을 써야함 action2 쓰면 에러남
action3.sayHello("박문수");
}
}
3. 오버로딩 생성자 사용
WriteAction.java
package spring6;
public class WriteAction {
public WriteAction() {
// 겟빈이 디폴트로 부르는 기본 생성자
System.out.println("디폴트 생성자 호출");
}
public WriteAction(String name){
System.out.println("오버로딩 생성자 호출");
}
public void sayHello(String name){
System.out.println(name + "님 안녕하세요");
}
}
Config.java
package spring6;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public WriteAction action1(){
return new WriteAction();
}
@Bean(name="action3") //메서드의 이름을 바꿈
public WriteAction action2(){
return new WriteAction();
}
@Bean
public WriteAction action4(){
return new WriteAction("성춘향"); //오버로딩 생성자 호출
}
}
ApplicationMain.java
package spring6;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ApplicationMain {
public static void main(String[] args) {
// 어노테이션 기법으로 스프링 빈객체를 만듦
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
}
}
4. AOP 관점 지향 프로그래밍 ---di(의존성 주입)를 이용해서 aop를 만듦
폴더구성
Action.java
package spring1;
public interface Action {
public abstract void sayHello();
}
WriteAction.java
package spring1;
public class WriteAction implements Action {
private String name;
public void setName(String name){
this.name = name;
}
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("sayHello 시작");
System.out.println("Hello " + name);
System.out.println("sayHello 끝");
}
}
LoginAdvice.java 인터페이스 MethodInterceptor 상속받음
package spring1;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
//서블릿의 필터와 같은 역할을 함 ( 전처리와 후처리 담당 )
public class LoginAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
//전처리 구간
System.out.println("전처리 구간");
Object rtnObj = arg0.proceed();
//후처리 구간
System.out.println("후처리 구간");
return rtnObj;
}
}
context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 전후처리 함수가있는 클래스 -->
<bean id="loginAdvice" class="spring1.LoginAdvice" />
<!-- 전후처리 빈 -->
<bean id="advice" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<!-- 공통 관심사를 구현하는 관점의 메서드를 advice라고 하며 각 어드바이스는 해당 어드바이스가 적용될 메서드를 지정한 pointcut과 연결됨 -->
<property name="advice" ref="loginAdvice" /><!-- 전후처리 클래스를 받음 -->
<property name="pointcut"><!-- 전후처리를 어느 메서드에 적용할 것인지 찾음 -->
<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*sayHello.*" /><!-- 정규표현식 sayHello 메서드를 찾음 -->
</bean>
</property>
</bean>
<!-- 그냥 빈 -->
<bean id="action" class="spring1.WriteAction">
<property name="name">
<value>홍길동</value>
</property>
</bean>
<!-- 전처리 후처리 빈 + 그냥 빈 -->
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="action" /><!-- 그냥 빈을 받음 -->
<property name="interceptorNames">
<list>
<value>advice</value><!-- 전처리 후처리 빈을 받음 -->
</list>
</property>
</bean>
</beans>
ApplicationMain.java
package spring1;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ApplicationMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring1/context.xml");
//Action action = (Action)ctx.getBean("action"); //그냥 호출
Action action = (Action)ctx.getBean("proxy"); //전처리 후처리 있는 빈 호출
action.sayHello();
ctx.close();
}
}
5. 전후처리를 메소드 명에따라서 선별적으로 적용함
Action.java
package spring1;
public interface Action {
public abstract void sayHello1();
public abstract void sayHello2();
}
WriteAction.java
package spring1;
public class WriteAction implements Action {
private String name;
public void setName(String name){
this.name = name;
}
@Override
public void sayHello1() {
// TODO Auto-generated method stub
System.out.println("sayHello1 시작");
System.out.println("Hello1 " + name);
System.out.println("sayHello1 끝");
}
@Override
public void sayHello2() {
// TODO Auto-generated method stub
System.out.println("sayHello2 시작");
System.out.println("Hello2 " + name);
System.out.println("sayHello2 끝");
}
}
LoginAdvice.java
package spring1;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
//서블릿의 필터와 같은 역할을 함 ( 전처리와 후처리 담당 )
public class LoginAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
//전처리 구간
System.out.println("전처리 구간");
Object rtnObj = arg0.proceed();
//후처리 구간
System.out.println("후처리 구간");
return rtnObj;
}
}
context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 전후처리 함수가있는 클래스 -->
<bean id="loginAdvice" class="spring1.LoginAdvice" />
<!-- 전후처리 빈 -->
<bean id="advice" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="loginAdvice" />
<property name="pointcut">
<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*sayHello1.*" /><!-- 정규표현식 sayHello1 메서드를 찾음 -->
</bean>
</property>
</bean>
<!-- 그냥 빈 -->
<bean id="action" class="spring1.WriteAction">
<property name="name">
<value>홍길동</value>
</property>
</bean>
<!-- 전처리 후처리 빈 + 그냥 빈 -->
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="action" />
<property name="interceptorNames">
<list>
<value>advice</value>
</list>
</property>
</bean>
</beans>
ApplicationMain.java
package spring1;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ApplicationMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring1/context.xml");
//Action action = (Action)ctx.getBean("action"); //그냥 호출
Action action = (Action)ctx.getBean("proxy"); //전처리 후처리 있는 빈 호출
action.sayHello1();
System.out.println();
action.sayHello2();
ctx.close();
}
}
6.전후처리에 메소드가동 시간을 측정하는 처리
폴더구성
Action.java
package spring1;
public interface Action {
public abstract void sayHello1();
public abstract void sayHello2();
}
WriteAction.java
package spring1;
public class WriteAction implements Action {
private String name;
public void setName(String name){
this.name = name;
}
@Override
public void sayHello1() {
// TODO Auto-generated method stub
System.out.println("sayHello1 시작");
System.out.println("Hello1 " + name);
System.out.println("sayHello1 끝");
}
@Override
public void sayHello2() {
// TODO Auto-generated method stub
System.out.println("sayHello2 시작");
System.out.println("Hello2 " + name);
System.out.println("sayHello2 끝");
}
}
LoginAdvice.java
package spring1;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StopWatch;
//서블릿의 필터와 같은 역할을 함 ( 전처리와 후처리 담당 )
public class LoginAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
//전처리 구간에서 함수적으로 처리할 수있음
System.out.println("-----------------전처리 구간");
String methodeName = arg0.getMethod().getName();
StopWatch stopWatch = new StopWatch();
stopWatch.start(methodeName);
System.out.println("[LOG] 1차 advice 호출 시작");
System.out.println("[LOG] 호출 Method명 : " + methodeName);
System.out.println("-------------------------");
Object rtnObj = arg0.proceed(); //null로 바꾸면 전후처리 안에 아무것도 안 넣음
//후처리 구간
System.out.println("-----------------후처리 구간");
stopWatch.stop();
System.out.println("[LOG] 처리시간 : " + stopWatch.getTotalTimeSeconds());
System.out.println("[LOG] 호출 Method명 : " + methodeName+" 종료");
System.out.println("-------------------------");
return rtnObj;
}
}
context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 전후처리 함수가있는 클래스 -->
<bean id="loginAdvice" class="spring1.LoginAdvice" />
<!-- 전후처리 빈 -->
<bean id="advice" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<!-- 공통 관심사를 구현하는 관점의 메서드를 advice라고 하며 각 어드바이스는 해당 어드바이스가 적용될 메서드를 지정한 pointcut과 연결됨 -->
<property name="advice" ref="loginAdvice" /><!-- 전후처리 클래스를 받음 -->
<property name="pointcut"><!-- 전후처리를 어느 메서드에 적용할 것인지 찾음 -->
<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*sayHello1.*" /><!-- 정규표현식 sayHello1 메서드를 찾음 -->
</bean>
</property>
</bean>
<!-- 그냥 빈 -->
<bean id="action" class="spring1.WriteAction">
<property name="name">
<value>홍길동</value>
</property>
</bean>
<!-- 전처리 후처리 빈 + 그냥 빈 -->
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="action" /><!-- 전후처리할 타겟에 그냥 빈을 받음 -->
<property name="interceptorNames">
<list>
<value>advice</value><!-- 전처리 후처리 빈을 받음 -->
</list>
</property>
</bean>
</beans>
ApplicationMain.java
package spring1;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ApplicationMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring1/context.xml");
//Action action = (Action)ctx.getBean("action"); //그냥 호출
Action action = (Action)ctx.getBean("proxy"); //전처리 후처리 있는 빈 호출
action.sayHello1();
System.out.println();
action.sayHello2();
ctx.close();
}
}
7. 여러가지 전후처리 클래스 적용하기
폴더구성
Action.java
package spring1;
public interface Action {
public abstract void sayHello1();
public abstract void sayHello2();
}
WriteAction.java
package spring1;
public class WriteAction implements Action {
private String name;
public void setName(String name){
this.name = name;
}
@Override
public void sayHello1() {
// TODO Auto-generated method stub
System.out.println("sayHello1 시작");
System.out.println("Hello1 " + name);
System.out.println("sayHello1 끝");
}
@Override
public void sayHello2() {
// TODO Auto-generated method stub
System.out.println("sayHello2 시작");
System.out.println("Hello2 " + name);
System.out.println("sayHello2 끝");
}
}
LoginAdvice.java
package spring1;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StopWatch;
//서블릿의 필터와 같은 역할을 함 ( 전처리와 후처리 담당 )
public class LoginAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
//전처리 구간에서 함수적으로 처리할 수있음
System.out.println("전처리 구간1");
Object rtnObj = arg0.proceed(); //null로 바꾸면 전후처리 안에 아무것도 안 넣음
//후처리 구간
System.out.println("후처리 구간1");
return rtnObj;
}
}
LoginAdvice2.java
package spring1;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StopWatch;
//서블릿의 필터와 같은 역할을 함 ( 전처리와 후처리 담당 )
public class LoginAdvice2 implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("전처리 구간2");
Object rtnObj = arg0.proceed();
//후처리 구간
System.out.println("후처리 구간2");
return rtnObj;
}
}
context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- 전후처리 함수가있는 클래스 -->
<bean id="loginAdvice" class="spring1.LoginAdvice" />
<!-- 전후처리2 함수가있는 클래스 -->
<bean id="loginAdvice2" class="spring1.LoginAdvice2" />
<!-- 전후처리 빈 -->
<bean id="advice" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<!-- 공통 관심사를 구현하는 관점의 메서드를 advice라고 하며 각 어드바이스는 해당 어드바이스가 적용될 메서드를 지정한 pointcut과 연결됨 -->
<property name="advice" ref="loginAdvice" /><!-- 전후처리 클래스를 받음 -->
<property name="pointcut"><!-- 전후처리를 어느 메서드에 적용할 것인지 찾음 -->
<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*sayHello1.*" /><!-- 정규표현식 sayHello1 메서드를 찾음 -->
</bean>
</property>
</bean>
<!-- 전후처리2 빈 -->
<bean id="advice2" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<!-- 공통 관심사를 구현하는 관점의 메서드를 advice라고 하며 각 어드바이스는 해당 어드바이스가 적용될 메서드를 지정한 pointcut과 연결됨 -->
<property name="advice" ref="loginAdvice2" /><!-- 전후처리 클래스를 받음 -->
<property name="pointcut"><!-- 전후처리를 어느 메서드에 적용할 것인지 찾음 -->
<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*sayHello1.*" /><!-- 정규표현식 sayHello1 메서드를 찾음 -->
</bean>
</property>
</bean>
<!-- 그냥 빈 -->
<bean id="action" class="spring1.WriteAction">
<property name="name">
<value>홍길동</value>
</property>
</bean>
<!-- 전처리 후처리 빈 + 그냥 빈 -->
<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="action" /><!-- 전후처리할 타겟에 그냥 빈을 받음 -->
<property name="interceptorNames">
<list> <!--처리 순서가 앞에것 먼저됨 -->
<value>advice</value><!-- 전처리 후처리 빈을 받음 -->
<value>advice2</value><!-- 전처리 후처리 빈을 받음 -->
</list>
</property>
</bean>
</beans>
ApplicationMain.java
package spring1;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ApplicationMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring1/context.xml");
//Action action = (Action)ctx.getBean("action"); //그냥 호출
Action action = (Action)ctx.getBean("proxy"); //전처리 후처리 있는 빈 호출
action.sayHello1();
System.out.println();
action.sayHello2();
ctx.close();
}
}
8. aspectj라이브러리를 이용해서 aop를 만듦
폴더구성
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.samples</groupId>
<artifactId>AOPEx02</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>4.2.3.RELEASE</spring-framework.version>
<!-- Hibernate / JPA -->
<hibernate.version>4.2.1.Final</hibernate.version>
<!-- Logging -->
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<!-- Test -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Logging with SLF4J & LogBack -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Test Artifacts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-framework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- 라이브러리 추가 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
</dependencies>
</project>
Action.java
package spring1;
public interface Action {
public abstract void sayHello1();
public abstract void sayHello2();
}
WriteAction.java
package spring1;
public class WriteAction implements Action {
private String name;
public void setName(String name){
this.name = name;
}
@Override
public void sayHello1() {
// TODO Auto-generated method stub
System.out.println("sayHello1 시작");
System.out.println("Hello1 " + name);
System.out.println("sayHello1 끝");
}
@Override
public void sayHello2() {
// TODO Auto-generated method stub
System.out.println("sayHello2 시작");
System.out.println("Hello2 " + name);
System.out.println("sayHello2 끝");
}
}
LoginAdvice.java
package spring1;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoginAdvice {
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("전처리 구간");
Object rtnObj = joinPoint.proceed();
System.out.println("후처리 구간");
return rtnObj;
}
}
context.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-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="action1" class="spring1.WriteAction">
<property name="name">
<value>홍길동</value>
</property>
</bean>
<bean id="loginAdvice" class="spring1.LoginAdvice"></bean>
<!-- 전처리 하고 싶으면 적고, 안하고 싶으면 삭제하면 됨 -->
<aop:config>
<aop:aspect id="logAspect" ref="loginAdvice">
<aop:around method="logAround" pointcut="execution(* sayHello1())"/>
</aop:aspect>
</aop:config>
</beans>
ApplicationMain.java
package spring1;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ApplicationMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring1/context.xml");
Action action = (Action)ctx.getBean("action1"); //proxy 방식이 아니라서 여기서 코드 수정 안함
action.sayHello1();
ctx.close();
}
}
전처리만 실행하고 싶을 때
LoginAdvice.java
package spring1;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class LoginAdvice {
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("전처리 구간");
Object rtnObj = joinPoint.proceed();
System.out.println("후처리 구간");
return rtnObj;
}
public void before(JoinPoint joinpoint) throws Throwable {
String data = joinpoint.getSignature().getName(); //joinpoint로 실행메서드 파악가능
System.out.println("전처리만 실행 : "+data);
}
}
context.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-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="action1" class="spring1.WriteAction">
<property name="name">
<value>홍길동</value>
</property>
</bean>
<bean id="loginAdvice" class="spring1.LoginAdvice"></bean>
<!-- 전처리 하고 싶으면 적고, 안하고 싶으면 삭제하면 됨 -->
<aop:config>
<aop:aspect id="logAspect" ref="loginAdvice">
<!-- <aop:around method="logAround" pointcut="execution(* sayHello1())"/> --><!-- 전후처리 모두 -->
<aop:before method="before" pointcut="execution(* sayHello1())"/><!-- 전처리만 -->
</aop:aspect>
</aop:config>
</beans>
ApplicationMain.java
package spring1;
import org.springframework.context.support.GenericXmlApplicationContext;
public class ApplicationMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:spring1/context.xml");
Action action = (Action)ctx.getBean("action1"); //proxy 방식이 아니라서 여기서 코드 수정 안함
action.sayHello1();
ctx.close();
}
}
댓글 없음:
댓글 쓰기