问题
I have a Seam component that handles login, with the name "authenticator":
@Name("authenticator")
public class AuthenticatorAction implements Authenticator
{
@PersistenceContext
private EntityManager em;
@In(required=false)
@Out(required=false, scope = SESSION)
private User user;
public boolean authenticate(){ ... }
}
This works just fine, Seam injects the EntityManager instance. However, as soon as I add the @Stateless
annotation, none of the injection happens! In this case, the EntityManager instance is null upon entry to the authenticate()
method. Another interesting note is that with a separate stateful session bean I have, the Logger instance in that class is only injected if I make it static. If i have it non-static, it is not injected. Thats fine for the logger, but for stateless session beans like that, I obviously can't have static member variables for these components.
I'm confused because this authenticator is exactly how it is in the Seam booking example: a stateless session bean with a private member variable being injected.
Any ideas?
回答1:
I am curious:
However, as soon as I add the @Stateless annotation, none of the injection happens!
So i hope your Authenticator interface is marked with @javax.ejb.Local or @javax.ejb.Remote. If not, so your Stateless will not work as expected.
When you have a @Stateless Session bean, you must activate a Seam interceptor in order to enable @In-jection. Something like
pureCharger-jar.jar
META-INF
ejb-jar.xml
persistence.xml
ejb-jar.xml is shown as follows
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar 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/ejb-jar_3_0.xsd" version="3.0">
<interceptors>
<interceptor>
<interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
</interceptor>
</interceptors>
<assembly-descriptor>
<interceptor-binding>
<ejb-name>*</ejb-name>
<interceptor-class>org.jboss.seam.ejb.SeamInterceptor</interceptor-class>
</interceptor-binding>
</assembly-descriptor>
</ejb-jar>
If possible, take a look at Seam Security with Dan Allen, at JavaOne, author of Seam in Action book.
regards,
来源:https://stackoverflow.com/questions/2404299/jboss-seam-components-injected-into-pojos-but-not-session-beans