Jak bkail powiedział, można to osiągnąć w następujący sposób. Nie jestem pewien, co się loader=some.properties.loader
naprawdę oznaczało, więc odpuściliśmy robi nic z tego, ale pod warunkiem opcję, że w przypadku, gdy chcesz załadować za pomocą loader.getClass().getResourceAsStream ("filename.properties");
pierwsze zdefiniować wtryskiem
@BindingType
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD,
ElementType.PARAMETER })
public @interface PropertiesResource {
@Nonbinding
public String name();
@Nonbinding
public String loader();
}
następnie utworzyć producent dla tego
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;
}
}
Następnie wstrzyknij go do swojego komponentu nazwanego.
@Inject
@PropertiesResource(name = "filename.properties", loader = "")
private Properties props;
Zrobiłem to patrząc w dokumentacji spoiny gdzie @HttpParam jest podany jako przykład here. Jest to jak na spoiny 1.1.0, 1.0.0 w spoinie, coraz adnotacji można zrobić jak ten
PropertiesResource annotation = ip.getAnnotation(PropertiesResource.class);
niełatwo w standardowy JavaEE. Podejrzewam, że CDI może zrobić coś takiego z @ Inject + @ Produces, ale nie jestem wystarczająco zaznajomiony z CDI. (Pozostawiając ten komentarz z nadzieją, że ktoś inny może podać szczegóły.) –