단순 코드 기록/Spring

Spring_Value

일일일코_장민기 2024. 2. 8. 13:07
728x90
DTO에서 value 설정

package com.spring;

import org.springframework.beans.factory.annotation.Value;

public class Person {


//Value: 특정값을 주입할 때 사용(자바코드 외부의 리소스나 환경정보 설정값을 사용하는 등)
@Value("홍길동") // xml: <property name = "uesrname" value="홍길동">
private String username;

@Value("10") // java코드: int age = 10;
private int age;

@Override
public String toString() {
return "Person [username=" + username + ", age=" + age + "]";
}

public Person() {
super();
// TODO Auto-generated constructor stub
}

public Person(String username, int age) {
super();
this.username = username;
this.age = age;
}

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;
}


}

 

 

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">

<context:annotation-config/> <!-- 어노테이션 설정이 안 되면 null값 출력 -->

<bean id="person" class="cohttp://m.spring.Person"></bean>


</beans>

 

 

 

------------------------------------------------------------ ------------------------------------------------------------

 

 

test.properties

value.name=\uD64D\uAE38\uB3D9
value.age=33

 

 

 

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">

<context:annotation-config/> <!-- 어노테이션 설정이 안 되면 null값 출력 -->
<context:property-placeholder location="classpath:com\spring\test.properties"/> <!-- 외부파일 데이터(property)사용 -->

<bean id="person" class="cohttp://m.spring.Person">
<!-- value: property에 쓰여진 name과 value값 -->
<property name="username" value="${value.name}"/>
<property name="age" value="${value.age}"/>
</bean>


</beans>

 

 

 

------------------------------------------------------------ ------------------------------------------------------------

 

person

 

package com.config;

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

public class Person {

@Value("${jdbc.name}") // "$[property의 Key전체}"
private String name;

@Value("${value.age}") // "$[property의 Key전체}"
private int age;

@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}

public Person() {
super();
// TODO Auto-generated constructor stub
}

public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}

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;
}


}

 

 

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">

<context:annotation-config/>
<context:property-placeholder location="classpath:com/config/test.properties"/>

<bean class="cohttp://m.config.Person" id="person"/>

</beans>

 

 

 

DTO에서 Value를 넣는 경우
xml에서 외부 파일을 읽는 경우
DTO에서 외부 파일을 읽는 경우

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

Spring_context-scan  (0) 2024.02.08
Spring_SpEL  (0) 2024.02.08
Spring_@Resource  (0) 2024.02.08
Spring_Multi Config  (0) 2024.02.08
Spring_Autowire  (0) 2024.02.08