일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- nodejs
- 자바
- Mac
- 자바스크립트
- 도커
- 파이썬
- Spring Framework
- 영어
- 비지니스영어
- SAP
- db
- 딥러닝
- JavaScript
- ABAP
- SAP ABAP
- 머신러닝
- Oracle DB
- 오라클
- 노드
- docker
- sap mm
- 오라클 디비
- Java
- IT
- oracle
- 유럽여행
- node.js
- Programming
- SAP ERP
- Python
- Today
- Total
JIHYUN JEONG
[스프링 프레임워크/Spring Framework] POJO 06 본문
오늘은 스프링 프레임워크(Spring Framework) POJO에 관련한 실습을 진행해 보겠습니다.
□ 실습 1 - POJO
[ 최종 Hierarchy ]
- 패키지를 생성합니다.
- 클래스 파일 들을 만듭니다.
- New > Spring Bean Configuration File
Spring Framework POJO
Spring Framework POJO
- 아래와 같이 설정
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
'Information Technology > Spring ' 카테고리의 다른 글
[스프링 프레임워크/Spring Framework] Annotation(어노테이션)/ AOP(Aspect Oriented Programming) 05 (0) | 2013.03.15 |
---|---|
[스프링 프레임워크/Spring Framework] Dependency Injection(DI) 04 (0) | 2013.03.15 |
[스프링 프레임워크/Spring Framework] Spring Framework container Factory기능 예제 03 (0) | 2013.03.13 |
[스프링 프레임워크/Spring Framework] 이클립스+메이븐(eclipse + maven) 설치02 (0) | 2013.03.13 |
[스프링 프레임워크/Spring Framework] 개요 및 특징 01 (0) | 2013.03.13 |