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 | 
                            Tags
                            
                        
                          
                          - mybatis
- Android
- 국제화
- 생성자
- paint
- layout
- Menu
- 기본
- 오버로딩
- Eclips
- Graphic
- 이클립스
- OGNL
- 에러페이지
- oracle
- Spring
- JavaScript
- HTML
- 예외처리
- 메서드
- JSP
- struts2
- 메소드
- 안드로이드
- 어노테이션
- AWT
- 클래스
- 배열
- Java
- 전화걸기
                            Archives
                            
                        
                          
                          - Today
- Total
note
Spring @Autowired 어노테이션 본문
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	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">
	<!-- 
	-RequiredAnnotationBeanPostProcessor  :@Required 사용시
	-AutowiredAnnotationBeanPostProcessor : @Autowired 사용시
	-CommonAnnotationBeanPostProcessor : @Resource,@PostConstruct , @PreDestroy 사용시
	-ConfigurationClassPostProcessor : @Configuration 사용시 
	
	아래 태그로 대체 가능
	<context:annotation-config /> 
	-->
	
	
	
	
	<!-- @Autowired 어노테이션 사용시 필수적으로 설정 -->
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
	
	<bean id="monitor" class="madvirus.spring.chap04.homecontrol.SystemMonitor" />
	
	<bean id="sender" class="madvirus.spring.chap04.homecontrol.SmsSender" />
</beans>
MessageSender.java
package madvirus.spring.chap04.homecontrol;
public interface MessageSender {
}
SmsSender.java
package madvirus.spring.chap04.homecontrol;
public class SmsSender implements MessageSender {
	public SmsSender() {}
	
	public SmsSender(boolean value) {}
}
SystemMonitor.java
package madvirus.spring.chap04.homecontrol;
import org.springframework.beans.factory.annotation.Autowired;
public class SystemMonitor {
	private int periodTime;
	private MessageSender sender;
	public SystemMonitor(int period){
		this(period, null);
	}
	public SystemMonitor(int periodTime, MessageSender sender) {
		super();
		this.periodTime = periodTime;
		this.sender = sender;
	}
	public SystemMonitor(){
		
	}
	public int getPeriodTime() {
		return periodTime;
	}
	public void setPeriodTime(int periodTime) {
		this.periodTime = periodTime;
	}
	public MessageSender getSender() {
		return sender;
	}
	@Autowired
	public void setSender(MessageSender sender) {
		this.sender = sender;
	}	
}
Main02.java
package madvirus.spring.chap04.homecontrol;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main02 {
	public static void main(String[] args) {
		String[] configLocations = new String[] { "applicationContext.xml" };
		ApplicationContext context = new ClassPathXmlApplicationContext(
				configLocations);
		SystemMonitor monitor = context.getBean("monitor",SystemMonitor.class);
		System.out.println(monitor.getSender());
	}
}
객체가 들어오지 않아도 될경우엔
(required=false)명시
기본 값이 true 이며 false로 할경우에는 객체가 들어오지 않아도 에러가 나지 않음
property를 입력하지 않았는데
객체를 받아 들임
이 @Autowired가 이 기능을 수행함
타입 체크를 하여 데이터를 받아들임
 
실행화면(Main02.java)
 
madvirus.spring.chap04.homecontrol.SmsSender@1c121b6'JSP > Spring' 카테고리의 다른 글
| Spring @Resource 어노테이션 (0) | 2012.03.02 | 
|---|---|
| Spring @Qualfier 어노테이션 (0) | 2012.02.29 | 
| Spring @Required 어노테이션(Annotation) (0) | 2012.02.29 | 
| Spring 국제화 (0) | 2012.02.29 | 
| Spring 프로퍼티 타입을 이용한 의존 관계 자동 설정 (0) | 2012.02.29 |