spring AOP์ ์ฌ์ฉํ๋ 3๊ฐ์ง ๋ฐฉ๋ฒ
์คํ๋ง์ ์ฒ์ ๋ณด๊ฒ ๋๊ฑด 2010๋
์ ๋ ์๋๊ฒ ๊ฐ์๋ฐโฆ ๊ทธ์ ๋ ํผ๋ฐ์ค๋๋ก ์ฌ์ฉ๋ง ํด๋ดค์ ๋ฟ spring์ ๋ํด์ ์ ๋๋ก ๋ค์ฌ๋ค ๋ณด๊ธฐ ์์ํ๊ฑด ์ต๊ทผ์ toby's spring 3.1์ ๋ณด๊ณ ๋์์ผ spring์ ๋ํด์ ์ข๋ ๊ณต๋ถ ํด ๋ณด๊ณ ์ถ์ ์๊ฐ์ด ๋ค์๋ค.
์คํ๋ง์ ์ฒ์ ์ ํ์ ๋๋ง ํด๋ xml์ค์ ์ง์ฅ์ด๋ ๋ญ๋ ํ์ ์๊ธฐ์๋ค. (๋ฌผ๋ก XML์ ์ด์ฉํ ์ค์ ์ ๊ฐ์ํ ์ ๋ ์๋ค) java Web framework๋ค์ ์ด๊ธฐ bootstrapping์์
์ ์ํ ์ค์ ์ XML์ ํตํด์ ๊ฐ์ ธ๊ฐ๊ณค ํ๋ค. ๋ฌผ๋ก tomcat๊ฐ์ WAS๋ ๋ง์ฐฌ๊ฐ์ง๋ค.
๊ทธ๋ฌ๋ค ์ฐ์ฐํ ๊ธฐํ์ ์ ํ SI ์ฌ์
์์ egov framework2.5์ ๊ฒฝํํ๊ฒ ๋๋ค.
spring2.5๊ธฐ๋ฐ์ผ๋ก ์์ฑ๋ ๊ตญ๊ฐ ๋ํ ํฌํธ ํ๋ ์์
์ธ๋ฐ.. ์ด๋ ์ฒ์์ผ๋ก annotation์ ์ด์ฉํ MVC์ ์ ํ๊ฒ ๋๋ค. bootstrapping์ ํ์ํ ํต์ฌ bean์ ์ ์ธํ ๋๋ถ๋ถ์ MVC์ ์ฝ๋ ๋ฒ ์ด์ค๋ก ๋ถ๋ฆฌ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ง๊ธ์ bootstrapping์กฐ์ฐจ๋ ์ฝ๋ ๋ฒ ์ด์ค๋ก ๋ชจ๋ ์์ฑํ ์ ์๊ฒ ๋์๋ค.
์ด์ ๋ ๊ฐ๋ฐ ์คํ์ผ์ด ์ฌ๋ฌ ๊ฐ์ง๋ก ๋๋๊ฒ ๋์๋ค.
AOP์ ์ฌ์ฉํ๊ธฐ ์ํ ๋ฐฉ๋ฒ๋ค๋ง ํด๋ ๋ณดํธ์ ์ผ๋ก ์ฌ์ฉํ๋ 3๊ฐ์ง ๋ฐฉ๋ฒ์ ์๋ฅผ ๋ค์ด๋ณด๋ฉด
๋จผ์ ์์ java ์ฝ๋ ๋ฒ ์ด์ค
public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); //context.register(HelloWorldConfig.class); context.scan("com.toturialspoint"); context.scan("com.toturialspoint.aspect"); context.refresh(); Service service = (AspectServiceImpl)context.getBean("serviceImpl"); service.print(); }
** joinPointCut๋์์ด ๋ ์ธํฐํ์ด์ค **
public interface Service { public String print(); } public class ServiceImpl implements AspectService { final private static Logger logger = LoggerFactory.getLogger(ServiceImpl.class); @Override public String print() { logger.info("ํ์
~"); return "1"; } }
public abstract class LoggingAspect { @Pointcut("execution(* com.toturialspoint.aspect.Service.*(..))") public void processCustomer(){ } } @Configuration @Aspect public class LoggingAspectImpl extends LoggingAspect { final private static Logger logger = LoggerFactory.getLogger(LoggingAspectImpl.class); @AfterThrowing("processCustomer()") public void before(JoinPoint joinPoint){ logger.error("๋ด๊ฐ ๋จผ์ ~"); } }
** 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 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:component-scan base-package="์ค์บํ ํจํค์ง ์์น"/> <aop:aspectj-autoproxy/> </beans>
** ํฌ์ธํธ ์ปท ๋์ ์๋น์ค **
public interface Service { public String runAspect(); } @Service public class ServiceImpl implements AspectService { final private static Logger logger = LoggerFactory.getLogger(ServiceImpl.class); @Override public String runAspect() { logger.info("run service()"); return null; } }
@Aspect @Configuration public class Aspect { final private static Logger logger = LoggerFactory.getLogger(Aspect.class); @Pointcut("execution(* package๋ช
..*(..))") public void serviceMethod(){} @Before("serviceMethod()") public void before(){ logger.info("Before()"); } }
public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(); context.setConfigLocation("/applicationContext.xml"); context.refresh(); Service service = (AspectService)context.getBean("serviceImpl"); service.runAspect(); }
** 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 http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <bean id="aspectServiceImpl" class="fullxmlbase.AspectServiceImpl"/> <bean id="loggingAspect" class="fullxmlbase.LoggingAspect"/> <aop:config> <aop:pointcut expression="execution(* fullxmlbase..*(..))" id="loggingAspectPointCut"/> <aop:aspect ref="loggingAspect"> <aop:before method="beforeMethod" pointcut-ref="loggingAspectPointCut"/> </aop:aspect> </aop:config> </beans>
** pointcut ๋์ ์๋น์ค ํด๋์ค **
public interface Service { public void runAspect(); } public class ServiceImpl implements AspectService { final private static Logger logger = LoggerFactory.getLogger(ServiceImpl.class); @Override public void runAspect() { logger.info("run"); } }
public class LoggingAspect { final private static Logger logger = LoggerFactory.getLogger(LoggingAspect.class); public void beforeMethod(JoinPoint joinPoint){ logger.info("before"); } }
public static void main(String[] args){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(); context.setConfigLocation("/fullxmlbase/applicationContext.xml"); context.refresh(); Service service = (Service)context.getBean("serviceImpl"); service.runAspect(); }