kr.spring.ch02 => StudentBean
package kr.spring.ch02;
public class StudentBean {
public void study(String course) {
System.out.println(course+"를 공부합니다");
}
}
SpringMain
package kr.spring.ch02;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringMain {
public static void main(String[] args) {
//applicationContext.xml 설정하일을 읽어들여 컨테이너를 생성
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//객체를 컨테이너로부터 읽어옴
StudentBean studentBean = (StudentBean)context.getBean("studentBean");
studentBean.study("국어");
//어플리케이션 종료시 컨테이너에 존재하는 모든 빈(객체)를 종료
context.close();
}
}
국어를 공부합니다
kr.spring.ch03 => OperatorBean
package kr.spring.ch03;
public class OperatorBean {
public int add(int a, int b) {
return a + b;
}
}
SpringMain
package kr.spring.ch03;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringMain {
public static void main(String[] args) {
//applicationContext.xml 설정하일을 읽어들여 컨테이너를 생성
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//객체를 컨테이너로 읽어옴
OperatorBean bean = (OperatorBean)context.getBean("operatorBean");
int result = bean.add(10, 20);
System.out.println("결과 : "+result);
//어플리케이션 종료시 컨테이너에 존재하는 모든 빈(객체)를 종료
context.close();
}
}
결과 : 30
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 객체 생성을 위한 설정(Spring Container에 생성된 객체 보관) -->
<!--
name : 빈(bean) 객체를 식별할 때 사용할 이름
class : 빈 객체를 생성할 때 사용할 클래스
-->
<!-- messageBean은 식별자 -->
<bean name="messageBean" class="kr.spring.ch01.MessageBean"/>
<bean name="studentBean" class="kr.spring.ch02.StudentBean"/>
<bean name="operatorBean" class="kr.spring.ch03.OperatorBean"/>
</beans>
- 컨테이너를 만들고 사용하는 이유
DI(Dependency Injection)를 지원:
설정 파일이나 어노테이션을 통해서 객체 간의 의존 관계를 설정(의존관계 주입)
*어노테이션은 @override 같은거를 할 때 @를 사용하는 것이며 주석이라고 생각하면 비슷하자
kr.spring.ch04 => WriteArticleDAO
package kr.spring.ch04;
public class WriteArticleDAO {
public void insert() {
System.out.println("WriteArticleDAO의 insert()메서드 실행");
}
}
WriteArticleService
package kr.spring.ch04;
public class WriteArticleService {
private WriteArticleDAO writeArticleDAO;
//생성자
public WriteArticleService(WriteArticleDAO writeArticleDAO) {
this.writeArticleDAO = writeArticleDAO;
}
public void write() {
System.out.println("WriteArticleService의 write()메서드 호출");
writeArticleDAO.insert();
} //WriteArticleService 가 WriteArticleDAO 에 의존하고있다.
}
SpringMain
package kr.spring.ch04;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringMain {
public static void main(String[] args) {
//applicationCOntext.xml 설정파일을 읽어들여 스프링 컨테이너를 생성
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//컨테이너에 DI 생성자 설정방식으로 생성된 객체를 읽어옴
WriteArticleService articleService = (WriteArticleService)context.getBean("writeArticleService");
articleService.write();
//어플리케이션 종료시 컨테이너에 존재하는 모든 빈(객체)를 종료
context.close();
}
}
WriteArticleService의 write()메서드 호출
WriteArticleDAO의 insert()메서드 실행
applicationContext.xml 추가
<!-- DI(의존관계 주입) : 생성자 방식 -->
<bean name="writeArticleService" class="kr.spring.ch04.WriteArticleService">
<!-- 생성자의 인자에 의존객체 전달 -->
<constructor-arg>
<!-- ref : 빈 객체를 참조한다는 뜻의 요약어(reference)
bean: 빈 객체를 전달할 수 있도록 빈 객체의 식별자를 명시
-->
<ref bean="writeArticleDAO"/> <!-- 아래에있는 WriteArticleDAO에 의존하고 있다는 것을 명시한것 -->
</constructor-arg>
</bean>
<bean name="writeArticleDAO" class="kr.spring.ch04.WriteArticleDAO"/>
kr.spring.ch05=> MemberDAO
package kr.spring.ch05;
public class MemberDAO {
public void register() {
System.out.println("MemberDAO의 register() 메서드 실행");
}
}
MemberService
package kr.spring.ch05;
public class MemberService {
private MemberDAO memberDAO;
//생성자
public MemberService(MemberDAO memberDAO) {
this.memberDAO = memberDAO;
}
public void send() {
System.out.println("MemberService의 send() 메서드 실행");
memberDAO.register();
}
}
applicationContext.xml 추가
<bean name="memberService" class="kr.spring.ch05.MemberService">
<constructor-arg>
<!-- 빈 객체를 전달할 수 있도록 빈 객체의 식별자를 등록해서 빈 객체 참초 -->
<ref bean="memberDAO"/>
</constructor-arg>
</bean>
<bean name="memberDAO" class="kr.spring.ch05.MemberDAO"/>
SpringMain
package kr.spring.ch05;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringMain {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MemberService service = (MemberService)context.getBean("memberService");
service.send();
//어플리케이션 종료시 컨테이너에 존재하는 모든 빈(객체)를 종료
context.close();
}
}
MemberService의 send() 메서드 실행
MemberDAO의 register() 메서드 실행
kr.spring.ch06 => SmsSender
package kr.spring.ch06;
public class SmsSender {
@Override
public String toString() {
return "SmsSender 호출";
}
}
SystemMonitor
package kr.spring.ch06;
public class SystemMonitor {
private long perioTime;
private SmsSender sender;
//생성자
public SystemMonitor(long perioTime, SmsSender sender) {
this.perioTime = perioTime;
this.sender = sender;
}
@Override
public String toString() {
return "SystemMonitor [perioTime=" + perioTime + ", sender=" + sender + "]";
}
}
applicationContext.xml 추가
<!-- DI 생성자 설정방식 -여러개의 인자 사용 -->
<bean id="monitor" class="kr.spring.ch06.SystemMonitor">
<!-- (주의) 인자의 순서대로 기재 -->
<!--
<constructor-arg>
<value>10</value>
</constructor-arg>
<constructor-arg>
<ref bean="smsSender"/>
</constructor-arg>
-->
<!-- 속성으로 값 또는 객체 설정 -->
<!--
<constructor-arg value="20"/>
<constructor-arg ref="smsSender"/>
-->
<!-- 순서를 무시할 때는 index 표시 -->
<!--
<constructor-arg index="1" ref="smsSender"/>
<constructor-arg index="0" value="30"/>
-->
<!-- 생성자의 인자명 기재 -->
<constructor-arg name="periodTime" value="40"/>
<constructor-arg name="sender" ref="smsSender"/>
</bean>
<bean id="smsSender" class="kr.spring.ch06.SmsSender"/>
SpringMain
package kr.spring.ch06;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringMain {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//DI 생성자 설정방식 - 여러개의 인자 사용
SystemMonitor monitor = (SystemMonitor)context.getBean("monitor");
System.out.println(monitor);
context.close();
}
}
SystemMonitor [periodTime=40, sender=SmsSender 호출]
kr.spring.ch07 => RegisterDAO
package kr.spring.ch07;
public class RegisterDAO {
public void insert() {
System.out.println("RegisterDAO의 insert() 메서드 실행");
}
}
RegisterService
package kr.spring.ch07;
public class RegisterService {
private RegisterDAO registerDAO;
public void setRegisterDAO(RegisterDAO registerDAO) {
this.registerDAO = registerDAO;
}
public void write() {
System.out.println("RegisterDAO의 write() 메서드 실행");
registerDAO.insert();
}
}
applicationContext2.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DI 프로퍼티 설정방식 -->
<bean name="registerService" class="kr.spring.ch07.RegisterService">
<!-- name : 의존 객체를 주입할 때 사용할 설정 메서드(setter)의 프로퍼티 이름 -->
<property name="registerDAO"><!-- registerDAO 프로퍼티 이름 -->
<ref bean="registerDAO"/><!-- ref 안에있는 bean의 registerDAO는 아래에 bean name이다. 즉 프로퍼티와 ref의 registerDAO는 다른것. -->
</property>
</bean>
<bean name="registerDAO" class="kr.spring.ch07.RegisterDAO"/>
</beans>
SpringMain
package kr.spring.ch07;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringMain {
public static void main(String[] args) {
//applicationContex2.xml 설정파일을 읽어들여 스프링 컨테이너를 생성
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
//DI 프로퍼티 설정방식으로 생성된 객체 호출
RegisterService service = (RegisterService)context.getBean("registerService");
service.write();
context.close();
}
}
RegisterDAO의 write() 메서드 실행
RegisterDAO의 insert() 메서드 실행
'쌍용교육(JAVA) > Spring' 카테고리의 다른 글
쌍용교육 -JSP수업 79일차 - ch08_SpringMVC(1) (0) | 2024.06.17 |
---|---|
쌍용교육 -JSP수업 79일차 - ch07_SpringDI(4) (0) | 2024.06.17 |
쌍용교육 -JSP수업 78일차 - ch07_SpringDI(3) (0) | 2024.06.14 |
쌍용교육 -JSP수업 77일차 - ch07_SpringDI(1) (0) | 2024.06.12 |
이클립스 스프링 설치 방법 (0) | 2024.06.11 |