실습 2일차_Injection_Constructor & Setter 혼합
Spring Injection_Constructor & Setter 혼합
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.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;
}
// setter 메서드 주입
public void setNum(int num) {
this.num = num;
}
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 + "]";
}
}
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="고길동" />
<property name="num" value="20"/>
<!-- 고길동이 생성자에 의해서 mesg 변수에 저장이 됨 -->
</bean>
<!--
c namespace
p namespace
-->
<bean id="service2" class="com.service.UserService" c:m="고길동" p:num="30" />
</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>
* 출력 화면 :
출처
+ 강의 교재
'AI Bootcamp > Spring' 카테고리의 다른 글
[Spring] 실습 3일차_Injection_Callback (0) | 2022.03.14 |
---|---|
[Spring] 실습 2일차_Injection_Collection(list & set & map &props) (0) | 2022.03.12 |
[Spring] 실습 2일차_Injection_shortcut_c (0) | 2022.03.11 |
[Spring] 실습 2일차_Injection_shortcut_p (0) | 2022.03.11 |
[Spring] 실습 2일차_exam (0) | 2022.03.11 |