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
 
 
String[] conf = {"conf1.xml""classpath:conf2.xml"};
ctx = new GenericXmlApplicationContext(conf);
 
 
또는 
 
ctx = new GenericXmlApplicationContext("classpath:conf1.xml""conf2.xml");
 
 
 
xml 파일에서 통합? 하기
 
예를 들어 conf2.xml 에서 
<import resource="classpath:conf1.xml">
 
 
아래처럼 한개의 파일만 사용하여 컨테이너 초기화 시켜주면 된다
new GenericXmlApplicationContext("conf2.xml");
 

'프로그래밍 > Spring' 카테고리의 다른 글

.java 파일을 사용하여 빈객체 설정  (0) 2019.06.29
의존 자동 주입 설정  (0) 2019.06.29

설정

트랙백

댓글

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
자바코드를 이용한 설정
 
xml 의 경우 필요했던 
<context:annotation-config />
태그가 자바 설정 파일에서는 필요 없다
당연한 얘긴가..?
 
 
@Configuration
public class BeanClass {
 
    @Bean
    public Student student() {
        return new Student;
    }
 
    @Bean
    public StudentInfo studentInfo() {
        return new StudentInfo(student());
    }
 
}
 
 
ApplicationContext ctx = 
    new AnnotationConfigApplicationContext(BeanClass.class);
 
StudentInfo studentInfo = 
    ctx.getBean("studentInfo", StudentInfo.class);
 
 
 
자바 설정을 사용한 자동 주입 방식
 
자바 설정을 이용한 @Atutowired 애노테이션의 경우
생성자에 적용하더라도 자동주입이 적용되지 않는다
 
 
@Bean 이 적용된 메소드 파라미터에도 자동주입이 적용된다 
 
@Bean    
public StudentInfo studentInfo(Student student){
    StudentInfo studentInfo = new StudentInfo();
    info.setStudent(student);
}
cs

'프로그래밍 > Spring' 카테고리의 다른 글

설정파일이 두개로 나뉘어진 경우 - xml  (0) 2019.06.29
의존 자동 주입 설정  (0) 2019.06.29

설정

트랙백

댓글

:: 의존자동주입보다 명시적주입이 우선한다

xml 파일에 추가한다
<context:annotation-config />

추가한 태그의 역활
@Configuration
@Autowired
@Autowired(required=false)
@Resource
@Resource(name="memberDao")
@Qualifier("sysout")
@PostConstruct 등등 을 사용할수 있게 셋팅해준다

 

 

 

@Autowired
적용범위 : 필드 , 생성자, 메소드
타입을 이용해서 주입할 객체를 찾는다
@Autowired(required=false)
주입 객체가 없어라도 에러를 발생시키지 않는다( 기본생성자 필요 )

 

 

@Resource(name="memberDao")
적용범위 : 필드, 메소드 ( 생성자는 안됨 )
name 속성을 이용하여 주입할 빈 객체를 찾는다
(name 속성없이)@Resource 를 단독으로 사용하면
@Resource 가 적용되는 필드나 메소드의 파라미터 타입을 이용하여 빈객체를 찾는다

 

 

 

@Qualifier("student")
주입 의존 객체가 중복 되었을 경우 활용한다

<bean id="Student3" class="spring.Student">
<qualifier value="student" />
</bean>

=======================================================

 

메소드의 파라미터가 2개 이상인 경우 자동주입 예)

@Autowired
public void method(Membor m, @Qualifier("student") Student s){}

설정

트랙백

댓글