Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- Graphic
- Eclips
- 메서드
- 이클립스
- 국제화
- HTML
- mybatis
- 오버로딩
- AWT
- layout
- 메소드
- oracle
- Spring
- 어노테이션
- Java
- 배열
- JavaScript
- paint
- OGNL
- struts2
- Android
- Menu
- 예외처리
- 클래스
- JSP
- 에러페이지
- 안드로이드
- 생성자
- 전화걸기
- 기본
Archives
- Today
- Total
note
Spring AOP XML스키마 기반의 POJO 클래스를 이용한 AOP 구현 본문
실행화면(Main.java)
WriteArticleService.write(..)시작
WriteArticleServiceImpl.write() 메서드 실행
ArticleDao.insert(..)시작
MySQLArticleDao.insert() 실행
ArticleDao.insert(..)종료
ArticleDao.insert(..)실행시간 : 0ms
WriteArticleService.write(..)종료
WriteArticleService.write(..)실행시간 : 0ms
Article -> ArticleDao -> MySQLArticleDao에 implements ->
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Advice 클래스를 빈으로 등록 -->
<bean id="performanceTraceAdvice" class="madvirus.spring.chap05.aop.pojo.ProfilingAdvice" />
<!-- Aspect 설정 : Advice를 어떤 Pointcut에 적용할 지 설정 -->
<aop:config>
<aop:aspect id="traceAspect1" ref="performanceTraceAdvice">
<aop:pointcut id="publicMethod" expression="execution(public * madvirus.spring.chap05.board..*(..))" />
<aop:around method="trace" pointcut-ref="publicMethod" />
</aop:aspect>
</aop:config>
<bean id="writeArticleService" class="madvirus.spring.chap05.board.service.WriteArticleServiceImpl" >
<constructor-arg>
<ref bean="articleDao"/>
</constructor-arg>
</bean>
<bean id="articleDao" class="madvirus.spring.chap05.board.dao.MySQLArticleDao" />
</beans>
공통 관심 사항
<aop:aorund> 메서드 호출 이전, 이후, 예외 발생등 모든 시점에 적용가능한 Advice
<aop:pointcut id="publicMethod" expression="execution(public * madvirus.spring.chap05.board..*(..))" /> <aop:around method="trace" pointcut-ref="publicMethod" />
<aop:aorund> 메서드 호출 이전, 이후, 예외 발생등 모든 시점에 적용가능한 Advice
Main
package madvirus.spring.chap05.aop.pojo;
import madvirus.spring.chap05.board.Article;
import madvirus.spring.chap05.board.service.WriteArticleService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
String[] configLocations = new String[] { "applicationContext.xml" };
ApplicationContext context = new ClassPathXmlApplicationContext(
configLocations);
WriteArticleService articleService= (WriteArticleService) context.getBean("writeArticleService");
articleService.write(new Article());
}
}
ProfilingAdvice
package madvirus.spring.chap05.aop.pojo;
import org.aspectj.lang.ProceedingJoinPoint;
public class ProfilingAdvice {
public Object trace(ProceedingJoinPoint joinPoint) throws Throwable{
String signatureString = joinPoint.getSignature().toShortString();
System.out.println(signatureString +"시작");
long start = System.currentTimeMillis();
try{
Object result =joinPoint.proceed();
return result;
}finally{
long finish = System.currentTimeMillis();
System.out.println(signatureString +"종료");
System.out.println(signatureString +"실행시간 : "+(finish - start)+"ms");
}
}
}
Article
package madvirus.spring.chap05.board;
import madvirus.spring.chap05.board.dao.ArticleDao;
public class Article {
private int id;
private ArticleDao articleDao;
public Article() {
}
public Article(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void increaseReadCount() {
articleDao.updateReadCount(id, 1);
}
public void setArticleDao(ArticleDao articleDao) {
this.articleDao = articleDao;
}
}
ArticleDao
package madvirus.spring.chap05.board.dao;
import madvirus.spring.chap05.board.Article;
public interface ArticleDao {
void insert(Article article);
void updateReadCount(int id, int i);
}
MySQLArticleDao
package madvirus.spring.chap05.board.dao;
import madvirus.spring.chap05.board.Article;
public class MySQLArticleDao implements ArticleDao {
@Override
public void insert(Article article) {
System.out.println("MySQLArticleDao.insert() 실행");
}
@Override
public void updateReadCount(int articleId, int inc) {
System.out.println("MySQLArticleDao.updateReadCount() 실행");
}
}
WriteArticleService
package madvirus.spring.chap05.board.service;
import madvirus.spring.chap05.board.Article;
public interface WriteArticleService {
void write(Article article);
}
WriteArticleServiceImpl
package madvirus.spring.chap05.board.service;
import madvirus.spring.chap05.board.Article;
import madvirus.spring.chap05.board.dao.ArticleDao;
public class WriteArticleServiceImpl implements WriteArticleService {
private ArticleDao articleDao;
public WriteArticleServiceImpl() {
}
public WriteArticleServiceImpl(ArticleDao articleDao) {
this.articleDao = articleDao;
}
@Override
public void write(Article article) {
System.out.println("WriteArticleServiceImpl.write() 메서드 실행");
articleDao.insert(article);
}
}
Member
package madvirus.spring.chap05.member;
public class Member {
}
MemberService
package madvirus.spring.chap05.member.service;
import madvirus.spring.chap05.member.Member;
public interface MemberService {
void regist(Member member);
boolean update(String memberId, UpdateInfo info);
}
MemberServiceImpl
package madvirus.spring.chap05.member.service;
import madvirus.spring.chap05.member.Member;
public class MemberServiceImpl implements MemberService {
@Override
public void regist(Member member) {
System.out.println("MemberServiceImpl.regist() 메서드 실행");
}
@Override
public boolean update(String memberId, UpdateInfo info) {
System.out.println("MemberServiceImpl.update() 메서드 실행");
return true;
}
}
UpdateInfo
package madvirus.spring.chap05.member.service;
public class UpdateInfo {
}
'JSP > Spring' 카테고리의 다른 글
| Spring RequsetMapping의 부가적인 기능 컨트롤러 메서드의 HTTP 전송방식 한정 (2) | 2012.03.02 |
|---|---|
| Spring Web (1) | 2012.03.02 |
| Spring AOP (1) | 2012.03.02 |
| Spring @Component 어노테이션을 이용한 자동 스캔 (0) | 2012.03.02 |
| Spring @PostConstruct / @PreDestroy 어노테이션과 라이프 사이클 (0) | 2012.03.02 |
lib.7z