본문 바로가기

🎧️ 강의듣기

스프링 프레임워크

스프링 프로젝트

        ...

        스프링 프레임워크

                DI                    Depency Injection

                                                        생성자                    - 초기화

                                                        프로퍼티

                                                        xml / annotation(POJO)

                AOP = filter

                            (Aspect Orientation(중심) Programming) 관점 중심 프로그래밍

                            객체 생성 이전 / 이후

 

                            Aspect - 공통 ( 로그인 / 검사 / 보안 ) 기능

                                        cross-cutting concern

                            언제 적용할지( 호출 전, 호출 후 ) - Advice

                            적용 가능 시점( 메서드 호출, 필드 값 ) - joinpoint

                                            → pointcut

                            적용 - weaving

 

                            AspectJ ( Apache ) → Spring 부분집합

 

                            1. 컴파일 시                    - AspectJ

                            2. 클래스 로딩                 - AspectJ

                            3. 런타임                        - AspectJ / Spring

 

                            XML

                            AspectJ

                                    http://www.eclipse.org/aspectj/

                            스프링 APIs

 

aop1프로젝트

com.exam.ex01/BoardAction.java (인터페이스)

package com.exam.ex01;

public interface BoardAction {
	void execute();
}

 

com.exam.ex01/WriteAction.java

package com.exam.ex01;

public class WriteAction implements BoardAction {

	private String writer;
	
	public WriteAction() {
		// TODO Auto-generated constructor stub
		this.writer = "홍길동";
		System.out.println( "WrtieAction() 호출" );
	}
	
	public void setWriter(String writer) {
		this.writer = writer;
		System.out.println( "setWriter(String writer) 호출" );
	}
	
	@Override
	public void execute() {
		// TODO Auto-generated method stub
		System.out.println( "execute() 시작" );
		System.out.println( "Hello " +  writer );
		System.out.println( "execute() 끝" );
	}

}

 

com.exam.ex01/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 https://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
	
	<!-- AOP 설정 -->
	<bean id="basicAdvice1" class="com.exam.ex01.BasicAdvice01" />
	<bean id="pointcutAdvice1" class="org.springframework.aop.support.DefaultPointcutAdvisor">
		<property name="advice" ref="basicAdvice1" />
		<property name="pointcut">
			<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
				<property name="pattern" value=".*execute.*" />
			</bean>
		</property>
	</bean>
	
	<bean id="basicAdvice2" class="com.exam.ex01.BasicAdvice02" />
	<bean id="pointcutAdvice2" class="org.springframework.aop.support.DefaultPointcutAdvisor">
		<property name="advice" ref="basicAdvice2" />
		<property name="pointcut">
			<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
				<property name="pattern" value=".*execute.*" />
			</bean>
		</property>
	</bean>
	
	<bean id="basicAdvice3" class="com.exam.ex01.BasicAdvice03" />
	<bean id="pointcutAdvice3" class="org.springframework.aop.support.DefaultPointcutAdvisor">
		<property name="advice" ref="basicAdvice3" />
		<property name="pointcut">
			<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
				<property name="pattern" value=".*execute.*" />
			</bean>
		</property>
	</bean>
    
    <!-- 한번만 사용할때 이렇게 작성가능 -->
    <!--
    <bean id="pointcutAdvice3" class="org.springframework.aop.support.DefaultPointcutAdvisor">
		<property name="advice" >
			<bean class="com.exam.ex01.BasicAdvice03" />
		</property>
		<property name="pointcut">
			<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
				<property name="pattern" value=".*execute.*" />
			</bean>
		</property>
	</bean>
     -->
	
	
	<bean id="action" class="com.exam.ex01.WriteAction" >
		<property name="writer">
			<value>박문수</value>
		</property>
	</bean>
	
	<!-- AOP 적용 -->
	<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean" >
		<property name="target" ref="action" />
		<property name="interceptorNames">
			<list>
				<value>pointcutAdvice1</value>
				<value>pointcutAdvice2</value>
				<value>pointcutAdvice3</value>
			</list>
		</property>
	</bean>
</beans>

 

com.exam.ex01/BasicAdvice01.java ~ BasicAdvice03.java

// BasicAdvice01.java
package com.exam.ex01;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class BasicAdvice01 implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		// TODO Auto-generated method stub
		
		System.out.println( "전처리 구간 1" );
		Object rtnObj = invocation.proceed();
		System.out.println( "후처리 구간 1" );
		
		
		return rtnObj;
	}
}

// BasicAdvice02.java
package com.exam.ex01;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class BasicAdvice02 implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		// TODO Auto-generated method stub
		
		System.out.println( "전처리 구간 2" );
		Object rtnObj = invocation.proceed();
		System.out.println( "후처리 구간 2" );
		
		
		return rtnObj;
	}
}

// BasicAdvice03.java
package com.exam.ex01;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class BasicAdvice03 implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		// TODO Auto-generated method stub
		
		System.out.println( "전처리 구간 3" );
		Object rtnObj = invocation.proceed();
		System.out.println( "후처리 구간 3" );
		
		
		return rtnObj;
	}
}

 

com.exam.ex01/App.java

package com.exam.ex01;

import org.springframework.context.support.GenericXmlApplicationContext;

public class App {
	public static void main(String[] args) {
		GenericXmlApplicationContext ctx =
				new GenericXmlApplicationContext( "classpath:com/exam/ex01/context.xml" );
		
		// BoardAction action = (BoardAction)ctx.getBean( "action" );
		BoardAction action = (BoardAction)ctx.getBean( "proxy" );
		System.out.println( "전처리" );
		action.execute();
		System.out.println( "후처리" );
		ctx.close();
	}
}

 

메서드 실행시간 알아보기

com.exam.ex02/BasicAdvice01.java

package com.exam.ex02;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StopWatch;

public class BasicAdvice01 implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		// TODO Auto-generated method stub
		
		System.out.println( "전처리 구간 1" );
		
		String methodName = invocation.getMethod().getName();
		StopWatch stopWatch = new StopWatch();
		stopWatch.start(methodName);
		
		Object rtnObj = invocation.proceed();
		System.out.println( "후처리 구간 1" );
		
		stopWatch.stop();
		System.out.println( "메서드명 : " + methodName );
		System.out.println( "실행시간 : " + stopWatch.getTotalTimeSeconds() + "초" );
		
		return rtnObj;
	}

}

 

com.exam.ex02/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 https://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
	
	<!-- AOP 설정 -->
	<bean id="basicAdvice1" class="com.exam.ex02.BasicAdvice01" />
	<bean id="pointcutAdvice1" class="org.springframework.aop.support.DefaultPointcutAdvisor">
		<property name="advice" ref="basicAdvice1" />
		<property name="pointcut">
			<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
				<property name="pattern" value=".*execute.*" />
			</bean>
		</property>
	</bean>
	
	
	
	
	<bean id="action" class="com.exam.ex02.WriteAction" >
		<property name="writer">
			<value>박문수</value>
		</property>
	</bean>
	
	<!-- AOP 적용 -->
	<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean" >
		<property name="target" ref="action" />
		<property name="interceptorNames">
			<list>
				<value>pointcutAdvice1</value>
			</list>
		</property>
	</bean>
</beans>

basicAdvice1만 남기고 다 지우고 패키지경로 확인

 

com.exam.ex02/App.java

package com.exam.ex02;

import org.springframework.context.support.GenericXmlApplicationContext;

public class App {
	public static void main(String[] args) {
		GenericXmlApplicationContext ctx =
				new GenericXmlApplicationContext( "classpath:com/exam/ex02/context.xml" );
		
		// BoardAction action = (BoardAction)ctx.getBean( "action" );
		BoardAction action = (BoardAction)ctx.getBean( "proxy" );
		System.out.println( "전처리" );
		action.execute();
		System.out.println( "후처리" );
		ctx.close();
	}
}

 

AspectJ

새로운 프로젝트 생성후 pom.xml에 추가

<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjrt</artifactId>
	<version>1.9.2</version>
</dependency>
<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjweaver</artifactId>
	<version>1.9.2</version>
</dependency>

BoardAction.java와 WriteAction.java는 전에 프로젝트에서 복사해옴

 

com.exam.ex01/BasicAdvice1.java

package com.exam.ex01;

import org.aspectj.lang.ProceedingJoinPoint;

public class BasicAdvice1 {
	public Object logAround(ProceedingJoinPoint joinpoint) throws Throwable {
		System.out.println( "전처리 1" );
		Object rtnObj = joinpoint.proceed();
		System.out.println( "후처리 1" );
		
		return rtnObj;
	}
}

 

                MVC (*)

동영상 ...

 

 

'🎧️ 강의듣기' 카테고리의 다른 글

Spring MVC  (0) 2021.08.10
Spring Framework (2)  (0) 2021.08.06
Spring Framework  (0) 2021.08.05
이클립스 Spring 설정하기  (0) 2021.08.05
Java를 위한 프레임 워크 mybatis  (0) 2021.08.02