Spring 주석@Value 및 속성 로드 프로필 방식

Spring 에서@Value 주 해 를 사용 하여 bean 에 속성 을 불 러 오 는 프로필 은 두 가지 사용 방식 이 있 습 니 다.
첫 번 째:@Value 사용("\#{configProperties['website.msgmname']}")
spring 에서 속성 으로 파일 을 불 러 오 는 설정 방식 을 설정 합 니 다.

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:/properties/websit.properties</value>
            </list>
        </property>
</bean>
주의 하 다.
1.여기 서 사용 하 는 configProperties 는 정 의 된 bean 이름과 일치 해 야 합 니 다.
2.websit 는 msgname 가 그 프로필 에서 유래 한 것 을 지정 합 니 다.
3.설 정 된 로드 속성 bean 이름 은 org.springframework.beans.factory.config.Properties Factory Bean 입 니 다.
두 번 째:@Value("${website.msgrame}")를 사용 합 니 다.
이런 방식 을 사용 하면 또 두 가지 설정 방식 이 있 을 수 있다.
방식 1

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="properties" ref="configProperties"/>
</bean>
 
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:/properties/websit.properties</value>
            </list>
        </property>
</bean>
방식 2

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="locations">
     <list>
         <value>classpath:properties/websit.properties</value>
     </list>
        </property>
</bean>
@Value 주석 bean 속성 을 사용 할 때 설정 파일 에 설정 되 어 있 지 않 으 면 spring 을 시작 하면 이상 을 던 집 니 다.@Value 는 속성 파일 에 설정 이 없 으 면 기본 값 을 사용 할 수 있 는 기본 값 설정 방식 을 제공 합 니 다.
형식 은 아래 와 같다.

@Value("${avg.age:22}")  
private int userAge; 
@Value 주 해 를 사용 한 후 데이터 가 정상적으로 주입 되 지 않 으 면 xml 설정 파일 에 다음 코드 를 추가 해 야 합 니 다.

<context:annotation-config/>
SpringBoot 는 properties(yml)파일 의 설정 정 보 를 주석(@value)으로 읽 습 니 다.
properties 파일 의 설정 값 을 간소화 하기 위해 spring 은@value 주 해 를 지원 하 는 방식 으로 얻 을 수 있 습 니 다.이러한 방식 은 프로젝트 설정 을 크게 간소화 하고 업무 중의 유연성 을 향상 시 킵 니 다.
1.두 가지 사용법
1)@Value("#{configProperties['key']}")
2)@Value("${key}")
2.설정 파일 예제
ftp:
ftplp: 10.2.23.89
ftpPort: 21
ftpUser: uftp
ftpPwd: 12345678
ftpRemotePath: /home
설명:이상 은 설정 파일 의 정보 이 고 주로 계 정 비밀번호 등 정보 입 니 다.
3.yml 프로필 을 읽 는 도구 클래스

package com.dbright.dataprediction.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:ftpconfig.yml")
@ConfigurationProperties(prefix = "ftp")
public class FtpProperties {

    @Value("${ftplp}")
    public String ftplp;
    @Value("${ftpPort}")
    public String ftpPort;
    @Value("${ftpUser}")
    public String ftpUser;
    @Value("${ftpPwd}")
    public String ftpPwd;
    @Value("${ftpRemotePath}")
    public String ftpRemotePath;

    public String getFtplp() {
        return ftplp;
    }

    public void setFtplp(String ftplp) {
        this.ftplp = ftplp;
    }

    public String getFtpPort() {
        return ftpPort;
    }

    public void setFtpPort(String ftpPort) {
        this.ftpPort = ftpPort;
    }

    public String getFtpUser() {
        return ftpUser;
    }

    public void setFtpUser(String ftpUser) {
        this.ftpUser = ftpUser;
    }

    public String getFtpPwd() {
        return ftpPwd;
    }

    public void setFtpPwd(String ftpPwd) {
        this.ftpPwd = ftpPwd;
    }

    public String getFtpRemotePath() {
        return ftpRemotePath;
    }

    public void setFtpRemotePath(String ftpRemotePath) {
        this.ftpRemotePath = ftpRemotePath;
    }
}
설명:yml 프로필 을 읽 기 위해@value 주 해 를 사용 한 코드 예제 입 니 다.
1)@component일반 pojo 를 spring 용기 에 예화 하 는 것 은 설정 파일 의'bean id='class='/'에 해당 합 니 다.
2)@PropertySource("classpath:ftpconfig.yml") -yml 파일 의 경 로 를 설정 하여 검색 하기 편리 합 니 다.일반적으로 우리 프로필 은 resources 패키지 에 놓 여 있 습 니 다.그래서 우 리 는 classpath+읽 어야 할 프로필 이름 만 필요 합 니 다.
3)@ConfigurationProperties(prefix = "ftp")-이것 은 너무 많이 설명 할 필요 가 없습니다.파일 에 있 는 내용 의 접 두 사 를 설정 합 니 다.우 리 는 ftp 아래 의 정 보 를 읽 습 니 다.
4)@Value("${ftplp}") 이것 은 우리 가 필요 로 하 는 설정 정 보 를 읽 는 것 입 니 다.달러 기호+{필드 이름}을 만 들 수 있 습 니 다.
5)읽 은 설정 정 보 를 받 기 위해 문자열 을 정의 합 니 다.
6)set 와 get 방법 을 써 서 외부 클래스 호출 에 편리 합 니 다.
4.데모:효과 도 는 다음 과 같다.


우리 가 원 하 는 값 을 얻 는 데 성공 한 것 을 볼 수 있다.
5.처음에 말 한 두 번 째 는 이것 과 차이 가 많 지 않다.
{}밖의$를\#번호 로 바 꾸 고 설정 파일 의 정보+필드 를 지정 할 뿐 입 니 다.대동소이 해서 나 는 코드 를 붙 이지 않 겠 다.
오늘 의 내용 은 여기까지 입 니 다.이상 은 개인 적 인 경험 입 니 다.여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다!

좋은 웹페이지 즐겨찾기