단순 코드 기록/Spring

Spring_@Resource

일일일코_장민기 2024. 2. 8. 12:32
728x90
Person

package com.spring;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;

public class Person {

private String userName;
private int age;

@Resource(name = "pet02")
private Cat cat;

@Override
public String toString() {
return "Person [userName=" + userName + ", age=" + age + ", cat=" + cat + "]";
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String userName, int age, Cat cat) {
super();
this.userName = userName;
this.age = age;
this.cat = cat;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
}

 

 

 

person.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">
<!-- Namespaces에서 context 추가 -->
<context:annotation-config></context:annotation-config>

<bean id="pet01" class="cohttp://m.spring.Cat">
<constructor-arg name="catName" value="야옹이"/>
<constructor-arg name="catAge" value="20"/>
</bean>

<bean id="pet02" class="cohttp://m.spring.Cat">
<constructor-arg name="catName" value="몽크"/>
<constructor-arg name="catAge" value="10"/>
</bean>

<bean id="person" class="cohttp://m.spring.Person">
<property name="userName" value="홍길동"/>
<property name="age" value="10"/>
</bean>

</beans>

 

 

 

DTO에서 @Resource를 부여함으로서 xml에서 주입받을 대상을 설정

 

 

 

 

 

 

 

 

'단순 코드 기록 > Spring' 카테고리의 다른 글

Spring_SpEL  (0) 2024.02.08
Spring_Value  (0) 2024.02.08
Spring_Multi Config  (0) 2024.02.08
Spring_Autowire  (0) 2024.02.08
Spring_Scope  (0) 2024.02.08