728x90
![](https://blog.kakaocdn.net/dn/yYZmh/btrneY8g05Y/qsx0j2bUkO4HdbiU8lJkw0/img.png)
패키지구조
![](https://blog.kakaocdn.net/dn/pc58B/btrnln6Afdr/63j3aeU0Ebg67tNBcY8KzK/img.png)
Student.java
package com.test06;
public interface Student {
void classWork();
}
Man.java
package com.test06;
import org.springframework.stereotype.Component;
@Component
public class Man implements Student {
@Override
public void classWork() {
System.out.println("컴퓨터를 켜서 뉴스본다.");
}
}
Woman.java
package com.test06;
import org.springframework.stereotype.Component;
@Component
public class Woman implements Student {
@Override
public void classWork() {
System.out.println("컴퓨터를 켜서 주식본다.");
}
}
MyAspect.java
package com.test06;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyAspect {
@Pointcut("execution(public * *(..))")
public void myClass() {
}
@Before("myClass()")
public void before() {
System.out.println("출석한다.");
}
@After("myClass()")
public void after() {
System.out.println("집에간다.");
}
}
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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.test06" />
</beans>
MTest.java
package com.test06;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MTest {
public static void main(String[] args) {
ApplicationContext factory = new ClassPathXmlApplicationContext("com/test06/applicationContext.xml");
Student m = factory.getBean("man", Student.class);
Student w = (Student) factory.getBean("woman");
System.out.println("남학생 입장");
m.classWork();
System.out.println("----------");
System.out.println("여학생 입장");
w.classWork();
}
}
실행결과
![](https://blog.kakaocdn.net/dn/3urhT/btrnjIclRDL/iw0MWeqGkTDUoaogY0JRF1/img.png)
이번에는 applicationContext.xml의 Namespaces에서 aop와 context를 체크하여 사용할 예정입니다.
이번에는 특이하게 bean으로 객체를 만들지 않고 component-scan을 사용했는데요
<context:component-scan base-package="com.test06" />
이 1줄의 코드가 com.test06패키지에 있는 @Component를 스캔하여 자동으로 객체화 시켜줍니다.
따라서 Man클래스, Woman클래스, MyAspect클래스에 @Component 어노테이션이 붙어있는데요.
그러면 이 1줄의 코드는
<bean id="man" class="com.test06.Man" />
<bean id="woman" class="com.test06.Woman" />
<bean id="myAspect" class="com.test06.MyAspect" />
해당 코드와 동일한 코드라고 볼 수 있습니다.
또한 MyAspect의 @Aspect는 @pointcut, @before, @after을 등록시키는 역할을 해줍니다.
728x90
'Java 관련 > Spring Legecy' 카테고리의 다른 글
[Spring] Spring Legacy Project (0) | 2022.03.30 |
---|---|
[Spring] Spring MVC & TODO (@RequestMapping @RequestParam @ModelAttribute @SessionAttribute) (0) | 2022.03.29 |
[Spring] AOP(before, after, after-returning, after-throwing, around) (0) | 2022.03.27 |
[Spring] AOP(Aspect Oriented Programming) (0) | 2022.03.26 |
[Spring] AOP(joinpoin, pointcut, advice, aspect, weaving) (0) | 2022.03.25 |