씨네
공부하는 개발자 강씨네
씨네
  • 분류 전체보기 (460)
    • Web (21)
      • HTML (11)
      • CSS (10)
    • JS 관련 (49)
      • JavaScript (27)
      • JQuery (22)
    • TS 관련 (15)
      • TypeScript (15)
    • NodeJS (7)
      • NodeJS (7)
    • 따라하며 배우는 시리즈 (23)
      • NodeJS & ReactJS Basic (23)
      • NodeJS & ReactJS Movie (0)
      • NodeJS & ReactJS Youtube (0)
      • NodeJS & ReactJS ChatBot (0)
    • SPA (14)
      • React (14)
      • Vue (0)
      • Anguler (0)
    • Java 관련 (118)
      • Java (52)
      • JDBC (6)
      • JSP & Servlet (18)
      • Spring Legecy (38)
      • SpringBoot (4)
    • Python (26)
      • Python (20)
      • PyMongo (1)
      • Django (5)
    • Git (24)
      • Github (24)
    • RDB (22)
      • Oracle (21)
      • MySQL (1)
    • NoSQL (5)
      • MongoDB (5)
    • OS (4)
      • Linux (4)
    • 빅데이터 (2)
      • hadoop (2)
    • IDE (20)
      • eclipse (11)
      • VSCODE (4)
      • VisualStudio (1)
      • IntelliJ (1)
      • PyCharm (1)
      • DBeaver (2)
    • Install (3)
      • Tomcat (1)
      • Docker (1)
      • Anaconda (1)
    • 오류&에러 (28)
      • TS (2)
      • NodeJS (7)
      • SQL (8)
      • Java (1)
      • Spring (4)
      • Git (6)
      • 기타 (0)
    • 알고리즘 (67)
      • 수열 (1)
      • 백준(backjoon) (39)
      • Programmers (27)
    • 자격증 (5)
      • SQLD (5)
    • 기타 (2)
    • IT유튜브로 지식쌓기 (2)

공지사항

인기 글

최근 글

티스토리

250x250
hELLO · Designed By 정상우.
씨네

공부하는 개발자 강씨네

[Spring] AOP(@Component)
Java 관련/Spring Legecy

[Spring] AOP(@Component)

2022. 3. 28. 12:50
728x90

 

 


패키지구조

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();
	}

}
 

실행결과

 

이번에는 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
    'Java 관련/Spring Legecy' 카테고리의 다른 글
    • [Spring] Spring Legacy Project
    • [Spring] Spring MVC & TODO (@RequestMapping @RequestParam @ModelAttribute @SessionAttribute)
    • [Spring] AOP(before, after, after-returning, after-throwing, around)
    • [Spring] AOP(Aspect Oriented Programming)
    씨네
    씨네
    개발자 씨네가 공부하는 내용을 기록 겸 공유하는 블로그입니다!

    티스토리툴바