본문 바로가기
국비교육

국비교육 68일차

by Diligejy 2019. 3. 14.

1.

sample01 : injection 구현

- MTest.java(main)

- Nic_name.java

- Nic_namePrn.java

- applicationContext.xml


sample01.anno : Injection을 @로 변경


sample02 : @Autowired를 has a 관계에 멤버로 지정한 다음 beans.xml에서 값전달을 확인


sample03 : 


sample05 : @Resource 연동확인

* Spring의 @autowired, @Component는 ID로 관리된다.

- @Resource : javax.annotation

- @Autowired : org.springframework.bean.factory

- @Qualifier : 어떤 클래스를 @Autowired 하는지를 지정하는 어노테이션


@Autowired는 Bean 이름을 지정할 수 없다

Bean 이름과 클래스 이름이 다른 경우 @Autowired는 사용할 수 없다.

ex)  TV 라는 클래스가 Samsung(S), LG(L)이라는 두 개의 자식 클래스가 있다고 가정

@Autowired

@Qualifier("S") -> is a 관계에서 원하는 후손을 대입하고자 할 때 사용 

private TV tv; 이라고 할 경우


public My(Samsung s){}

public void setTv(Samsung s){} 한 것과 동일한 효과를 지닌다.

<bean id = "S" class = "Samsung" />

<bean id = "my" class = "My">

<property name = "tv" ref = "S"/>

</bean>


@ Resource는 Bean이름을 지정할 수 있다

ex) @Resource("testbean")

private Emp emp;


sample06 : @Scope 속성을 이용해서 객체의 영역을 지정할 수 있다


- singleton : 컨테이너에 대해 하나의 인스턴스를 정의(default)

- prototype : Bean을 호출할 때마다 새로운 인스턴스를 생성

- request : HTTP Request단위로 인스턴스를 생성

- session : HTTP Session단위로 인스턴스를 생성


sample08 : 멤버변수 위에 선언되어 값을 바로 전달한다.

@Value 값에는 두 가지 유형

$ {property : default_value}

# {obj.property ? : default_value}


PropertySourcesPlaceholderConfigurer = 파일.properties

                                              


2.

@Autowired

private Student person; //이렇게 함으로써 public School(Student person) { this.person = person};, 

// public void setStudent(Student person){this.person = person};이 생략됨


3.

@Component //Component를 줌으로써 School school = new School(); <bean id = "school" class = "School"/> 을 만든다고 생각함

public class School {


4.

환경설정을 하는 방법에는 크게 두가지가 있다.


첫째, 생성자로 하는 방법

둘째, XML로 하는 방법


그런데 생성자로 하는 방법에는 또 두가지로 나뉜다.

첫째, Main에서 ApplicationContext(또는 AnnotationConfigApplicationContext)를 불러온 뒤 scan과 refresh를 통해 출력하는 방법

둘째는 새로운 Class를 생성하여 그곳에 

@Configuration (<beans>와 같은 의미)

@ComponentScan

@PropertySource(value = { "sample08/test.properties" })

를 주고 ApplicationContext(또는 AnnotationConfigApplicationContext)에 당겨올 클래스를 입력해주는 방법이 있다.


따라서 스프링 DI에서 환경설정 방법은 3가지이다.


'국비교육' 카테고리의 다른 글

국비교육 70일차  (0) 2019.03.18
국비교육 69일차  (0) 2019.03.15
국비교육 67일차  (0) 2019.03.13
국비교육 66일차  (0) 2019.03.12
국비교육 65일차  (0) 2019.03.11

댓글