Christmas Pikachu AOP 적용하기
카테고리 없음

AOP 적용하기

ZI_CO 2024. 2. 29.

AOP 사용시 필수 의존 라이브러리

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.3</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.koreait</groupId>
	<artifactId>day063</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>day063</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>8.0.33</version>
		</dependency>
		
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

 

dependence 적용하기

 

 

 

application.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-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
   
   <context:component-scan base-package="com.spring.biz" />

   <bean class="com.spring.biz.common.LogAdvice" id="logAdvice" />
   <bean class="com.spring.biz.common.AfterReturningAdvice" id="ara" />
   <bean class="com.spring.biz.common.AfterThrowingAdvice" id="ata" />
   <bean class="com.spring.biz.common.AroundAdvice" id="aa" />
   <aop:config>
      <aop:pointcut id="aPointcut" expression="execution(* com.spring.biz..*Impl.*(..))" />
      <aop:pointcut id="bPointcut" expression="execution(* com.spring.biz..*Impl.select*(..))" />
      
      <aop:aspect ref="aa">
         <aop:around pointcut-ref="aPointcut" method="aroundPrintLog" />
      </aop:aspect>
      <aop:aspect ref="ara">
         <aop:after-returning pointcut-ref="bPointcut" method="afterReturningPrintLog" />
      </aop:aspect>
      <aop:aspect ref="ata">
         <aop:after-throwing pointcut-ref="aPointcut" method="printException" />
      </aop:aspect>
   </aop:config>

</beans>

 

 

표시된부분 코드 추가

 

댓글