ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 44일 차 Spring(스프링 프레임워크) AOP & 프록시(proxy)2
    삼성SDS_멀티캠퍼스/Spring Frame Work 2015. 11. 10. 14:59
    반응형










    위에 조건에 맞춰서 만들어 보자!







    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>Spring02</groupId>

    <artifactId>Spring02</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <build>

    <sourceDirectory>src</sourceDirectory>

    <plugins>

    <plugin>

    <artifactId>maven-compiler-plugin</artifactId>

    <version>3.3</version>

    <configuration>

    <source>1.7</source>

    <target>1.7</target>

    </configuration>

    </plugin>

    </plugins>

    </build>

    <dependencies>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-core</artifactId>

    <version>3.2.8.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>3.2.8.RELEASE</version>

    </dependency>

    <dependency>

    <groupId>org.aspectj</groupId>

    <artifactId>aspectjrt</artifactId>

    <version>1.7.4</version>

    </dependency>

    <dependency>

    <groupId>org.aspectj</groupId>

    <artifactId>aspectjweaver</artifactId>

    <version>1.7.4</version>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-aop</artifactId>

    <version>3.2.8.RELEASE</version>

    </dependency>

    </dependencies>

    </project>





    package aop4;


    public class After implements PersonImpl {

    public void doSomething() {

    System.out.println("문을 열고 집을 나온다");

    }

    }








    package aop4;


    public class Before implements PersonImpl {

    public void doSomething() {

    System.out.println("문을 열고 집에 들어간다");

    }

    }





    package aop4;


    public class AfterReturning implements PersonImpl {

    public void doSomething() {

    System.out.println("옷 갈아 입고 잔다");

    }

    }






    package aop4;


    public class AfterThrowing implements PersonImpl {

    public void doSomething() {

    System.out.println("119에 신고한다");

    }

    }




    package aop4;


    public interface PersonImpl {

    public void doSomething();

    }



    package aop4;


    //여자의 기능 : 방과후 일과


    //1. 문을 열고 집에 들어간다

    //2. 화장을 지운다 ← 집에서 하는 일

    //3. 옷 갈아입고 잔다

    //4. 문을 열고 집을 나온다


    //예외상황 : 집에 불나면 119에 신고한다


    public class Girl implements PersonImpl {

    public void doSomething() {

    System.out.println("화장을 지운다");

    }

    }








    package aop4;


    //남자의 방과 후 일과


    //1. 문을 열고 집에 들어간다

    //2. 게임을 한다  ← 핵심관심사항

    //3. 옷 갈아입고 잔다

    //4. 문을 열고 집을 나온다


    //예외상황 : 집에 불나면 119에 신고한다


    public class Boy implements PersonImpl {

    public void doSomething() {

    System.out.println("게임을 잘했다");

    }

    }







    package aop4;


    public class PersonProxy implements PersonImpl {


    private PersonImpl person;

    private PersonImpl before;

    private PersonImpl afterReturning;

    private PersonImpl afterThrowing;

    private PersonImpl after;


    public void setPerson(PersonImpl person) {

    this.person = person;

    }


    public void setBefore(PersonImpl before) {

    this.before = before;

    }


    public void setAfterReturning(PersonImpl afterReturning) {

    this.afterReturning = afterReturning;

    }


    public void setAfterThrowing(PersonImpl afterThrowing) {

    this.afterThrowing = afterThrowing;

    }


    public void setAfter(PersonImpl after) {

    this.after = after;

    }


    @Override

    public void doSomething() {

    // TODO Auto-generated method stub


    before.doSomething();

    try {

    person.doSomething();

    afterReturning.doSomething();

    } catch (Exception e) {

    afterThrowing.doSomething();

    }

    after.doSomething();

    }


    }










    package aop4;


    public class Test {

    public static void main(String[] args) {

    PersonImpl mp = new Boy();

    PersonImpl gp = new Girl();

    PersonImpl before = new Before();

    PersonImpl afterReturn = new AfterReturning();

    PersonImpl afterThrow = new AfterThrowing();

    PersonImpl after = new After();

    PersonProxy pp = new PersonProxy();


    pp.setBefore(before);

    pp.setAfterReturning(afterReturn);

    pp.setPerson(mp);

    pp.setAfterThrowing(afterThrow);

    pp.setAfter(after);

    pp.doSomething();

    }

    }










    결과창











    반응형
Designed by Tistory.