실습 4일차_@Resource
Spring @Resource
MainClass.java
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.dto.Cat;
import com.dto.Person;
public class MainClass {
public static void main(String[] args) {
// IoC Container
ApplicationContext ctx =
new GenericXmlApplicationContext("classpath:com/config/test.xml");
Person p1 = ctx.getBean("p1", Person.class);
Cat c = p1.getCat();
System.out.printf("이름:%s 나이:%s 주소:%s \n", p1.getUsername(), p1.getUserage(), p1.getAddress()); // printformat
System.out.printf("고양이 이름:%s 나이:%s 성별:%s", c.getName(), c.getAge(), c.getSex());
}
}
Cat.java
package com.dto;
import org.springframework.beans.factory.annotation.Autowired;
public class Cat {
String name;
int age;
String sex;
public Cat() {
}
// 기본 생성자 안 만들고 인자있는 생성자 만듬
public Cat(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Cat [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
Person.java
package com.dto;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Person {
String username;
int userage;
String address;
//@Autowired(required = false)
//@Qualifier("cat1")
@Resource(name = "cat2")
Cat cat; // null
public Person() {
}
// 기본 생성자 안 만들고 인자있는 생성자 만듬
public Person(String username, int userage, String address, Cat cat) {
super();
this.username = username;
this.userage = userage;
this.address = address;
this.cat = cat;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getUserage() {
return userage;
}
public void setUserage(int userage) {
this.userage = userage;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "Person [username=" + username + ", userage=" + userage + ", address=" + address + ", cat=" + cat + "]";
}
}
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:annotation-config />
<bean id="cat1" class="com.dto.Cat"> <!-- 생성자로 주입 -->
<property name="name" value="나비" />
<property name="age" value="3" />
<property name="sex" value="암컷" />
</bean>
<bean id="cat2" class="com.dto.Cat"> <!-- 생성자로 주입 -->
<property name="name" value="나비2" />
<property name="age" value="3" />
<property name="sex" value="암컷" />
</bean>
<bean id="p1" class="com.dto.Person">
<property name="username" value="고길동" />
<property name="userage" value="30" />
<property name="address" value="서울" />
<!-- <property name="cat" ref="cat" /> 자동으로 주입 Cat.java @Autowired 코드 확인-->
</bean>
</beans>
<!-- @ 어노테이션 활성화 -->
<context:annotation-config />
* context 활성화 방법
[Namespaces 탭] → [context] Check → Configure Namespaces 알림창 OK 버튼 마우스로 클릭
* 출력 화면
출처
@Resource :
+ 강의 교재
'AI Bootcamp > Spring' 카테고리의 다른 글
[Spring] 실습 4일차_Component_Scan (0) | 2022.03.18 |
---|---|
[Spring] 실습 4일차_@Value_resourceBundle (0) | 2022.03.18 |
[Spring] 실습 3일차_I18N_MessageSource (0) | 2022.03.18 |
[Spring] 실습 3일차_Injection_Callback (0) | 2022.03.14 |
[Spring] 실습 2일차_Injection_Collection(list & set & map &props) (0) | 2022.03.12 |