-
1. Re: Interceptors blocking Injection?
dan.j.allen Nov 24, 2010 1:52 PM (in response to jasondlee)Jason,
This appears to be an outstanding issue in the Weld GlassFish integration. I tried the test case below on GlassFish 3.0.1 (Weld 1.0.1-SP3) and GlassFish 3.1 (Weld 1.1.0.Beta2) and got the same results: no resource injection. I removed the @Transactional binding as you did and observed the resource injection work.
In both cases, injection using @Inject works, so this is strictly a problem with resource injections (e.g., @Resource, @PersistenceUnit, etc).
The injection does work on JBoss AS 6, so my gut tells me it's a problem at the integration layer.
Here's the Arquillian test I used, along with the supporting classes and resources:
TransactionInterceptorTest.java
@RunWith(Arquillian.class) public class TransactionInterceptorTest { @Deployment public static Archive<?> createDeployment() { return ShrinkWrap.create(WebArchive.class, "test.war") .addClasses(ShoppingCart.class, TransactionInterceptor.class, Transactional.class) .addManifestResource("test-persistence.xml", "persistence.xml") .addWebResource("transaction-interceptor-beans.xml", "beans.xml"); } @Inject ShoppingCart cart; @Test public void interceptor_should_be_applied() { TransactionInterceptor.interceptorInvoked = false; cart.checkOut(); assertTrue(TransactionInterceptor.interceptorInvoked); } @Test public void resource_injection_should_occur() { assertTrue(cart.isResourceInjectionWorking()); } }
Transactional.java
@Inherited @InterceptorBinding @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.METHOD, ElementType.TYPE }) public @interface Transactional {}
TransactionInterceptor.java
@Transactional @Interceptor public class TransactionInterceptor { public static boolean interceptorInvoked; @Inject private UserTransaction tx; @AroundInvoke public Object manageTransaction(InvocationContext context) throws Exception { interceptorInvoked = true; tx.begin(); System.out.println("Starting transaction"); Object result = context.proceed(); tx.commit(); System.out.println("Committing transaction"); return result; } }
ShoppingCart.java
@Named @RequestScoped @Transactional public class ShoppingCart { @PersistenceUnit(unitName = "em") private EntityManagerFactory emf; @PersistenceContext private EntityManager em; @Inject private UserTransaction utx; public void checkOut() { System.out.println("Checking out"); } public boolean isResourceInjectionWorking() { System.out.println("emf = " + emf + "; em = " + em + "; utx = " + utx); return emf != null; } }
transaction-interceptor-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>com.acme.resting.test.TransactionInterceptor</class> </interceptors> </beans>
test-persistence.xml
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="em"> <jta-data-source>jdbc/__default</jta-data-source> </persistence-unit> </persistence>
-
2. Re: Interceptors blocking Injection?
dan.j.allen Nov 24, 2010 1:52 PM (in response to jasondlee)Can you file an issue in GlassFish?