आप जावा एसई में सीडीआई का उपयोग कर सकते हैं लेकिन आपको अपना कार्यान्वयन प्रदान करना होगा। यहाँ संदर्भ कार्यान्वयन का उपयोग कर एक उदाहरण है - वेल्ड:
package foo;
import org.jboss.weld.environment.se.Weld;
public class Demo {
public static class Foo {
@Guarded public String invoke() {
return "Hello, World!";
}
}
public static void main(String[] args) {
Weld weld = new Weld();
Foo foo = weld.initialize()
.instance()
.select(Foo.class)
.get();
System.out.println(foo.invoke());
weld.shutdown();
}
}
classpath के लिए एक ही है:
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
<version>1.1.10.Final</version>
</dependency>
एनोटेशन:
package foo;
import java.lang.annotation.*;
import javax.interceptor.InterceptorBinding;
@Inherited @InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
public @interface Guarded {}
इंटरसेप्टर कार्यान्वयन:
package foo;
import javax.interceptor.*;
@Guarded @Interceptor
public class Guard {
@AroundInvoke
public Object intercept(InvocationContext invocationContext) throws Exception {
return "intercepted";
}
}
वर्णनकर्ता:
<!-- META-INF/beans.xml -->
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
<interceptors>
<class>foo.Guard</class>
</interceptors>
</beans>