@Required
반드시 프로퍼티를 이용하여 값을 주입받도록 정의. 값이 안들어오면 에러나게 한다.
메서드 위에만 표시 가능하다. (프로퍼티 위에는 표시 불가능)
설정 파일에서 값을 설정하지 않을 경우 예외를 발생시킨다.
1) 리콰이어드 어노테이션을 넣은 클래스
package spring.anno.ch01;
import org.springframework.beans.factory.annotation.Required;
public class RequiredClass {
private int number;
@Required // 반드시 프로퍼티를 이용하여 값을 주입받도록 정의. 값이 안들어오면 에러나게 함.
public void setNumber(int number){
this.number = number;
}
public void display(){
System.out.println("RequiredClass.number = "+number);
}
}
2) 실행 클래스
package spring.anno.ch01;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
AbstractApplicationContext factory =
new ClassPathXmlApplicationContext("required.xml");
factory.registerShutdownHook();
RequiredClass required =
factory.getBean("required", RequiredClass.class);
required.display();
factory.close();
}
}
' Spring Framework' 카테고리의 다른 글
@Resource : 이름으로 Bean을 지정하는 어노테이션 (0) | 2016.03.30 |
---|---|
@Autowired : 객체를 자동으로 주입하는 어노테이션 (0) | 2016.03.30 |
스프링에서 Bean 객체의 플로우를 보기 위한 예제 (0) | 2016.03.29 |
Bean 객체의 타입을 이용하여 의존 객체를 주입 (autowire="byType") (0) | 2016.03.28 |
프로퍼티 이름을 이용한 의존 관계 자동 설정 (autowire="byName") (0) | 2016.03.28 |