실습 2일차_Injection_shortcut_c
Spring Injection_shortcut_c 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 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>
* 출력 화면 :
출처
+ 강의 교재
'AI Bootcamp > Spring' 카테고리의 다른 글
[Spring] 실습 2일차_Injection_Collection(list & set & map &props) (0) | 2022.03.12 |
---|---|
[Spring] 실습 2일차_Injection_Constructor & Setter 혼합 (0) | 2022.03.12 |
[Spring] 실습 2일차_Injection_shortcut_p (0) | 2022.03.11 |
[Spring] 실습 2일차_exam (0) | 2022.03.11 |
[Spring] 실습 2일차_Setter Injection (0) | 2022.03.10 |