File tree Expand file tree Collapse file tree 8 files changed +146
-0
lines changed
cdi/dynamic-bean-decorated
java/org/javaee8/cdi/dynamic/bean/decorated
resources/META-INF/services
java/org/javaee8/cdi/dynamic/bean/decorated Expand file tree Collapse file tree 8 files changed +146
-0
lines changed Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+ <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 >
3
+
4
+ <parent >
5
+ <groupId >org.javaee8</groupId >
6
+ <artifactId >cdi</artifactId >
7
+ <version >1.0-SNAPSHOT</version >
8
+ </parent >
9
+
10
+ <artifactId >dynamic-bean-decorated</artifactId >
11
+ <name >Java EE 8 Samples: CDI - Dynamic Bean Decorated</name >
12
+ </project >
Original file line number Diff line number Diff line change
1
+ package org .javaee8 .cdi .dynamic .bean .decorated ;
2
+
3
+ import javax .enterprise .context .ApplicationScoped ;
4
+ import javax .enterprise .event .Observes ;
5
+ import javax .enterprise .inject .spi .AfterBeanDiscovery ;
6
+ import javax .enterprise .inject .spi .Extension ;
7
+
8
+ /**
9
+ *
10
+ * @author Arjan Tijms
11
+ *
12
+ */
13
+ public class CdiExtension implements Extension {
14
+
15
+ public void afterBean (final @ Observes AfterBeanDiscovery afterBeanDiscovery ) {
16
+ afterBeanDiscovery
17
+ .addBean ()
18
+ .scope (ApplicationScoped .class )
19
+ .types (MyBean .class )
20
+ .id ("Created by " + CdiExtension .class )
21
+ .createWith (e -> new MyBeanImpl ("Hi!" ));
22
+ }
23
+
24
+ }
Original file line number Diff line number Diff line change
1
+ package org .javaee8 .cdi .dynamic .bean .decorated ;
2
+
3
+ /**
4
+ *
5
+ * @author Arjan Tijms
6
+ *
7
+ */
8
+ public interface MyBean {
9
+ String sayHi ();
10
+ }
Original file line number Diff line number Diff line change
1
+ package org .javaee8 .cdi .dynamic .bean .decorated ;
2
+
3
+ import javax .enterprise .inject .Typed ;
4
+
5
+ /**
6
+ *
7
+ * @author Arjan Tijms
8
+ *
9
+ */
10
+ // Typed: Extra guard so that MyBeanImpl has no types of itself, but extension archive is not scanned
11
+ // so not strictly needed.
12
+ @ Typed
13
+ public class MyBeanImpl implements MyBean {
14
+
15
+ private final String greet ;
16
+
17
+ // Note: There's no default ctor, so CDI cannot directly inject an instance of this
18
+ // bean.
19
+
20
+ public MyBeanImpl (String greet ) {
21
+ this .greet = greet ;
22
+ }
23
+
24
+ @ Override
25
+ public String sayHi () {
26
+ return greet ;
27
+ }
28
+
29
+ }
Original file line number Diff line number Diff line change
1
+ org.javaee8.cdi.dynamic.bean.decorated.CdiExtension
Original file line number Diff line number Diff line change
1
+ package org .javaee8 .cdi .dynamic .bean .decorated ;
2
+
3
+ import static org .jboss .shrinkwrap .api .ShrinkWrap .create ;
4
+ import static org .junit .Assert .assertEquals ;
5
+
6
+ import javax .inject .Inject ;
7
+
8
+ import org .jboss .arquillian .container .test .api .Deployment ;
9
+ import org .jboss .arquillian .junit .Arquillian ;
10
+ import org .jboss .shrinkwrap .api .spec .JavaArchive ;
11
+ import org .jboss .shrinkwrap .api .spec .WebArchive ;
12
+ import org .junit .Test ;
13
+ import org .junit .runner .RunWith ;
14
+
15
+ /**
16
+ *
17
+ * @author Arjan Tijms
18
+ *
19
+ */
20
+ @ RunWith (Arquillian .class )
21
+ public class DynamicBeanTest {
22
+
23
+ @ Deployment
24
+ public static WebArchive deploy () {
25
+ return create (WebArchive .class )
26
+ .addAsLibraries (
27
+ create (JavaArchive .class )
28
+ .addClasses (CdiExtension .class , MyBean .class , MyBeanImpl .class )
29
+ .addAsResource ("META-INF/services/javax.enterprise.inject.spi.Extension" ))
30
+ .addClass (MyDecorator .class )
31
+ .addAsManifestResource ("beans.xml" );
32
+ }
33
+
34
+ @ Inject
35
+ private MyBean myBean ;
36
+
37
+ @ Test
38
+ public void test () {
39
+ assertEquals ("Hi! decorated" , myBean .sayHi ());
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ package org .javaee8 .cdi .dynamic .bean .decorated ;
2
+
3
+ import javax .annotation .Priority ;
4
+ import javax .decorator .Decorator ;
5
+ import javax .decorator .Delegate ;
6
+ import javax .inject .Inject ;
7
+
8
+ @ Decorator
9
+ @ Priority (100 )
10
+ public class MyDecorator implements MyBean {
11
+
12
+ @ Inject
13
+ @ Delegate
14
+ MyBean mybean ;
15
+
16
+ @ Override
17
+ public String sayHi () {
18
+ return mybean .sayHi () + " decorated" ;
19
+ }
20
+
21
+ }
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2
+
3
+ <beans xmlns =" http://xmlns.jcp.org/xml/ns/javaee"
4
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
5
+ xsi : schemaLocation =" http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
6
+ bean-discovery-mode =" all" >
7
+
8
+ </beans >
You can’t perform that action at this time.
0 commit comments