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
- 생성자
- oracle
- Spring
- 예외처리
- layout
- 오버로딩
- Java
- struts2
- 국제화
- HTML
- 배열
- Eclips
- AWT
- mybatis
- 클래스
- paint
- JavaScript
- OGNL
- 이클립스
- JSP
- Android
- Menu
- 전화걸기
- 메소드
- 에러페이지
- 기본
- Graphic
- 안드로이드
- 어노테이션
- 메서드
Archives
- Today
- Total
note
Spring @Resource 어노테이션 본문
Spring2.5부터 사용가능
스프링 싱글턴패턴
아이디 갯수만큼 객체생성
동일한 객체지만
객체주소가 다른 객체를 받기 위한
같은 프로퍼티 명의 데이터를 받는다
실행화면(Main04.java)
HomeController [alarmDevice=null, viewer=null, camera1=Camera [number=1], camera2=Camera [number=2], camera3=Camera [number=3], camera4=Camera [number=4]]
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 /> --> <!-- @Required 어노테이션 사용시 필수적으로 설정 --> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" /> <bean id="camera1" class="madvirus.spring.chap04.homecontrol.Camera"> <!-- number 프로퍼티에 @Required 어노테이션적용 number 프로퍼티를 설정하지 않을 경우 예외 발생 --> <property name="number" value="1" /> </bean> <!-- @Resource 어노테이션 사용시 필수적으로 설정 --> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> <bean id="homeController" class="madvirus.spring.chap04.homecontrol.HomeController" /> <bean id="camera2" class="madvirus.spring.chap04.homecontrol.Camera" p:number="2" /> <bean id="camera3" class="madvirus.spring.chap04.homecontrol.Camera" p:number="3" /> <bean id="camera4" class="madvirus.spring.chap04.homecontrol.Camera" p:number="4" /> </beans>
HomeController.java
package madvirus.spring.chap04.homecontrol; import javax.annotation.Resource; public class HomeController { private AlarmDevice alarmDevice; private Viewer viewer; @Resource(name = "camera1") private Camera camera1; @Resource(name = "camera2") private Camera camera2; @Resource(name = "camera3") private Camera camera3; private Camera camera4; public void setCamera1(Camera camera1) { this.camera1 = camera1; } public void setCamera2(Camera camera2) { this.camera2 = camera2; } public void setCamera3(Camera camera3) { this.camera3 = camera3; } @Resource(name = "camera4") public void setCamera4(Camera camera4) { this.camera4 = camera4; } @Override public String toString() { return "HomeController [alarmDevice=" + alarmDevice + ", viewer=" + viewer + ", camera1=" + camera1 + ", camera2=" + camera2 + ", camera3=" + camera3 + ", camera4=" + camera4 + "]"; } }
Viewer.java
package madvirus.spring.chap04.homecontrol; public interface Viewer { void add(Camera camera1); void draw(); }
AlarmDevice.java
package madvirus.spring.chap04.homecontrol; public interface AlarmDevice { void alram(String name); }
Main04.java
package madvirus.spring.chap04.homecontrol; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main04 { public static void main(String[] args) { String[] configLocations = new String[] { "applicationContext.xml" }; ApplicationContext context = new ClassPathXmlApplicationContext( configLocations); HomeController home = context.getBean("homeController",HomeController.class); System.out.println(home); } }
'JSP > Spring' 카테고리의 다른 글
Spring @Component 어노테이션을 이용한 자동 스캔 (0) | 2012.03.02 |
---|---|
Spring @PostConstruct / @PreDestroy 어노테이션과 라이프 사이클 (0) | 2012.03.02 |
Spring @Qualfier 어노테이션 (0) | 2012.02.29 |
Spring @Autowired 어노테이션 (0) | 2012.02.29 |
Spring @Required 어노테이션(Annotation) (0) | 2012.02.29 |