Spring Framework에서는 AOP 사용을 위하여 XML방식도 지원을 한다.
AOP라는 네임스페이스 태그를 사용하면 된다.
XML 방식 사용 시, XML선언에 하단 내용을 추가해주여야 한다.
(기존 내용에서 xmlns:aop, xsi:schemaLocation의 aop 부분이 추가되었다.)
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
XML을 사용하여 AOP 전체 예제 내용
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:config>
<aop:aspect id="aspect" ref="schemaBasedCls">
<aop:pointcut id="executionTest" expression="execution(* com.example.aop.PrintCls.*(..))"/>
<aop:before
pointcut-ref="executionTest"
method="beforeTest"/>
<aop:after-returning
pointcut-ref="executionTest"
returning="retVal"
method="executionAndReturnValTest"/>
<aop:after-throwing
pointcut-ref="executionTest"
throwing="ex"
method="executionAndAfterThrowingValTest"/>
<aop:after
pointcut-ref="executionTest"
method="executionAndAfterTest"/>
<aop:around
pointcut-ref="executionTest"
method="executionAndAroundTest"/>
</aop:aspect>
</aop:config>
<bean id="schemaBasedCls" class="com.example.aop.SchemaBasedCls"></bean>
<bean id="printCls" class="com.example.aop.PrintCls"></bean>
</beans>
package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;
import com.example.aop.PrintCls;
@SpringBootApplication
@ImportResource("classpath:aop.xml")
public class DemoSchemabasedApplication {
private static final Logger logger = LoggerFactory.getLogger(DemoSchemabasedApplication.class);
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(DemoSchemabasedApplication.class, args);
PrintCls bean = run.getBean(PrintCls.class);
logger.info("-------------- bean method start --------------");
bean.printExample();
logger.info("-------------- bean method end --------------");
logger.info("-------------- bean return method start --------------");
bean.returnStr();
logger.info("-------------- bean return method end --------------");
logger.info("-------------- bean throw method start --------------");
try {
bean.returnThrow();
} catch (Exception e) {
}
logger.info("-------------- bean throw method end --------------");
}
}
package com.example.aop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PrintCls {
private static final Logger logger = LoggerFactory.getLogger(PrintCls.class);
public void printExample() {
logger.info("*** printExample");
}
public String returnStr() {
String value = "testReturning";
logger.info("*** print returning Str" + value );
return value;
}
public void returnThrow() throws Exception {
String msg = "execption test";
logger.info("*** print throw " + msg);
throw new Exception(msg);
}
}
package com.example.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SchemaBasedCls {
private static final Logger logger = LoggerFactory.getLogger(SchemaBasedCls.class);
//--before--
public void beforeTest() {
logger.info("before start");
logger.info("before end");
}
//--before--
//--AfterReturning--
public void executionAndReturnValTest(String retVal) {
logger.info("execution and afterReturning Value start");
logger.info("Value :" + retVal);
logger.info("execution and afterReturning Value end");
}
//--AfterReturning--
//--AfterThrowing--
public void executionAndAfterThrowingValTest(Exception ex) {
logger.info("execution and AfterThrowing Value start");
logger.error(ex.getMessage());
logger.info("execution and AfterThorwing Value end");
}
//--AfterThrowing--
//--After--
public void executionAndAfterTest() {
logger.info("execution and After start");
logger.info("execution and AFter end");
}
//--After--
//--Around--
public void executionAndAroundTest(ProceedingJoinPoint pjp) {
logger.info("execution and around start");
logger.info(pjp.getSignature().getName() + " Before Method Execution");
try {
pjp.proceed();
} catch (Throwable e) {
logger.info(pjp.getSignature().getName() + "Throw Method Execution");
} finally {
// Do Something useful, If you have
}
logger.info(pjp.getSignature().getName() + " After Method Execution");
logger.info("execution and around end");
}
//--Around
}
'프로그래밍 > Spring' 카테고리의 다른 글
[Spring]SpringBoot MVC 예제 (0) | 2021.01.28 |
---|---|
[Spring]AOP(Aspect Oriented Programming) @AspectJ (0) | 2021.01.22 |
[Spring]AOP(Aspect Oriented Programming) (0) | 2021.01.21 |
[Spring]DI(Dependency Injection) (0) | 2021.01.20 |
Spring Boot @Transactional 설정 및 유의점 (0) | 2018.02.19 |