Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Archives
Today
Total
관리 메뉴

Everything has an expiration date

[inflearn] 20240108 [스프링-프레임워크] - 12-1강 필기 본문

[Inflearn]/자바 스프링 프레임워크(renew ver.)

[inflearn] 20240108 [스프링-프레임워크] - 12-1강 필기

Jelly-fish 2024. 1. 8. 22:00

12-1강 어노테이션을 이용한 스프링 설정

 

 - XML 을 이용한 스프링 설정파일 제작을 java 파일로 제작할 수 있는 방법에 대해서 학습한다.

12-1 XML 파일을 java 파일로 변경하기

 

 

12-1 XML 파일을 java 파일로 변경하기

 

[이전 방식]
Application
↓
.xml
↓
container
↓
bean, bean, bean​


[새롭게 학습할 방식] - 어노테이션(`annotation`) 사용.

Application
↓
.java
↓
container
↓
bean, bean, bean

`applicationContext.xml` 에서 작성했던 빈(Bean) 객체 태그들을
`.java` 파일로 변경하기

 

MemberConfig.java

 

package ems.member.configration;

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


import ems.member.dao.StudentDao;
import ems.member.service.StudentRegisterService;
import ems.member.service.StudentModifyService;
import ems.member.service.StudentDeleteService;
import ems.member.service.StudentSelectService;
import ems.member.service.StudentAllSelectService;

@Configuration
public class MemberConfig
{
  
    // <bean id="studentDao" class="ems.member.dao.StudentDao" />
    // 원래는 XML 파일을 통해 이렇게 빈(Bean)객체를 생성했으나
    // 자바를 통해 다음과 같이 작성할 수 있다.
    // 			↓
    
    // 빈 객체를 생성하기 위해 @Bean 어노테이션을 사용한다.
    @Bean
    //                __________ → bean 객체의 id와 동일하다.
    public StudentDao studentDao()
    {
    	return new StudentDao();
        // 반환형 : 빈(Bean) 객체의 타입
    }
    
    //===============================================================================
   
    /*
    <bean id="registerService" class="ems.member.service.StudentRegisterService">
    	<constructor-arg ref="studentDao"></constructor-args>
    </bean>
    */
    
    @Bean
    public StudentRegisterService registerService()
    {
    	return new StudentRegisterService(studentDao());
        //-- 생성자에 MemberDao 객체를 넣어 주어야 하므로
        //   위에서 생성한 객체 메소드를 매개변수 자리에서 호출해 준다.
    }
    
    @Bean
    public StudentModifyService modifyService()
    {
    	return new StudentModifyService(studentDao());
    }

    
    @Bean
    public StudentDeleteService deleteService()
    {
    	return new StudentDeleteService(studentDao());
    }
    
    @Bean
    public StudentSelectService selectService()
    {
    	return new StudentSelectService(studentDao());	
    }
    
    @Bean
    public StudentAllSelectService allSelectService()
    {
    	return new StudentAllSelectService(studentDao());
    }
    
    //===============================================================================
    
    /*
    <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
    //          클래스명              빈(Bean)객체 id
    //     _____________________  ___________________________
	public DataBaseConnectionInfo dataBaseConnectionInfoDev()
    {
		DataBaseConnectionInfo infoDev = new DataBaseConnectionInfo();
		infoDev.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:xe");
		infoDev.setUserId("scott");
		infoDev.setUserPw("tiger");
		
		return infoDev;
	}
    
    // 실제 서버
    @Bean
    public DataBaseConnectionInfo dataBaseConnectionInfoReal()
    {
    	DataBaseConnectionInfo infoReal = new DataBaseConnectionInfo()
        infoReal.setJdbcUrl("jdbc:oracle:thin:@192.168.0.1:1521:xe");
        infoReal.setUserId("masterid");
        infoReal.setUserPw("masterpw");
        
        return infoReal;
    }
  
  //===============================================================================
    
    /*
    	<bean id="informationService" class="ems.member.service.EMSInformationService">
		<property name="info">
			<value>Education Management System program was developed in 2015.</value>
		</property>
		<property name="copyRight">
			<value>COPYRIGHT(C) 2015 EMS CO., LTD. ALL RIGHT RESERVED. CONTACT MASTER FOR MORE INFORMATION.</value>
		</property>
		<property name="ver">
			<value>The version is 1.0</value>
		</property>
		<property name="sYear">
			<value>2015</value>
		</property>
		<property name="sMonth">
			<value>1</value>
		</property>
		<property name="sDay">
			<value>1</value>
		</property>
		<property name="eYear" value="2015" />
		<property name="eMonth" value="2" />
		<property name="eDay" value="28" />
		<property name="developers">
			<list>
				<value>Cheney.</value>
				<value>Eloy.</value>
				<value>Jasper.</value>
				<value>Dillon.</value>
				<value>Kian.</value>
			</list>
		</property>
		<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>
		<property name="dbInfos">
			<map>
				<entry>
					<key>
						<value>dev</value>
					</key>
					<ref bean="dataBaseConnectionInfoDev"/>
				</entry>
				<entry>
					<key>
						<value>real</value>
					</key>
					<ref bean="dataBaseConnectionInfoReal"/>
				</entry>
			</map>
		</property>
	</bean>
    */
    
    @Bean
	public EMSInformationService informationService() {
		EMSInformationService info = new EMSInformationService();
		info.setInfo("Education Management System program was developed in 2015.");
		info.setCopyRight("COPYRIGHT(C) 2015 EMS CO., LTD. ALL RIGHT RESERVED. CONTACT MASTER FOR MORE INFORMATION.");
		info.setVer("The version is 1.0");
		info.setsYear(2015);
		info.setsMonth(1);
		info.setsDay(1);
		info.seteYear(2015);
		info.seteMonth(2);
		info.seteDay(28);
		
		ArrayList<String> developers = new ArrayList<String>();
		developers.add("Cheney.");
		developers.add("Eloy.");
		developers.add("Jasper.");
		developers.add("Dillon.");
		developers.add("Kian.");
		info.setDevelopers(developers);
		
		Map<String, String> administrators = new HashMap<String, String>();
		administrators.put("Cheney", "cheney@springPjt.org");
		administrators.put("Jasper", "jasper@springPjt.org");
		info.setAdministrators(administrators);
		
		Map<String, DataBaseConnectionInfo> dbInfos = new HashMap<String, DataBaseConnectionInfo>();
		dbInfos.put("dev", dataBaseConnectionInfoDev);
		dbInfos.put("real", dataBaseConnectionInfoReal);
		info.setDbInfos(dbInfos);
		
		return info;
	}
    
  //===============================================================================
    
    

}

 

 


ⓐ 빈(Bean) 객체를 applicationContext.xml에서 생성했을 때, 스프링 컨테이너(IoC) 초기화(생성)
GenericXmlApplicationContext ctx
 = new GenericXmlApplicationContext("classpath:applicationContext.xml");
 
 /*
 ApplicationContext context
  = new ClassPathXmlApplicationContext("applicationContext.xml");
 */

ⓑ 빈(Bean) 객체를 .java에서 어노테이션을 통해 생성했을 때, 스프링 컨테이너(IoC) 초기화(생성)
AnnotationConfigApplicationContext ctx
 = new AnnotationConfigApplicationContext(MemberConfig.class);