๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

[Spring] ์Šคํ”„๋ง XML ์„ค์ • → ์• ๋…ธํ…Œ์ด์…˜ ์„ค์ • ๋ณ€ํ™˜ ๋ฐฉ๋ฒ•

by Leica 2020. 4. 14.
๋ฐ˜์‘ํ˜•

[Spring] ์Šคํ”„๋ง XML ์„ค์ • → ์• ๋…ธํ…Œ์ด์…˜ ์„ค์ • ๋ณ€ํ™˜ ๋ฐฉ๋ฒ•

์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉ์€ XML ์„ค์ •์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ž๋ฐ” ์• ๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜์˜ ์„ค์ •์„ ์ด์šฉํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค. (์ฐธ๊ณ  - ์Šคํ”„๋ง XML ์„ค์ • ๋ฐฉ๋ฒ• : [Spring] ์Šคํ”„๋ง XML ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ ๋ฐฉ๋ฒ• ์ •๋ฆฌ)
๋ณธ ๊ธ€์—์„œ๋Š” ์Šคํ”„๋ง์˜ XML ์„ค์ •์„ ์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ฃฌ๋‹ค.

 

๐Ÿ“„ ๋ชฉ์ฐจ

1. ์Šคํ”„๋ง ์• ๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜ ์„ค์ • ๊ธฐ๋ณธ ํฌ๋งท

2. ๊ธฐ๋ณธ์ ์ธ ๋นˆ ์„ค์ • ๋ฐฉ๋ฒ•
- ์ž๋™ ์ฃผ์ž… ์„ค์ • - autowire ์†์„ฑ

3. DI(Dependency Injection) ์„ค์ •
- ์ƒ์„ฑ์ž ์ฃผ์ž…
- ํ”„๋กœํผํ‹ฐ ์ฃผ์ž…
- ํ”„๋กœํผํ‹ฐ ์ฃผ์ž… - List ํƒ€์ž…
- ํ”„๋กœํผํ‹ฐ ์ฃผ์ž… - Map ํƒ€์ž…

4. ApplicationContext ๊ฐ์ฒด ์ƒ์„ฑ
- Configuration ํด๋ž˜์Šค๊ฐ€ ํ•œ ๊ฐœ์ธ ๊ฒฝ์šฐ
- Configuration ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ
- Configuration ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ(@Import ์‚ฌ์šฉ)

 

1. ์Šคํ”„๋ง ์• ๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜ ์„ค์ • ๊ธฐ๋ณธ ํฌ๋งท

import org.springframework.context.annotation.Configuration;

@Configuration
public class MemberConfig {

}

์•Œ์•„๋ณด๊ธฐ ์‰ฝ๋„๋ก XXXConfig, XXXConfiguration ๊ฐ™์€ ์ด๋ฆ„์˜ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํด๋ž˜์Šค์— @Configuration ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ธ๋‹ค.

 

2. ๊ธฐ๋ณธ์ ์ธ ๋นˆ ์„ค์ • ๋ฐฉ๋ฒ•

XML ์„ค์ •

<bean id="studentDao" class="ems.member.dao.StudentDao"></bean>

โฌ‡๏ธ

์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •

@Bean
public StudentDao studentDao() {
	return new StudentDao();
}

๋ฉ”์†Œ๋“œ๋ช… โžก๏ธ studentDao โžก๏ธ ๋นˆ ์ด๋ฆ„(id)

๋ฆฌํ„ด ํƒ€์ž… โžก๏ธ StudentDao โžก๏ธ ๋นˆ ํƒ€์ž…

๋ฆฌํ„ด ๊ฐ์ฒด โžก๏ธ new StudentDao() โžก๏ธ ๋นˆ ๊ฐ์ฒด

 

๋นˆ ์„ค์ • ์• ๋…ธํ…Œ์ด์…˜ (์ขŒ:XML ์„ค์ •/์šฐ:์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •)

  • id="..." โžก๏ธ @Bean(name="...")
  • scope="prototype" โžก๏ธ @Scope("prototype")
  • primary=true โžก๏ธ @Primary
  • lazy-init=true โžก๏ธ @Lazy
  • init-method="..." โžก๏ธ Bean(initMethod="...")
  • destroy-method="..." โžก๏ธ Bean(destroyMethod="...")

 

๋ฉ”์†Œ๋“œ ์•„๊ทœ๋จผํŠธ๋ฅผ ํ†ตํ•œ ์ฃผ์ž…

@Bean
public BasicDataSource source() {
	BasicDataSource source = new BasicDataSource();
	source.setDriverClassName("oracle.jdbc.OracleDriver");
	source.setUrl("jdbc:oracle:thin:@localhost:1521:orcl");
	source.setUsername("scott");
	source.setPassword("1234");
	
	return source;
}

// ๋ฉ”์†Œ๋“œ ์•„๊ทœ๋จผํŠธ ํƒ€์ž…๊ณผ ๊ฐ™์€ ๋นˆ์ด ์ž๋™์œผ๋กœ ์ฃผ์ž…๋œ๋‹ค.
@Bean
public JdbcTemplate db(BasicDataSource source) {
	JdbcTemplate db = new JdbcTemplate(source);
	return db;
}

๋นˆ ๋“ฑ๋ก ๋ฉ”์†Œ๋“œ์— ์„ ์–ธํ•œ ์•„๊ทœ๋จผํŠธ์™€ ํƒ€์ž…์ด ๊ฐ™์€ ๋นˆ์ด ์ž๋™์œผ๋กœ ์ฃผ์ž…๋œ๋‹ค.

 

์ž๋™ ์ฃผ์ž… ์„ค์ • - autowire ์†์„ฑ

๋นˆ์˜ ์ด๋ฆ„(id)์ด๋‚˜ ํƒ€์ž…์„ ํ†ตํ•ด ์ž๋™ ์ฃผ์ž…ํ•˜๋Š” <bean>์˜ autowire ์†์„ฑ์€ @Bean์˜ autowire ์†์„ฑ๊ณผ ๊ฐ™๋‹ค.

๋‹จ, ์ด ๋ฐฉ๋ฒ•์€ ์Šคํ”„๋ง 5.1๋ถ€ํ„ฐ ์ •์ƒ ๋™์ž‘์€ ํ•˜์ง€๋งŒ deprecated ๋˜์—ˆ๋‹ค.

 

์ด๋ฆ„(id) ์ž๋™ ์ฃผ์ž… ๐Ÿ‘‰

XML ์„ค์ •

<bean id='data1' class='com.atoz_develop.beans.DataBean2'/>
<bean id='data2' class='com.atoz_develop.beans.DataBean2'/>

<bean id='xml3' class='com.atoz_develop.beans.TestBean2' autowire="byName"/>

โฌ‡๏ธ

์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •

@Bean
public DataBean2 data1() {
	return new DataBean2();
}

@Bean
public DataBean2 data2() {
	return new DataBean2();
}

// autowire: deprecated since 5.1
@Bean(autowire = Autowire.BY_NAME)
public TestBean2 java3() {
	return new TestBean2();
}

 

ํƒ€์ž… ์ž๋™ ์ฃผ์ž… ๐Ÿ‘‰

XML ์„ค์ •

<!-- byType์ด๋ฏ€๋กœ id๋Š” ์„ค์ •ํ•˜์ง€ ์•Š์•„๋„ ๋จ -->
<bean class='com.atoz_develop.beans.DataBean3'/>

<bean id='xml4' class='com.atoz_develop.beans.TestBean3' autowire="byType"/>    

โฌ‡๏ธ

์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •

// BY_TYPE์ด๋ฏ€๋กœ ๋ฉ”์†Œ๋“œ ์ด๋ฆ„(๋นˆ id)๋Š” ์ž์œ ๋กญ๊ฒŒ ์ง€์ • ๊ฐ€๋Šฅ
@Bean
public DataBean3 data100() {
	return new DataBean3();
}

@Bean(autowire = Autowire.BY_TYPE)
public TestBean3 java4() {
	return new TestBean3();
}

 

 

3. DI(Dependency Injection) ์„ค์ •

์ƒ์„ฑ์ž ์ฃผ์ž…

XML ์„ค์ •

<bean id="registerService" class="ems.member.service.StudentRegisterService">
    <constructor-arg ref="studentDao"></constructor-arg>
</bean>

โฌ‡๏ธ

์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •

@Bean
public StudentModifyService modifyService() {
	return new StudentModifyService(studentDao());
}

 

ํ”„๋กœํผํ‹ฐ ์ฃผ์ž…

XML ์„ค์ •

<bean id="dataBaseConnectionInfoDev" class="ems.member.DataBaseConnectionInfo">
    <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe"/>
    <property name="userId" value="scott"/>
    <property name="userPw" value="tiger"/>
</bean>

โฌ‡๏ธ

์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •

@Bean
public DataBaseConnectionInfo dataBaseConnectionInfoDev() {
	DataBaseConnectionInfo infoDev = new DataBaseConnectionInfo();
	infoDev.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:xe");
	infoDev.setUserId("scott");
	infoDev.setUserPw("tiger");
	
	return infoDev;
}

 

ํ”„๋กœํผํ‹ฐ ์ฃผ์ž… - List ํƒ€์ž…

XML ์„ค์ •

<bean id="informationService" class="ems.member.service.EMSInformationService">
	<property name="developers">
		<list>
			<value>Cheney.</value>
			<value>Eloy.</value>
			<value>Jasper.</value>
			<value>Dillon.</value>
			<value>Kian.</value>
		</list>
	</property>
</bean>

โฌ‡๏ธ

์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •

@Bean
public EMSInformationService informationService() {
	EMSInformationService info = new EMSInformationService();
	
	List<String> developers = new ArrayList<>();
	developers.add("Cheney.");
	developers.add("Eloy.");
	developers.add("Jasper.");
	developers.add("Dillon.");
	developers.add("Kian.");
	info.setDevelopers(developers);
	
	return info;
}

 

ํ”„๋กœํผํ‹ฐ ์ฃผ์ž… - Map ํƒ€์ž…

XML ์„ค์ •

<bean id="informationService" class="ems.member.service.EMSInformationService">
	<property name="administrators">
		<map>
			<entry>
				<key>
					<value>Cheney</value>
				</key>
				<value>cheney@springPjt.org</value>
			</entry>
			<entry>
				<key>
					<value>Jasper</value>
				</key>
				<value>jasper@springPjt.org</value>
			</entry>
		</map>
	</property>
</bean>

โฌ‡๏ธ

์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •

@Bean
public EMSInformationService informationService() {
	EMSInformationService info = new EMSInformationService();
	
	Map<String, String> administrators = new HashMap<>();
	administrators.put("Cheney", "cheney@springPjt.org");
	administrators.put("Jasper", "jasper@springPjt.org");
	info.setAdministrators(administrators);
	
	return info;
}

 

4. ApplicationContext ๊ฐ์ฒด ์ƒ์„ฑ

์• ๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜์˜ ์Šคํ”„๋ง ์„ค์ •์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ApplicationContext(์Šคํ”„๋ง IoC ์ปจํ…Œ์ด๋„ˆ)์˜ ๊ตฌํ˜„์ฒด๋Š” AnnotationConfigApplicationContext๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

Configuration ํด๋ž˜์Šค๊ฐ€ ํ•œ ๊ฐœ์ธ ๊ฒฝ์šฐ

ApplicationContext context = new AnnotationConfigApplicationContext(MemberConfig.class);

 

Configuration ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ

ApplicationContext context = new AnnotationConfigApplicationContext(MemberConfig1.class, MemberConfig2.class, MemberConfig3.class);

 

Configuration ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ(@Import ์‚ฌ์šฉ)

์• ๋…ธํ…Œ์ด์…˜ ์„ค์ •

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import({MemberConfig2.class, MemberConfig3.class})
public class MemberConfigImport {

}

@Configuration๊ณผ ํ•จ๊ป˜ @Import๋ฅผ ์‚ฌ์šฉํ•ด์„œ Importํ•  Configuration ํด๋ž˜์Šค๋ฅผ ๋ช…์‹œํ•œ๋‹ค.

 

ApplicationContext ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ๋ฒ•์€ Configuration ํด๋ž˜์Šค๊ฐ€ ํ•œ ๊ฐœ์ธ ๊ฒฝ์šฐ์™€ ๋™์ผํ•˜๋‹ค.

 

๊ด€๋ จ ๊ธ€

 - [Spring] ์Šคํ”„๋ง XML ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ ๋ฐฉ๋ฒ• ์ •๋ฆฌ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€