public
Last active

  • Download Gist
App.java
Java
1 2 3 4 5 6 7 8 9 10 11
package test.weldtest;
 
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
 
public class App {
public static void main(String[] args) {
WeldContainer weld = new Weld().initialize();
weld.instance().select(HelloWorldWriter.class).get().sayIt();
}
}
Configuration.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13
package test.weldtest;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
 
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface Configuration {
}
ConfigurationFactory.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
package test.weldtest;
 
import java.io.IOException;
import java.util.Properties;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
 
public class ConfigurationFactory {
public @Produces @Configuration String getConfiguration(InjectionPoint ip) throws IOException {
Properties props = new Properties();
props.load(ConfigurationFactory.class.getResourceAsStream("/messages.properties"));
return props.getProperty(ip.getMember().getDeclaringClass().getSimpleName() + "." + ip.getMember().getName());
}
}
HelloWorld.java
Java
1 2 3 4 5 6 7 8 9 10 11 12
package test.weldtest;
 
import javax.inject.Inject;
 
public class HelloWorldWriter {
@Inject @Configuration
private String message;
public void sayIt() {
System.out.println(message);
}
}
beans.xml
XML
1 2 3
<?xml version="1.0" encoding="UTF-8"?>
<beans>
</beans>
messages.properties
INI
1
HelloWorldWriter.message=Hello World!
pom.xml
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
 
<groupId>test</groupId>
<artifactId>weld-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
 
<name>weld-test</name>
<url>http://maven.apache.org</url>
 
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
 
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
<version>1.1.10.Final</version>
</dependency>
</dependencies>
</project>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.