[스프링 프레임워크/Spring Framework] POJO 06

2013. 3. 18. 11:19Information Technology/Spring

오늘은 스프링 프레임워크(Spring Framework) POJO에 관련한 실습을 진행해 보겠습니다.

 

□ 실습 1 - POJO

[ 최종 Hierarchy ]

 

  1. 패키지를 생성합니다.

 

  1. 클래스 파일 들을 만듭니다.

 

  1. New > Spring Bean Configuration File

Spring Framework POJO

Spring Framework POJO

  1. 아래와 같이 설정

 

Spring Framework POJO

 

[ Hierarchy 의 소스코드 ]

 

package com.asianaidt.sample2.pojo;

 

public interface GreetingService {

    void sayHello(Person person);

    void sayGoodbye(Person person);

 

}

 

package com.asianaidt.sample2.pojo;

 

public class GreetingServiceImpl implements GreetingService {

 

    @Override

    public void sayHello(Person person) {

        System.out.println(person.getAge()+"살인"+person.getName()+" 안녕");

 

    }

Spring Framework POJO

 

    @Override

    public void sayGoodbye(Person person) {

        System.out.println("잘가 " + person.getName());

    }

 

}

 

package com.asianaidt.sample2.pojo;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

 

 

public class GreetingTest {

 

    public static void main(String[] args) {

        

        

        ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext2.xml");

 

        // big >= small

        GreetingService greetingService = (GreetingService)container.getBean("greeting");

        System.out.println(greetingService.getClass().getName());

        

        greetingService.sayHello(new Person("Daniel",7));

        System.out.println("============================");

        greetingService.sayGoodbye(new Person("jia",5));

        

    }

 

}

 

package com.asianaidt.sample2.pojo;

 

public class LogAspect {

    

    public void afterLog() {

        System.out.println("method complete.....................");

    }

 

}

 

package com.asianaidt.sample2.pojo;

 

public class Person {

    private String name;

    private int age;

    

    public Person(String name, int age) {

        super();

        this.name = name;

        this.age = age;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

 

    @Override

    public String toString() {

        return "Person [name=" + name + ", age=" + age + "]";

    }

      

Spring Framework POJO

    

    

 

}

 

package com.asianaidt.sample2.pojo;

 

 

public class TransformAspect {

    public void transformUpper(Person p){

        System.out.println("try to transform................");

        p.setName(p.getName().toUpperCase());

    }

 

}

 

<?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" xmlns:p="http://www.springframework.org/schema/p"

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

 

    <!-- AOP namespace 쓰면 설정의 간소화(스프링 고유의 클래스를 사용하지 X) -->

 

    <!-- Bean 등록 -->

    <bean id="logAspect" class="com.asianaidt.sample2.pojo.LogAspect" />

    <bean id="transformAspect" class="com.asianaidt.sample2.pojo.TransformAspect" />

 

    <bean id = "greeting" class="com.asianaidt.sample2.pojo.GreetingServiceImpl" />

    <aop:config>

    <!-- and args(매개변수 이름) -->

        <aop:pointcut expression="execution(public void sayHello(com.asianaidt.sample2.pojo.Person)) and args(person)" id="HelloPointcut"/>

        <aop:aspect ref="logAspect">

            <!-- join point 설정 -->

            <aop:after-returning method="afterLog" pointcut="execution(public void say*(..))"/>

        </aop:aspect>

        <aop:aspect ref="transformAspect">

        <!-- aop:point cut args(person) 매개변수 이름을 맞춰준다 -->

            <aop:before method="transformUpper" pointcut-ref="HelloPointcut" arg-names="person"/>

        </aop:aspect>

    </aop:config>

 

</beans>

 

[ 최종 Hierarchy ]

 

 

 

□ 실습 2 - Annotation

 

 

Spring Framework POJO

 

 

 

 

[ 최종 Hierarchy의 소스코드 ]

 

 

package com.asianaidt.sample3.annotation;

 

public interface GreetingService {

    void sayHello(Person person);

    void sayGoodbye(Person person);

 

}

 

 

package com.asianaidt.sample3.annotation;

 

import org.springframework.stereotype.Component;

 

 

@Component("greeting")

public class GreetingServiceImpl implements GreetingService {

 

    @Override

    public void sayHello(Person person) {

        System.out.println(person.getAge()+"살인"+person.getName()+" 안녕");

 

    }

 

    @Override

    public void sayGoodbye(Person person) {

        System.out.println("잘가 " + person.getName());

    }

 

}

 

 

package com.asianaidt.sample3.annotation;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

 

 

public class GreetingTest {

 

    public static void main(String[] args) {

        

        

        ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext3.xml");

 

        // big >= small

        GreetingService greetingService = (GreetingService)container.getBean("greeting");

        System.out.println(greetingService.getClass().getName());

        

        greetingService.sayHello(new Person("Daniel",7));

        System.out.println("============================");

        greetingService.sayGoodbye(new Person("jia",5));

        

    }

 

}

 

 

package com.asianaidt.sample3.annotation;

 

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Aspect;

import org.springframework.stereotype.Component;

 

// 스프링으로 관리될 Bean이랑 공통 로직 //

 

@Aspect

@Component

public class LogAspect {

    

    //

    @AfterReturning("execution(public void say*(..))")

    public void afterLog() {

        System.out.println("method complete.....................");

    }

 

}

 

package com.asianaidt.sample3.annotation;

 

public class Person {

    private String name;

    private int age;

    

    public Person(String name, int age) {

        super();

        this.name = name;

        this.age = age;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public int getAge() {

        return age;

    }

 

    public void setAge(int age) {

        this.age = age;

    }

 

    @Override

    public String toString() {

        return "Person [name=" + name + ", age=" + age + "]";

    }

      

      

    

 

}

 

 

package com.asianaidt.sample3.annotation;

 

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

import org.springframework.stereotype.Component;

 

@Aspect

@Component

public class TransformAspect {

 

    // 포인트컷의 표현식은 value로 준다

    @Before(value = "execution(public void sayHello(com.asianaidt.sample3.annotation.Person)) and args(person)", argNames = "person")

    public void transformUpper(Person p) {

        System.out.println("try to transform................");

        p.setName(p.getName().toUpperCase());

    }

 

}

Spring Framework POJO

1 2 3 4 5 ··· 7