2011-07-28 3 views
5

클래스 경로의 파일과 함께로드 된 Properties 클래스를 EJB (3.1)에 삽입하는 몇 가지 간단한 방법이 있습니까? 이 같은EJB3.1 속성 파일 삽입

뭔가 : bkail 말했듯이

@Resource(name="filename.properties", loader=some.properties.loader) 
private Properties someProperties; 

감사합니다,

광대는

+1

아닌 POM이를 추가하는 것만 큼 간단) 예 표준 JavaEE. 나는 CDI가 @ Inject + @ Produces로 이런 일을 할 수 있다고 의심하지만 CDI에 익숙하지는 않습니다. (다른 사람이 세부 사항을 기입 할 수 있기를 희망하여이 의견을 남깁니다.) –

답변

3

, 다음과 같은 방법으로이 작업을 수행 할 수 있습니다. 그래서 당신이 그 다음을 만들 loader.getClass().getResourceAsStream ("filename.properties");

먼저 당신의 주입 형에게

@BindingType 
@Retention(RetentionPolicy.RUNTIME) 
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, 
     ElementType.PARAMETER }) 
public @interface PropertiesResource { 

    @Nonbinding 
    public String name(); 

    @Nonbinding 
    public String loader(); 

} 

을 정의하여로드 할 그와 아무것도 생략하지만, 경우에 그것을 위해 옵션을 제공, 당신의 loader=some.properties.loader 정말 무엇을 의미하는지 확실하지 않다 제작자

public class PropertiesResourceLoader { 

    @Produces 
    @PropertiesResource(name = "", loader = "") 
    Properties loadProperties(InjectionPoint ip) { 
     System.out.println("-- called PropertiesResource loader"); 
     PropertiesResource annotation = ip.getAnnotated().getAnnotation(
       PropertiesResource.class); 
     String fileName = annotation.name(); 
     String loader = annotation.loader(); 
     Properties props = null; 
     // Load the properties from file 
     URL url = null; 
     url = Thread.currentThread().getContextClassLoader() 
       .getResource(fileName); 
     if (url != null) { 
      props = new Properties(); 
      try { 
       props.load(url.openStream()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     return props; 
    } 
} 

그런 다음이를 명명 된 구성 요소에 삽입하십시오.

@Inject 
@PropertiesResource(name = "filename.properties", loader = "") 
private Properties props; 

나는 이것이 @HttpParam이 예 here로 주어진 용접 문서에보고했다. 이것은 용접 1.0.0에서, 용접 1.1.0에 따라되면, 점점 주석이 사용중인 응용 프로그램 서버가 CDI 구현 등 WELD (글래스 피시 3.x 또는 보스가있는 경우이

PropertiesResource annotation = ip.getAnnotation(PropertiesResource.class); 
0

같이 수행 할 수 있습니다 7.x의 또는 웹 로직 (12)는 다음 용접 용접 문서 here

에 설명되어 확장를 사용하려면 그것은 쉽게

<dependency> 
    <groupId>org.jboss.weld</groupId> 
    <artifactId>weld-extensions</artifactId> 
    <version>${weld.extensions.version}</version> 
    <type>pom</type> 
    <scope>import</scope> 
</dependency>