실습 2일차_Injection_shortcut_p
Spring Injection_shortcut_p 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 ==> 외부에서 문자열을 주입
// 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>
* 출력 화면 :
출처
+ 강의 교재
'AI Bootcamp > Spring' 카테고리의 다른 글
[Spring] 실습 2일차_Injection_Constructor & Setter 혼합 (0) | 2022.03.12 |
---|---|
[Spring] 실습 2일차_Injection_shortcut_c (0) | 2022.03.11 |
[Spring] 실습 2일차_exam (0) | 2022.03.11 |
[Spring] 실습 2일차_Setter Injection (0) | 2022.03.10 |
[Spring] 실습 1일차_Injection (Constructor) (0) | 2022.03.10 |