import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service = ctx.getBean("service", UserService.class);
List<String> list = service.list();
System.out.println(list);
UserService service2 = ctx.getBean("service2", UserService.class);
List<String> list2 = service2.list();
System.out.println(list2);
}
}
UserService.java
package com.service;
import java.util.List;
import com.dao.UserDAO;
// POJO Class
public class UserService {
// property (인스턴스 변수)
UserDAO dao;
// setter 메서드 주입
public UserService(UserDAO dao) {
this.dao = dao;
}
public List<String> list(){
return dao.list();
}
}
UserDAO.java
package com.dao;
import java.util.Arrays;
import java.util.List;
public class UserDAO {
// DB 연동 가정
public List<String> list(){
List<String> list = Arrays.asList("고길동", "신길동", "홍길동");
return list;
}
}
user.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:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dao" class="com.dao.UserDAO" /> <!-- bean 태그로 등록하면 기본 생성자 호출 가능 -->
<!-- 다음 코드와 동일
UserDAO userDAO = new UserDAO();
-->
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<constructor-arg name="dao" ref="dao" />
</bean>
<!-- 다음 코드와 동일
UserService service = new UserService();
service.setDAO(UserDAO);
-->
<!--
c namespace
-->
<bean id="service2" class="com.service.UserService" c:dao-ref="dao" />
<!--
UserService service2= new UserService(dao);
-->
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service =
ctx.getBean("service", UserService.class);
System.out.println(service.getMesg());
UserService service2 =
ctx.getBean("service2", UserService.class);
System.out.println(service2.getMesg());
}
}
UserService.java
package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// POJO Class
public class UserService {
String mesg; // null ==> 외부에서 문자열을 주입
// 생성자 주입
public UserService(String m) {
this.mesg = m;
}
public String getMesg() {
return mesg;
}
}
user.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:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="service" class="com.service.UserService">
<constructor-arg name="m" value="helloWorld" />
</bean>
<!--
위의 코드는 다음 코드와 동일하다.
UserService service = new UserService("helloWorld");
-->
<!--
c namespace
-->
<bean id="service2" class="com.service.UserService" c:m="happyworld" />
</beans>
* 출력 화면
Spring Injection_shortcut_p 2
MainClass.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service = ctx.getBean("service", UserService.class);
List<String> list = service.list();
System.out.println(list);
UserService service2 = ctx.getBean("service2", UserService.class);
List<String> list2 = service2.list();
System.out.println(list2);
}
}
UserService.java
package com.service;
import java.util.List;
import com.dao.UserDAO;
// POJO Class
public class UserService {
// property (인스턴스 변수)
UserDAO dao;
// setter 메서드 주입
public UserService(UserDAO dao) {
this.dao = dao;
}
public List<String> list(){
return dao.list();
}
}
UserDAO.java
package com.dao;
import java.util.Arrays;
import java.util.List;
public class UserDAO {
// DB 연동 가정
public List<String> list(){
List<String> list = Arrays.asList("고길동", "신길동", "홍길동");
return list;
}
}
user.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:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dao" class="com.dao.UserDAO" /> <!-- bean 태그로 등록하면 기본 생성자 호출 가능 -->
<!-- 다음 코드와 동일
UserDAO userDAO = new UserDAO();
-->
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<constructor-arg name="dao" ref="dao" />
</bean>
<!-- 다음 코드와 동일
UserService service = new UserService();
service.setDAO(UserDAO);
-->
<!--
c namespace
-->
<bean id="service2" class="com.service.UserService" c:dao-ref="dao" />
<!--
UserService service2= new UserService(dao);
-->
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service =
ctx.getBean("service", UserService.class);
System.out.println(service.getMesg());
UserService service2 =
ctx.getBean("service2", UserService.class);
System.out.println(service2.getMesg());
}
}
UserService.java
package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// POJO Class
public class UserService {
String mesg; // null ==> 외부에서 문자열을 주입
// setter 메서드 주입
public void setMesg(String mesg) {
System.out.println("setMesg 메서드");
this.mesg = mesg;
}
public String getMesg() {
return mesg;
}
}
user.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<property name="mesg" value="HelloWorld" /> <!-- System.out.println("setMesg 메서드"); property에 의해 출력 -->
</bean>
<!-- 아래 코드와 동일
UserService service = new UserService();
service.setMesg("HelloWorld");
-->
<bean id="service2" class="com.service.UserService" p:mesg="HappyWorld" />
<!-- 아래 코드와 동일
UserService service2 = new UserService();
service2.setMesg("HelloWorld");
-->
</beans>
* 출력 화면
Spring Injection_shortcut_p 2
MainClass.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service = ctx.getBean("service", UserService.class);
System.out.println(service.getMesg());
System.out.println(service);
UserService service2 = ctx.getBean("service2", UserService.class);
System.out.println(service2.getMesg());
System.out.println(service2);
UserService service3 = ctx.getBean("service3", UserService.class);
System.out.println(service3.getMesg());
System.out.println(service3);
UserService service4 = ctx.getBean("service4", UserService.class);
System.out.println(service4.getMesg());
System.out.println(service4);
}
}
UserService.java
package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// POJO Class
public class UserService {
// property (인스턴스 변수)
String mesg; // null ==> 외부에서 문자열을 주입
int num; // 0 ==> 외부에서 값을 주입
// setter 메서드 주입
public void setMesg(String mesg) {
this.mesg = mesg;
}
public void setNum(int num) {
this.num = num;
}
public void setMesgNum(String mesg, int num) {
this.mesg = mesg;
this.num = num;
}
// getter
public String getMesg() {
return mesg;
}
public int getNum() {
return num;
}
// toString
@Override
public String toString() {
return "UserService [mesg=" + mesg + ", num=" + num + "]";
}
}
user.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<property name="mesg" value="HelloWorld" /> <!-- System.out.println("setMesg 메서드"); property에 의해 출력 -->
</bean>
<!--
UserService service = new UserService();
service.setMesg("HelloWorld");
-->
<bean id="service2" class="com.service.UserService">
<property name="num" value="20" />
</bean>
<bean id="service3" class="com.service.UserService">
<property name="mesg" value="Happy" />
<property name="num" value="30" />
</bean>
<!-- setter based injection -->
<!--
p namespace 사용
-->
<bean id="service4" class="com.service.UserService" p:mesg="Lucky" p:num="40"/>
</beans>
* 출력 화면
Spring Injection_shortcut_p 3
MainClass.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service = ctx.getBean("service", UserService.class);
List<String> list = service.list();
System.out.println(list);
UserService service2 = ctx.getBean("service2", UserService.class);
List<String> list2 = service.list();
System.out.println(list2);
}
}
UserService.java
package com.service;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.dao.UserDAO;
// POJO Class
public class UserService {
// property (인스턴스 변수)
UserDAO dao;
// setter 메서드 주입
public void setDao(UserDAO dao) {
this.dao = dao;
}
public List<String> list(){
return dao.list();
}
}
UserDAO.java
package com.dao;
import java.util.Arrays;
import java.util.List;
public class UserDAO {
// DB 연동 가정
public List<String> list(){
List<String> list = Arrays.asList("고길동", "신길동", "홍길동");
return list;
}
}
user.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDAO" class="com.dao.UserDAO" /> <!-- bean 태그로 등록하면 기본 생성자 호출 가능 -->
<!-- 아래 코드와 동일
UserDAO userDAO = new UserDAO();
-->
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<property name="dao" ref="userDAO"></property>
</bean>
<!-- 아래 코드와 동일
UserService service = new UserService();
service.setDAO(UserDAO);
-->
<!--
p namespace 설정
-->
<bean id="service2" class="com.service.UserService" p:dao-ref="userDAO" />
<!-- 아래 코드와 동일
UserService service = new UserService();
service2.setDAO(UserDAO);
-->
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service =
ctx.getBean("service", UserService.class);
System.out.println(service.getMesg());
}
}
UserService.java
package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// POJO Class
public class UserService {
String mesg; // null ==> 외부에서 문자열을 주입
// setter 메서드 주입
public void setMesg(String mesg) {
System.out.println("setMesg 메서드");
this.mesg = mesg;
}
public String getMesg() {
return mesg;
}
}
user.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.xsd">
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<property name="mesg" value="HelloWorld" /> <!-- System.out.println("setMesg 메서드"); property에 의해 출력 -->
</bean>
<!-- 다음 코드와 동일
UserService service = new UserService();
service.setMesg("HelloWorld");
-->
</beans>
* 출력 화면
Spring Setter Injection 2
MainClass.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service =
ctx.getBean("service", UserService.class);
System.out.println(service.getMesg());
System.out.println(service);
UserService service2 =
ctx.getBean("service2", UserService.class);
System.out.println(service2.getMesg());
System.out.println(service2);
UserService service3 =
ctx.getBean("service3", UserService.class);
System.out.println(service3.getMesg());
System.out.println(service3);
}
}
UserService.java
package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// POJO Class
public class UserService {
// property (인스턴스 변수)
String mesg; // null ==> 외부에서 문자열을 주입
int num; // 0 ==> 외부에서 값을 주입
// setter 메서드 주입
public void setMesg(String mesg) {
this.mesg = mesg;
}
public void setNum(int num) {
this.num = num;
}
public void setMesgNum(String mesg, int num) {
this.mesg = mesg;
this.num = num;
}
// getter
public String getMesg() {
return mesg;
}
public int getNum() {
return num;
}
// toString
@Override
public String toString() {
return "UserService [mesg=" + mesg + ", num=" + num + "]";
}
}
user.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.xsd">
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<property name="mesg" value="HelloWorld" /> <!-- System.out.println("setMesg 메서드"); property에 의해 출력 -->
</bean>
<!-- 다음 코드와 동일
UserService service = new UserService();
service.setMesg("HelloWorld");
-->
<bean id="service2" class="com.service.UserService">
<property name="num" value="20" />
</bean>
<bean id="service3" class="com.service.UserService">
<property name="mesg" value="Happy" />
<property name="num" value="35" />
</bean>
<!-- setter based injection -->
</beans>
* 출력 화면
Spring Setter Injection 3_Service_dao 추가
MainClass.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service = ctx.getBean("service", UserService.class);
List<String> list = service.list();
System.out.println(list);
}
}
UserService.java
package com.service;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.dao.UserDAO;
// POJO Class
public class UserService {
// property (인스턴스 변수)
UserDAO dao;
// setter 메서드 주입
public void setDao(UserDAO dao) {
this.dao = dao;
}
public List<String> list(){
return dao.list();
}
}
UserDAO.java
package com.dao;
import java.util.Arrays;
import java.util.List;
public class UserDAO {
// DB 연동 가정
public List<String> list(){
List<String> list = Arrays.asList("고길동", "홍길동", "신길동");
return list;
}
}
user.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.xsd">
<bean id="userDAO" class="com.dao.UserDAO" /> <!-- bean 태그로 등록하면 기본 생성자 호출 가능 -->
<!--
UserDAO userDAO = new UserDAO();
-->
<bean id="service" class="com.service.UserService"> <!-- id는 식별 가능한 값, class는 클래스 입력 -->
<property name="dao" ref="userDAO"></property>
</bean>
<!-- 다음 코드와 동일
UserService service = new UserService();
service.setDAO(UserDAO);
-->
</beans>
생성자 주입 방식을 권장하는 이유 - 단일 책임의 원칙 생성자의 인자가 많아지면서 하나의 클래스가 많은 책임을 떠안는다는 걸 알게된다. 그래서 Constructor Injection을 사용해 의존관계, 복잡성을 쉽게 알 수 있다.
- 의존성이 숨는다 DI(Dependency Injection) 컨테이너를 사용한다는 것은 클래스가 자신의 의존성만 책임지는게 아니다. 제공된 의존성 또한 책임진다. 그래서 클래스가 어떤 의존성을 책임지지 않을 때, 메소드나 생성자를 통해 커뮤니케이션이 되어야한다. 하지만 Field Injection은 숨은 의존성만 제공해준다.
- DI 컨테이너의 결합성과 테스트의 용이성 DI 프레임워크의 핵심은 관리되는 클래스가 DI 컨테이너에 의존성이 없어야한다. 즉, 필요한 의존성을 전달하면 독립적으로 인스턴스화 할 수 있는 단순 POJO여야한다. DI 컨테이너 없이 유닛테스트에서 인스턴스화 가능하며, 테스트 가능하다. 컨테이너 결합성이 없다면 관리하거나 관리하지 않는 클래스를 사용할 수 있고, 다른 DI 컨테이너로 전환할 수 있다.
- Immutability (불변 객체) 생성자 주입 방식에서 필드는 final로 선언할 수 있다. 하지만, 필드 주입 방식에서는 final로 선언할 수 없어 객체가 변경 가능한 상태가 된다.
- 순환 의존성 생성자 주입 방식에서 순환 의존성을 가질 경우 BeanCurrentlyCreationExcepiton을 발생시킴으로써 순환 의존성을 알 수 있다. 1번 클래스가 2번 클래스를 참조하는데, 다시 2번 클래스가 1번 클래스를 참조하는 경우 순환 의존성이라고 부른다.
Spring Constructor Injection 1
MainClass.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service =
ctx.getBean("service", UserService.class);
System.out.println(service.getMesg());
}
}
UserService.java
package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// POJO Class
public class UserService {
String mesg; // null ==> 외부에서 문자열을 주입
// 생성자 주입
public UserService(String m) {
this.mesg = m;
}
public String getMesg() {
return mesg;
}
}
user.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.xsd">
<bean id="service" class="com.service.UserService">
<constructor-arg name="m" value="helloWorld" />
</bean>
<!--
위의 코드는 다음 코드와 동일하다.
UserService service = new UserService("helloWorld");
-->
</beans>
* 출력 화면
Spring Constructor Injection 2
MainClass.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service =
ctx.getBean("service", UserService.class);
System.out.println(service);
UserService service2 =
ctx.getBean("service2", UserService.class);
System.out.println(service2);
UserService service3 =
ctx.getBean("service3", UserService.class);
System.out.println(service3);
}
}
UserService.java
package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// POJO Class
public class UserService {
// 인스턴스
String mesg; // null ==> 외부에서 문자열을 주입
int num; // 0 ==> 외부에서 값을 주입
// 생성자 주입
public UserService(String m) {
this.mesg = m;
}
public UserService(int n) {
this.num = n;
}
// overriding
public UserService(String m, int n) {
this.mesg = m;
this.num = n;
}
// getter
public String getMesg() {
return mesg;
}
public int getNum() {
return num;
}
// toString
@Override
public String toString() {
return "UserService [mesg=" + mesg + ", num=" + num + "]";
}
}
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/user.xml");
UserService service = ctx.getBean("service", UserService.class);
List<String> list = service.list();
System.out.println(list);
}
}
UserService.java
package com.service;
import java.util.List;
import com.dao.UserDAO;
public class UserService {
UserDAO dao;
// 생성자 주입
public UserService(UserDAO dao) {
this.dao = dao;
}
public List<String> list(){
return dao.list();
}
}
UserDAO.java
package com.dao;
import java.util.Arrays;
import java.util.List;
public class UserDAO {
// DB 연동 가정
public List<String> list(){
List<String> list = Arrays.asList("홍길동", "고길동", "신길동");
return list;
}
}
user.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.xsd">
<bean id="dao" class="com.dao.UserDAO" />
<!-- UserDAO dao = new UserDAO(); -->
<bean id="service" class="com.service.UserService">
<constructor-arg name="dao" ref="dao" /> <!-- 생성자를 이용하여 주입하면 constructor-arg-->
</bean>
<!--
UserService service = new UserService();
-->
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.service.UserService;
public class Main {
public static void main(String[] args) {
// IoC 컨테이너 생성 ==> XXXXApplicationContext
ApplicationContext ctx = new GenericXmlApplicationContext("user.xml");
// 생성된 빈 접근
UserService service = ctx.getBean("service", UserService.class);
System.out.println(service.getMesg());
}
}
UserDAO.java
package com.dao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.service.UserService;
public class UserDAO {
private static final Logger logger = LoggerFactory.getLogger(UserService.class);
public UserDAO() {
logger.info("UserDAO 생성자");
}
}
UserService.java
package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// POJO Class
public class UserService {
private static final Logger logger = LoggerFactory.getLogger(UserService.class);
public UserService() {
logger.info("UserService 생성자");
}
// 메서드 추가
public String getMesg() {
return "UserService";
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("com/config/user.xml");
}
}
UserService.java
package com.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// POJO Class
public class UserService {
private static final Logger logger = LoggerFactory.getLogger(UserService.class);
public UserService() {
logger.info("UserService 생성자");
}
// 메서드 추가
public String getMesg() {
return "UserService";
}
}