DI
의존성 주입을 사용한다면 무엇이 좋은가?
의존성 주입을 이용한다면 의존하는(필요한) 클래스를 직접 생성하는것이 아닌, 주입해 줌으로써, 객체간의 결합도를 줄이고, 좀더 유연한 코드를 생성할 수 있게 된다.
즉, 한 클래스를 수정하였을 때, 다른 클래스도 수정해야 하는 상황을 막아줄 수 있다.
IoC
- IOC란 "제어의 역전"이라는 의미로, 말 그대로 메소드가 객체의 호출 작업을 개발자가 하는것이 아니라, 외부에서 결정되는 것을 의미한다.
- 프로그램 제어권을 Frameowrk가 가져가는 것
- 생성된 객체의 생명주기 '전체'에 대한 권한과 관리를 프레임 워크에 주어, 개발자는 비지니스 로직에만 신경쓰면 된다.
- 개발자가 설정만 하면 (annotaion , xml 등을 통해 설정해놓으면) Container가 알아서 처리한다.
이것을 잘 보면 Database의 Pooling 개념하고 비슷한거 같다.
스프링 ➡ 객체를 만들어놓고 가져다 쓴다.
데이터베이스 ➡ 연결을 만들어놓고 가져다 사용한다.
다형성을 생각할때는 라이브러리를 만드는 것이 아니라 호출해서 사용하는 것이 편하다.
MVC Model 2를 하게되면 다른 곳에서 호출해서 사용할 수 있다.
어제 작성했던 인터페이스 구현받았던 코드를 Spring 을 사용해 작성해보자.
Hello.java
, HelloBean1.java
, HelloBean2.java
는 동일하다.
context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean name="helloBean1" class="com.exam.spring04.HelloBean1"></bean>
<bean name="helloBean2" class="com.exam.spring04.HelloBean2"></bean>
</beans>
App.java
package com.exam.spring04;
import org.springframework.context.support.GenericXmlApplicationContext;
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 1
GenericXmlApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/exam/spring04/context.xml");
// 2
Hello hello = (Hello)ctx.getBean("helloBean1");
// 3
hello.sayHello("홍길동");
hello = (Hello)ctx.getBean("helloBean2");
hello.sayHello("박문수");
// 4
ctx.close();
}
}
1
: 객체를 미리 선언context.xml
파일에 경로를 입력한다.GenericXmlApplicationContext
클래스를 통해 인스턴스화 해서 ctx
변수에 저장한다.ctx
에 저장될 내용은 context.xml
파일 내용이다.2
: Spring에 선언된 객체를 가져오기ctx
의 Bean
을 가져온다.ctx
변수에 저장된 helloBean1
이라는 이름의 com.exam.spring04.HelloBean1
클래스를 가져와 Hello
클래스를 인스턴스화 해서 hello
변수에 저장한다.3
hello에 저장된 메서드 호출hello
에 저장된 ctx
에서 Bean4
: 작업 완료시 Spring 닫아주기GenericXmlApplicationContext
을 .close()
메서드를 사용해 닫아준다.19 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [com/exam/spring04/context.xml]
09:50:46.021 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
09:50:46.048 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'helloBean1'
09:50:46.062 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'helloBean2'
홍길동님 안녕하세요?
Hello 박문수
09:50:46.090 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 09:50:46 KST 2022
singleton
: 클래스
prototype
: 인스턴스
pom.xml
dependency에 Spring 라이브러리 추가Singleton
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd">
<bean name="helloBean1" class="com.exam.spring01.HelloBean" scope="singleton"></bean>
<bean name="helloBean2" class="com.exam.spring01.HelloBean" scope="singleton"></bean>
</beans>
package com.exam.spring01;
public class HelloBean {
public HelloBean() {
// TODO Auto-generated constructor stub
System.out.println("HelloBean() 생성자 호출");
}
public void sayHello(String name) {
System.out.println(name + "님 안녕하세요");
}
}
package com.exam.spring01;
import org.springframework.context.support.GenericXmlApplicationContext;
public class App {
public static void main( String[] args ) {
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring01/context.xml");
ctx.close();
}
}
10:27:35.724 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [com/exam/spring01/context.xml]
10:27:35.726 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
10:27:35.758 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'helloBean1'
HelloBean() 생성자 호출
10:27:35.773 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'helloBean2'
HelloBean() 생성자 호출
10:27:35.810 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 10:27:35 KST 2022
singleton scope를 빼고 bean을 입력해본다면?
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd">
<bean name="helloBean1" class="com.exam.spring01.HelloBean" scope="singleton"></bean>
<bean name="helloBean2" class="com.exam.spring01.HelloBean" scope="singleton"></bean>
<bean name="helloBean2" class="com.exam.spring01.HelloBean"></bean>
</beans>
10:30:52.156 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 3 bean definitions from class path resource [com/exam/spring01/context.xml]
10:30:52.158 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
10:30:52.181 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'helloBean1'
HelloBean() 생성자 호출
10:30:52.196 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'helloBean2'
HelloBean() 생성자 호출
10:30:52.196 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'helloBean3'
HelloBean() 생성자 호출
10:30:52.226 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 10:30:52 KST 2022
생성자가 호출된다.
이를 통해 알 수 있는 점은?
Bean의 생성시점? 로딩은 안해도 생성이 된다.
scope의 디폴트 값은 singleton이다.
package com.exam.spring01;
import org.springframework.context.support.GenericXmlApplicationContext;
public class App {
public static void main( String[] args ) {
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring01/context.xml");
// helloBean
HelloBean helloBean11 = (HelloBean)ctx.getBean("helloBean1");
HelloBean helloBean12 = (HelloBean)ctx.getBean("helloBean2");
HelloBean helloBean13 = (HelloBean)ctx.getBean("helloBean1");
helloBean11.sayHello("홍길동");
helloBean12.sayHello("박문수");
System.out.println(helloBean11);
System.out.println(helloBean12);
System.out.println(helloBean13);
ctx.close();
}
}
10:33:50.367 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 3 bean definitions from class path resource [com/exam/spring01/context.xml]
10:33:50.370 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
10:33:50.403 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'helloBean1'
HelloBean() 생성자 호출
10:33:50.416 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'helloBean2'
HelloBean() 생성자 호출
10:33:50.417 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'helloBean3'
HelloBean() 생성자 호출
홍길동님 안녕하세요
박문수님 안녕하세요
com.exam.spring01.HelloBean@505fc5a4
com.exam.spring01.HelloBean@5fbdfdcf
com.exam.spring01.HelloBean@505fc5a4
10:33:50.445 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 10:33:50 KST 2022
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd">
<bean name="helloBean1" class="com.exam.spring02.HelloBean" scope="prototype"></bean>
<bean name="helloBean2" class="com.exam.spring02.HelloBean" scope="prototype"></bean>
</beans>
package com.exam.spring02;
import org.springframework.context.support.GenericXmlApplicationContext;
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring02/context.xml");
ctx.close();
}
}
10:36:55.059 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [com/exam/spring02/context.xml]
10:36:55.061 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
10:36:55.124 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 10:36:55 KST 2022
package com.exam.spring02;
import org.springframework.context.support.GenericXmlApplicationContext;
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring02/context.xml");
HelloBean helloBean11 = (HelloBean)ctx.getBean("helloBean1");
HelloBean helloBean12 = (HelloBean)ctx.getBean("helloBean2");
HelloBean helloBean13 = (HelloBean)ctx.getBean("helloBean1");
helloBean11.sayHello("홍길동");
helloBean12.sayHello("박문수");
System.out.println(helloBean11);
System.out.println(helloBean12);
System.out.println(helloBean13);
ctx.close();
}
}
10:38:17.910 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [com/exam/spring02/context.xml]
10:38:17.913 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
HelloBean() 생성자 호출
HelloBean() 생성자 호출
HelloBean() 생성자 호출
홍길동님 안녕하세요
박문수님 안녕하세요
com.exam.spring02.HelloBean@76c3e77a
com.exam.spring02.HelloBean@78123e82
com.exam.spring02.HelloBean@67c33749
10:38:17.994 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 10:38:17 KST 2022
Scope가 싱글톤이다?
최초 프로그램 시작 시점에 객체를 생성한다.
Scope가 프로토타입이다?
객체 필요시에만(생성자 선언 했을 경우) 객체를 생성한다.
생성자의 주입
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd">
<bean name="helloBean1" class="com.exam.spring03.HelloBean" scope="prototype"></bean>
</beans>
package com.exam.spring03;
public class HelloBean {
private String name;
public HelloBean() {
// TODO Auto-generated constructor stub
System.out.println("HelloBean() 생성자 호출");
this.name = "홍길동";
}
public HelloBean(String name) {
// TODO Auto-generated constructor stub
System.out.println("HelloBean(String name) 생성자 호출");
this.name = name;
}
public void sayHello() {
System.out.println(this.name + "님 안녕하세요");
}
}
package com.exam.spring03;
import org.springframework.context.support.GenericXmlApplicationContext;
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring03/context.xml");
HelloBean helloBean11 = (HelloBean)ctx.getBean("helloBean1");
helloBean11.sayHello();
ctx.close();
}
}
10:48:32.947 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [com/exam/spring03/context.xml]
10:48:32.949 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
HelloBean() 생성자 호출
홍길동님 안녕하세요
10:48:33.015 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 10:48:32 KST 2022
위에꺼는 디폴트 생성자였고 이번엔 생성자의 오버라이딩?
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd">
<bean name="helloBean1" class="com.exam.spring03.HelloBean" scope="prototype"></bean>
<bean name="helloBean2" class="com.exam.spring03.HelloBean" scope="prototype">
<constructor-arg>
<value>박문수</value>
</constructor-arg>
</bean>
</beans>
Hello.java
는 동일해서 패스
package com.exam.spring03;
import org.springframework.context.support.GenericXmlApplicationContext;
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring03/context.xml");
HelloBean helloBean11 = (HelloBean)ctx.getBean("helloBean1");
helloBean11.sayHello();
HelloBean helloBean12 = (HelloBean)ctx.getBean("helloBean2");
helloBean12.sayHello();
ctx.close();
}
}
10:53:35.105 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [com/exam/spring03/context.xml]
10:53:35.107 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
HelloBean() 생성자 호출
홍길동님 안녕하세요
HelloBean(String name) 생성자 호출
박문수님 안녕하세요
10:53:35.201 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 10:53:35 KST 2022
생성자 주입시 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<bean name="helloBean1" class="com.exam.spring03.HelloBean" scope="prototype"></bean>
<bean name="helloBean2" class="com.exam.spring03.HelloBean" scope="prototype">
<constructor-arg>
<value>박문수</value>
</constructor-arg>
</bean>
<bean name="helloBean3" class="com.exam.spring03.HelloBean" scope="prototype">
<constructor-arg value="이몽룡" />
</bean>
</beans>
package com.exam.spring03;
import org.springframework.context.support.GenericXmlApplicationContext;
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring03/context.xml");
HelloBean helloBean11 = (HelloBean)ctx.getBean("helloBean1");
helloBean11.sayHello();
HelloBean helloBean12 = (HelloBean)ctx.getBean("helloBean2");
helloBean12.sayHello();
HelloBean helloBean13 = (HelloBean)ctx.getBean("helloBean3");
helloBean13.sayHello();
ctx.close();
}
}
10:55:55.975 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 3 bean definitions from class path resource [com/exam/spring03/context.xml]
10:55:55.977 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
HelloBean() 생성자 호출
홍길동님 안녕하세요
HelloBean(String name) 생성자 호출
박문수님 안녕하세요
HelloBean(String name) 생성자 호출
이몽룡님 안녕하세요
10:55:56.063 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 10:55:55 KST 2022
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd">
<bean name="helloBean1" class="com.exam.spring04.HelloBean" scope="prototype"></bean>
<bean name="helloBean2" class="com.exam.spring04.HelloBean" scope="prototype">
<constructor-arg>
<value>박문수</value>
</constructor-arg>
</bean>
<bean name="helloBean3" class="com.exam.spring04.HelloBean" scope="prototype">
<constructor-arg value="이몽룡" />
</bean>
<bean name="helloBean4" class="com.exam.spring04.HelloBean" scope="prototype">
<constructor-arg>
<value>문수</value>
</constructor-arg>
<constructor-arg>
<value>박</value>
</constructor-arg>
</bean>
</beans>
package com.exam.spring04;
public class HelloBean {
private String name;
public HelloBean() {
// TODO Auto-generated constructor stub
System.out.println("HelloBean() 생성자 호출");
this.name = "홍길동";
}
public HelloBean(String name) {
// TODO Auto-generated constructor stub
System.out.println("HelloBean(String name) 생성자 호출");
this.name = name;
}
public HelloBean(String firstName, String secondName) {
// TODO Auto-generated constructor stub
System.out.println("HelloBean(String firstName, String secondName) 생성자 호출");
this.name = secondName + " " + firstName;
}
public void sayHello() {
System.out.println(this.name + "님 안녕하세요");
}
}
package com.exam.spring04;
import org.springframework.context.support.GenericXmlApplicationContext;
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring04/context.xml");
HelloBean helloBean11 = (HelloBean)ctx.getBean("helloBean1");
helloBean11.sayHello();
HelloBean helloBean12 = (HelloBean)ctx.getBean("helloBean2");
helloBean12.sayHello();
HelloBean helloBean13 = (HelloBean)ctx.getBean("helloBean3");
helloBean13.sayHello();
HelloBean helloBean14 = (HelloBean)ctx.getBean("helloBean4");
helloBean14.sayHello();
ctx.close();
}
}
11:14:47.285 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 4 bean definitions from class path resource [com/exam/spring04/context.xml]
11:14:47.287 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
HelloBean() 생성자 호출
홍길동님 안녕하세요
HelloBean(String name) 생성자 호출
박문수님 안녕하세요
HelloBean(String name) 생성자 호출
이몽룡님 안녕하세요
HelloBean(String firstName, String secondName) 생성자 호출
박 문수님 안녕하세요
11:14:47.374 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 11:14:47 KST 2022
게시판
앞에서 해온것은 단순히 문자열만 받았지만
이번에 해볼것은? 사용자적인 객체를 받는다.
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd">
<!-- WriteAction action = new WriteAction(new BoardTO) 와 동일한 개념이다. -->
<bean name="action2" class="com.exam.spring05.WriteAction" scope="prototype" >
<constructor-arg>
<bean class="com.exam.spring05.BoardTO" />
</constructor-arg>
</bean>
</beans>
package com.exam.spring05;
import org.springframework.context.support.GenericXmlApplicationContext;
public class App02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring05/context.xml");
WriteAction action = (WriteAction)ctx.getBean("action1");
action.excute();
ctx.close();
}
}
먼저 인스턴스화 해서 가져오고 싶어!
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd">
<!-- 먼저 인스턴스화해서 가져오고 싶어 -->
<bean id="to" class="com.exam.spring05.BoardTO" />
<!-- WriteAction action = new WriteAction(to) 와 동일한 개념이다. -->
<bean name="action3" class="com.exam.spring05.WriteAction" scope="prototype" >
<constructor-arg>
<ref bean="to" />
</constructor-arg>
</bean>
</beans>
package com.exam.spring05;
import org.springframework.context.support.GenericXmlApplicationContext;
public class App03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring05/context.xml");
WriteAction action = (WriteAction)ctx.getBean("action3");
action.excute();
ctx.close();
}
}
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd">
<bean id="to" class="com.exam.spring07.model1.BoardTO" >
<constructor-arg value="홍길동" />
</bean>
<bean id="dao" class="com.exam.spring07.model1.BoardDAO" >
<constructor-arg ref="to" />
</bean>
<bean id="action" class="com.exam.spring07.model2.WriteAction" >
<constructor-arg ref="dao" />
</bean>
</beans>
xml 에서는 실행될 준비를 한다.
아래 Java 프로그램을 보면, 호출될 객체는 action 이다.
action을 실행하기 위해 필요한 인자값들은 context.xml
에서 조립한다.
BoardDAO
가 필요하기 때문에, dao를 만들었다.BoardTO
가 필요하기 때문에, to를 만들었다.package com.exam.spring07.model1;
public class BoardTO {
private String writer;
public BoardTO(String writer) {
// TODO Auto-generated constructor stub
System.out.println("BoardTO(String writer) 호출");
this.writer = writer;
}
}
package com.exam.spring07.model1;
public class BoardDAO {
private BoardTO to;
public BoardDAO(BoardTO to) {
// TODO Auto-generated constructor stub
System.out.println("BoardDAO(BoardTO to) 호출");
this.to = to;
}
}
package com.exam.spring07.model2;
import com.exam.spring07.model1.BoardDAO;
public class WriteAction {
private BoardDAO dao;
public WriteAction(BoardDAO dao) {
// TODO Auto-generated constructor stub
System.out.println("WriteAction(BoardDAO dao) 호출");
this.dao = dao;
}
public void execute() {
System.out.println("execute() : " + dao);
}
}
package com.exam.spring07;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.exam.spring07.model2.WriteAction;
public class App02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring07/context.xml");
WriteAction action = (WriteAction)ctx.getBean("action");
action.execute();
ctx.close();
}
}
context.xml
에서는 조립을 해주고
App02.java
에서는 호출만 해준다.
실행 결과는 다음과 같다.
xml의 순서대로
BoardTO를 선언해준다.
인자값으로 문자열 형식의 writer
를 받는다.
➡ writer
= 홍길동
그 다음 BoardDAO를 선언해준다.
인자값으로 BoardTO 형식의 to를 받는다.
➡ to
= 홍길동 인자값이 들어간 BoardTO
마지막으로 WriteAction을 선언해준다.
인자값으로 BoardDAO 형식의 dao를 받는다.
➡ dao
= 홍길동 인자값이 들어간 BoardTO를 내부적으로 가공(처리)한 데이터
App02.java
에서는 xml에서 조립해준 정보들을 action이라는 변수에 저장했다가, 실행시킨다.
14:08:47.417 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 3 bean definitions from class path resource [com/exam/spring07/context.xml]
14:08:47.419 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
14:08:47.447 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'to'
BoardTO(String writer) 호출
14:08:47.478 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'dao'
BoardDAO(BoardTO to) 호출
14:08:47.479 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'action'
WriteAction(BoardDAO dao) 호출
execute() : com.exam.spring07.model1.BoardDAO@66f57048
14:08:47.506 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 14:08:47 KST 2022
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd">
<bean id="to" class="com.exam.spring04.BoardTO" scope="prototype" >
<property name="seq" value="1" />
<property name="subject" value="제목" />
</bean>
<bean id="action" class="com.exam.spring04.WriteAction" scope="prototype" >
<property name="to" ref="to" />
</bean>
</beans>
package com.exam.spring04;
public class BoardTO {
private int seq;
private String subject;
public int getSeq() {
return seq;
}
public void setSeq(int seq) {
System.out.println("setSeq(int seq)");
this.seq = seq;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
System.out.println("setSubject(String subject)");
this.subject = subject;
}
}
package com.exam.spring04;
public class WriteAction {
private BoardTO to;
public BoardTO getTo() {
return to;
}
public void setTo(BoardTO to) {
this.to = to;
}
public void execute() {
System.out.println("execute() 호출");
System.out.println(to.getSeq());
System.out.println(to.getSubject());
}
}
package com.exam.spring04;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.exam.spring02.BoardTO;
public class App02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring04/context.xml");
WriteAction action = (WriteAction)ctx.getBean("action");
action.execute();
ctx.close();
}
}
실행 결과
14:47:25.595 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [com/exam/spring04/context.xml]
14:47:25.597 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
setSeq(int seq)
setSubject(String subject)
execute() 호출
1
제목
14:47:25.705 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 14:47:25 KST 2022
list
hash set
hash map
<?xml version="1.0" encoding= "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.3.xsd">
<bean id="listTO" class="com.exam.spring06.BoardListTO" scope="prototype">
<property name="userMaps">
<map>
<entry key="key1">
<value>데이터 1</value>
</entry>
<entry key="key2">
<value>데이터 2</value>
</entry>
</map>
</property>
<property name="boardMaps">
<map>
<entry key="key1">
<bean id="boardTO" class="com.exam.spring06.BoardTO" scope="prototype">
<property name="seq" value="1" />
<property name="subject" value="제목1" />
</bean>
</entry>
<entry key="key2">
<bean id="boardTO" class="com.exam.spring06.BoardTO" scope="prototype">
<property name="seq" value="2" />
<property name="subject" value="제목2" />
</bean>
</entry>
</map>
</property>
</bean>
</beans>
package com.exam.spring06;
public class BoardTO {
private int seq;
private String subject;
public int getSeq() {
return seq;
}
public void setSeq(int seq) {
System.out.println("setSeq(int seq)");
this.seq = seq;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
System.out.println("setSubject(String subject)");
this.subject = subject;
}
}
package com.exam.spring06;
import java.util.HashMap;
public class BoardListTO {
private HashMap<String, String> userMaps;
private HashMap<String, BoardTO> boardMaps;
public HashMap<String, String> getUserMaps() {
return userMaps;
}
public void setUserMaps(HashMap<String, String> userMaps) {
this.userMaps = userMaps;
}
public HashMap<String, BoardTO> getBoardMaps() {
return boardMaps;
}
public void setBoardMaps(HashMap<String, BoardTO> boardMaps) {
this.boardMaps = boardMaps;
}
}
package com.exam.spring06;
import org.springframework.context.support.GenericXmlApplicationContext;
public class App02 {
public static void main(String[] args) {
GenericXmlApplicationContext ctx
= new GenericXmlApplicationContext("classpath:com/exam/spring06/context.xml");
BoardListTO listTO = (BoardListTO)ctx.getBean("listTO");
// value 값 가져오기
for(String value : listTO.getUserMaps().values()) {
System.out.println(value);
}
// key 값 가져오기
for(String key : listTO.getUserMaps().keySet()) {
System.out.println(key);
}
// BoardTO 값 가져오기
for(BoardTO to : listTO.getBoardMaps().values()) {
System.out.print(to.getSeq()+" ");
System.out.println(to.getSubject());
}
ctx.close();
}
}
16:26:48.214 [main] DEBUG o.s.b.f.xml.XmlBeanDefinitionReader - Loaded 1 bean definitions from class path resource [com/exam/spring06/context.xml]
16:26:48.216 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@27fe3806
setSeq(int seq)
setSubject(String subject)
setSeq(int seq)
setSubject(String subject)
데이터 1
데이터 2
key1
key2
1 제목1
2 제목2
16:26:48.338 [main] DEBUG o.s.c.s.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@27fe3806, started on Fri Jul 01 16:26:48 KST 2022
AOP
객체 활용(생성, 소멸 관리)
서블릿에서의 Filter 기능과 동일하다
Filter
: 전처리 / 후처리
Spring MVC(java ➡ JSP)
Q : 똑같으면 원래 기존꺼도 사용해도 좋잖아요?
분업화의 의미에서 서로 다르게 작업하려고 그렇게 썻다.