2017-10-15 156 views
0

我想從使用cdi注入的application.properties文件中獲取屬性。當我的字段被使用時,我的製作者方法從未被調用。所以他們總是空的。我錯過了什麼,做錯了什麼?cdi生成方法忽略

這裏是我的製作等級:

propertyProducer

import java.io.IOException; 
import java.io.InputStream; 
import java.util.Properties; 
import javax.annotation.PostConstruct; 
import javax.enterprise.context.ApplicationScoped; 
import javax.enterprise.inject.Produces; 
import javax.enterprise.inject.spi.InjectionPoint; 
import org.apache.log4j.Logger; 

@ApplicationScoped 
public class propertyProducer implements property{ 

    final static Logger logger = Logger.getLogger(propertyProducer.class); 
    private Properties properties; 

    @Property 
    @Produces 
    public String produceString(final InjectionPoint ip) { 
     return this.properties.getProperty(getKey(ip)); 
    } 

    @Property 
    @Produces 
    public int produceInt(final InjectionPoint ip) { 
     return Integer.valueOf(this.properties.getProperty(getKey(ip))); 
    } 

    @Property 
    @Produces 
    public boolean produceBoolean(final InjectionPoint ip) { 
     return Boolean.valueOf(this.properties.getProperty(getKey(ip))); 
    } 

    private String getKey(final InjectionPoint ip) { 

     return (ip.getAnnotated().isAnnotationPresent(Property.class) && 
       !ip.getAnnotated().getAnnotation(Property.class).value().isEmpty()) ? 
       ip.getAnnotated().getAnnotation(Property.class).value():ip.getMember().getName(); 

    } 
    @PostConstruct 

    public void init() {  
     this.properties = new Properties(); 
     final InputStream stream = propertyProducer.class.getResourceAsStream("/application.properties"); 
     if (stream == null) { 
      throw new RuntimeException("No properties!!!"); 
     }  
     try { 
      this.properties.load(stream);  
     } catch (final IOException e) { 
      throw new RuntimeException("Configuration could not be loaded!"); 
     } 
    } 
} 

我的界面

import javax.enterprise.util.Nonbinding; 
import javax.inject.Qualifier; 
import java.lang.annotation.Retention; 
import java.lang.annotation.Target; 
import static java.lang.annotation.ElementType.FIELD; 
import static java.lang.annotation.ElementType.METHOD; 
import static java.lang.annotation.ElementType.PARAMETER; 
import static java.lang.annotation.ElementType.TYPE; 
import static java.lang.annotation.RetentionPolicy.RUNTIME; 

public interface property { 
@Qualifier 
@Retention(RUNTIME) 
@Target({TYPE, METHOD, FIELD, PARAMETER}) 
public @interface Property { 
    @Nonbinding String value() default ""; 
    @Nonbinding boolean required() default true; 
    } 
} 

,這裏是我如何獲得注入的屬性trhough領域

import java.io.ByteArrayInputStream; 
import java.io.InputStream; 
import javax.enterprise.context.RequestScoped; 
import javax.inject.Inject; 
import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.xpath.XPath; 
import javax.xml.xpath.XPathConstants; 
import javax.xml.xpath.XPathExpression; 
import javax.xml.xpath.XPathFactory; 
import org.apache.log4j.Logger; 
import org.w3c.dom.Document; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import com.kino.jax.property.Property; 
import com.kino.jax.urlReader.URLReader; 

@RequestScoped 
public class caller implements callerInterface{ 

    @Inject 
    @Property("appId") 
    private String appId; 

    @Inject 
    @Property("devId") 
    private String devId; 

    @Inject 
    @Property("certId") 
    private String certId; 

    @Inject 
    @Property("ebayWsBaseUrl") 
    private String ebayWsBaseUrl; 

    @Inject 
    @Property("ebayFindAndGetWsExtension") 
    private String ebayFindAndGetWsExtension; 

    @Inject 
    @Property("ebayFindAndGetWsParam") 
    private String ebayFindAndGetWsParam; 


    final static Logger logger = Logger.getLogger(caller.class); 
    //private int maxResults; 
    private final int REQUEST_DELAY=500; 

    @Override 
    public void run(String search) throws Exception { 
     logger.info("inside caller class"); 
     String address = createAddress(search); 
     logger.info("sending request to :: "+ address); 
     String response = URLReader.read(address); 
     logger.info("response :: "+ response); 
     processResponse(response); 
     //Honor rate limits - wait between results 
     Thread.sleep(REQUEST_DELAY); 
    } 
    private String createAddress(String search){ 
    //substitute token 
    logger.info("preparing ws URL "); 
    try{ 
     String address = ebayWsBaseUrl+ebayFindAndGetWsExtension+ebayFindAndGetWsParam; 
     address.replace("[appName]", appId); 
     address.replace("[keyWords]",search); 

     return address; 
    } 
    catch(Exception e){ 
     logger.fatal("could not get service property "+e); 
     return null; 
    } 
} 

和我homeManager從哪裏獲得參考主叫類

//some code 
public void callWS(String search){ 
    callerInterface call = new caller(); 
     try { 
       call.run(search); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
//some code 
+0

爲什麼'propertyProducer'實現'property'?它不需要。爲了提供更多信息,你可以舉一個你如何注入這個領域的例子嗎?並獲得該類的參考? –

+0

@JohnAment坦克你爲你的答案,沒有特別的原因propertyProducer實現屬性。它只是爲了測試它是否有效果。我編輯了我的問題,以便您可以看到我的文件夾是如何使用的。 – kino

+0

在你的限定符接口「屬性」(大寫)周圍有一個接口「屬性」(小寫)是什麼意思?似乎不尋常。我試圖消除這一點,看看它有什麼影響。 – mtj

回答

0

的問題是,你手動實例化類。你需要在你的bean中保留對caller的引用。

如果您使用CDI 1.1並需要靜態查找,則可以使用CDI.current().select(callerInterface.class).get()來獲取參考。否則你會@Inject callerInterface c

+0

感謝;' 的伎倆 – kino