Bean등록 및 메타정보구성이란?
Bean등록 메타정보 구성전략
1. XML 단독 사용
- 모든 Bean을 명시적으로 XML에 등록하는 방법이다.
- XML장점:생성된 Bean을 XML에서 확인할 수 있는 장점이 있다.
- XML단점:Bean개수가 많아지면 XML관리가 번거로워진다.
- 여러개발자가 같은 설정파일을 공유해서 개발할시 설정파일을 동시에 수정하면 충돌문제가생긴다.
- DI에필요함 Setter,constructor가 코드내에 반드시 존재해야한다.
- 개발중 보다는 운영중에 적합하다.
2. XML과 빈 스캐닝((Bean Scanning)의 혼용
-Bean에 해당되는 클래스에 어노테이션을 부여해준다.
-어노테이션 붙은 클래스를 자동으로 찾아 빈스캐닝을 해준다.(자동스캔)
-어노테이션을 부여하고 자동 스캔하면 XML문서자 많이 줄어든다.
-단점:Bean설정 정보를 한번에 파악하기 어려움이 있다. (의존관계파악어려움)
적합한 환경
- XML 단독 사용 : 운영환경적합하다.(한눈에 확인가능)
- XML과 빈 스캐닝((Bean Scanning) : 개발환경에적합하다.(XML을 공유하는 상황이 줄어듬)
Bean등록 및 의존관계 설정(annotation)
-@Component
-일반적인 스테레오타입이다.
-<bean>태그와 동일한 역할을 한다.
-@Repository
-영속성을 가지는 속성(파일,DB)을 가진 클래스이다.
-@Service
-비지니스로직을 가지는 클래스를 말한다.
-@Controller
-프리젠테이션레이어에서 사용한다.(웹어플리케이션에서 웹요청과 응답처리) 화면과 연결
Bean의존관계를 주입 Annotation
-@autowired
- 정밀한 의존관계 주입(DI)이 필요한 경우에 사용한다.
- 프로퍼티,setter메서드,생성자,일반메서드에 적용된다.
- 의존 하는 객체를 주입할떄 주로 Type을 이용하게된다.
- XML에서 <property> ,<constructot-arg>태그와 동일한 역할이다.
-@Resource
-기능면에선 autowired와 동일하다.
-프로퍼티,setter에서 제한적 사용된다.
-해당되는 객체를 가져올때 주로 Name으로 이용하게된다.
-@Value
-단순한값줄때 사용한다.
-@Qualifier
-@Autowired와 같이 사용한다.
-@Autowired는 타입으로 찾아 주입하는데, 동일한타입의 Bean객체가 여러개 존재할 때 특정 Bean을 찾기위해 사용한다.
Component Scan을 지원하는 태그
<context:component-scan>태그
- @Component를 통해 자동으로 Bean을 등록한다.
- @Autowired로 의존관계를 주입받는 어노테이션을 클래스에서 선언하여 사용했을 경우,
해당 클래스가 위치한 특정 패키지를 Scan하기위한 설정을XML에 해준다
- 즉 위에있는 어노테이션을 읽어줄수있게 패키지명을 XML에 등록한다.
ex><context:component-scan base-package="myspring.di.annot" />
<context:include-filter>태그 & <context:exclude-filter>태그
-자동 스캔될시 포함시킬클래스, 포함시키지않을 클래스 구체적으로 명시한다.
SpringPrinter클래스
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 | @Component ( "stringPrinter" ) /*name으로 설정*/ public class StringPrinter implements Printer { private StringBuffer buffer = new StringBuffer(); @Override //인터페이스 오버라이딩 public void print(String message) { buffer.append(message); } public String toString(){ return buffer.toString(); } } |
Hello클래스
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | @Component /*Bean으로사용*/ public class Hello { @Value("${myname}") private String name; /* @AutowiredDI의존관계설정 @Qualifier("stringPrinter")@Autowired는 Type으로 찾기때문에 객체 name설정 */ @Resource(name="${printer1}") private Printer printer; /*컬렉션타입변수선언*/ private List<String> names; //기본생성자 public Hello() { } //생성자추가 public Hello(String name, Printer printer) { super(); this.name = name; this.printer = printer; } public List<String> getNames() { return names; } public void setNames(List<String> names) { this.names = names; } /* 어노테이션사용시 set사용 할 필요없음 public void setName(String name) { this.name = name; } public void setPrinter(Printer printer) { this.printer = printer; }*/ public String sayHello() { return "Hello" + name; } public void print(){ this .printer.print(sayHello()); } } |
value.properties파일
1 2 3 4 5 6 7 8 9 10 11 12 13 | myname=Spring myprinter=printer value1=AOP value2=Spring value3=DI printer1=stringPrinter printer2=consolePrinter |
annot.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 어노테이션설정된 패키지설정 --> < context:component-scan base-package = "myspirng.di.annot" ></ context:component-scan > <!-- 프로퍼티파일 읽어오기 --> < context:property-placeholder location = "classpath:config/value.properties" /> </ beans > |
'개발 소발 > 개발 Spring' 카테고리의 다른 글
데이터엑세스개념(jdbc,springjdbc)이란? (0) | 2017.11.21 |
---|---|
사용자관리프로젝트(컨트롤러,서비스,DAO,도메인모델)란? (0) | 2017.11.21 |
bean,properties설정이란? (0) | 2017.11.20 |
Junit,Spring-Test란?(의존성예제) (0) | 2017.11.20 |
IoC,DI란? (0) | 2017.11.20 |