Spring

[Spring]Spring DI(2)__개발공부 86일차

하체는 스쿼트 2022. 7. 12. 21:24
자바 코드를 이용한 DI 설정

 

1. applicationContext.xml 삭제하고 xml 파일 대신에 자바 파일 : Config.java 파일 추가
2.Config.java 설정 파일 -> 스프링컨테이너 사용
p59 스츠링 컨테이너의 종류
AnnotationConfigApplicationContext - 자바 코드를 설정파일로 사용 스프링 컨테이너

 

Config.java

 

//xml 파일을 대신할 자바 설정 파일(DI)
//@Configuration, @Bean 어노테이션을 사용해야된다.

 

@Configuration
public class Config {
    //@Configuration을 쓰는 의미                                            => xml파일을 대신할 것을 써준다는 의미로 '꼭'   써야한다!

    //RecordImpl record - new RecordImpl();
    //<bean id="record" class="di.RecordImpl"></bean>

    @Bean
    public RecordImpl record() {
        return  new RecordImpl();
    }

    @Bean(name = "rvi")
    public RecordViewImpl getRecordViewImpl() {
        RecordViewImpl rvi = new RecordViewImpl();
        rvi.setRecord(record());                                                 //setter 프로퍼티를 통해서 DI
        return  rvi;
    }



}//class

 


어노테이션을 이용한 
객체 간의 의존 자동 연결

 

어노테이션을 사용해서 자동으로 의존성 주입하면

xml파일 안에서 의존성 주입을 의미하는 부분이 생략 가능하다.

 

@Resource(name = "record2")   => 여러 객체중에 선택해서 주입하고 싶을떄     @Resource 사용

 

 

xml 파일 작성

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
            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/context
           http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-config></context:annotation-config>

<bean id="record" class="di.RecordImpl"></bean>     
<bean id="record2" class="di.RecordImpl"></bean>     

<bean id="rvi" class="di.RecordViewImpl"></bean> 

</beans>

 



// p104 @Autowired 또는 @Resource 어노테이션을  사용하려면 추가해야하는 코딩
//의존 자동 연결할 때 필요한 객체(스프링 빈)들을 등록하는 코딩


<context:annotation-config></context:annotation-config>

 


//bean으로 객체를 생성해 주는 부분


<bean id="record" class="di.RecordImpl"></bean>     
<bean id="record2" class="di.RecordImpl"></bean>     

//어노테이션을 사용해서 자동으로 의존성 주입   
//안에 속성 또는 자식태그로 설정하는 걸 생략할 수 있는거다.프로퍼티로 주입하는 or 자식태그 생략 가능


<bean id="rvi" class="di.RecordViewImpl"></bean> 

 

recordImpl
recordViewImpl

 

//자동으로 의존 주입하는데 나는 생서된 여러개 객체 중에 내가 원하는 걸 주입하고 싶다. 어떻게 해야할까?

의존주입을 해야되는 부분에 @Resource를 사용해 준다.

 

@Resource(name = "record2")   => 여러 객체중에 선택해서 주입하고 싶을떄     @Resource 사용

 

@Resource(name = "record2") 

private RecordImpl record = null;

 


컴포넌트 스캔을 이용한 
빈 자동 등록

 

컴포넌트 스캔?

자동으로 패키지 등록   자동으로 객체 생성하려고 

 

 

xml 파일 작성

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
            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/context
           http://www.springframework.org/schema/context/spring-context.xsd"
 >

 <!-- 빈 스캔 + 자동  -->
 <context:component-scan base-package="di"></context:component-scan>

 

</beans>

 

@Component : 스캔해서 객체를 생성해 줘야하는 부분에 적어준다.

@Autowired : 의존 주입해야되는 부분에 적어준다.

 

recordImpl
recordViewImpl

 

@Component
public class RecordImpl implements Record{

}

 

@Component("rvi")
public class RecordImpl implements Record{

     //자동으로 의존주입(DI)      어디부분에 써도 상관없다... 여기서 RecordImpl RecordViewImpl 이 연결되는 주입이 된다.
    @Autowired

    private RecordImpl record = null;

}