2014-11-03 5 views
0

현재 스프링 프로젝트에서 일부 POJO 클래스의 속성 값을 할당하는 방법을 찾고 있습니다.annotation을 기반으로 변수에 값을 할당하십시오.

이 기능은 주석 PathVariable와 컨트롤러의 방법에서 일부 매개 변수에 대한 ModelAttribute의 사용과 유사합니다 매개 변수가이 주석을 하나의 주석을 붙일 수 있고, 자신의 시스템이 매개 변수의 값을 읽고에 할당 변수를 사용할 필요없이 <variable> = <value>. 내 프로젝트에서

,이 같은 일부 POJO 클래스가 : 나는이 클래스의 새로운 인스턴스를 생성, 시스템 :

class Class { 

    private <type> <attribute> 

    public <type> get<attribute>() { 
     return <attribute>; 
    } 

    public void set<attribute>(<type> <parameter>) { 
     <attribute> = <parameter>; 
    } 

} 

을 (@Setting 예를 들어) 속성에 주석을 추가하는 경우 파일의 값을 읽고 변수에 할당해야합니다.

누구든지이 작업을 수행 할 수 있습니다.

+1

http://stackoverflow.com/questions/20782673/creating-custom-annotation-in-spring -mvc-and-getting-httpservletrequest-object –

답변

1

스프링 @PropertySource & @Value 주석을 사용할 수 있습니다. 나는 이런 식으로 뭔가 의미 :

@Value("${sourceLocation:c:/temp/input}") 
    private String source; 

    @Value("${destinationLocation:c:/temp/output}") 
    private String destination; 

    @Autowired 
    private Environment environment; 

    public void readValues() { 
     System.out.println("Getting property via Spring Environment :" 
       + environment.getProperty("jdbc.driverClassName")); 

     System.out.println("Source Location : " + source); 
     System.out.println("Destination Location : " + destination); 

이를 좋은 최신이 도움이 될 수 blogpost here

+0

'@ Value' 매개 변수 대신'$ {...} '대신 다른 응용 프로그램 클래스의 기존 메서드의 반환 값을 사용할 수 있습니까? –

+0

값의 javadoc에 따른 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Value.html 일반적인 사용 사례는 다음을 사용하여 기본 필드 값을 할당하는 것입니다. "# {systemProperties.myProp}"스타일 식. 그래서 나는 당신이 @PostConstruct '주석이 달린 메소드를 초기화하고 다른 클래스 객체를 autowired로 만들어야한다고 생각한다. @PostConstructor 주석을 확인할 수 있습니다. – erhun

관련 문제