springtapestrytynamo

How to inject bean in a Tapestry service


I want to inject bean in a Tapestry service (not in a page).

For the moment, I use this :

public class EntityRealm extends AuthorizingRealm {

ApplicationContext ctx = new ClassPathXmlApplicationContext("/application-context-security.xml");
SecurityServices securityServices = (SecurityServices)ctx.getBean("securityServices");

It works, but I want use this :

public class EntityRealm extends AuthorizingRealm {

@Inject
private SecurityServices securityServices;

And my applicationContext is in the web.xml. In this second case, the injection doesn't work. Why ?

AppModule.java :

public class AppModule
{

//@Resource(name = "realm")
@Inject
private static EntityRealm realm;

@Contribute(WebSecurityManager.class)
public static void addRealms(Configuration<EntityRealm> configuration) {

    //EntityRealm realm = new EntityRealm();

    configuration.add(realm);
}

public static void contributeFactoryDefaults( MappedConfiguration<String, Object> configuration)
{
    configuration.override(SecuritySymbols.LOGIN_URL, "/login");
    configuration.override(SecuritySymbols.UNAUTHORIZED_URL, "/login");
    configuration.override(SecuritySymbols.SUCCESS_URL, "/index");
    configuration.override(SymbolConstants.APPLICATION_VERSION, "2.0-SNAPSHOT");
}

public static void contributeApplicationDefaults(MappedConfiguration<String, Object> configuration)
{
    configuration.add(SymbolConstants.HMAC_PASSPHRASE, new BigInteger(130, new SecureRandom()).toString(32));
    configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en,fr");
    configuration.add( "tapestry.default-cookie-max-age", "31536000" ); 
}

public RequestFilter buildTimingFilter(final Logger log)
{
    return new RequestFilter()
    {
        public boolean service(Request request, Response response, RequestHandler handler)
                throws IOException
        {
            long startTime = System.currentTimeMillis();
            try
            {
                return handler.service(request, response);
            } finally
            {
                long elapsed = System.currentTimeMillis() - startTime;

                log.info(String.format("Request time: %d ms", elapsed));
            }
        }
    };
}

public void contributeRequestHandler(OrderedConfiguration<RequestFilter> configuration,
                                     @Local
                                     RequestFilter filter)
{
    configuration.add("Timing", filter);
}
}

And the EntityRealm.java :

public class EntityRealm extends AuthorizingRealm {

//***************************************
//************* Attributes  *************
//***************************************   
//ApplicationContext ctx = new ClassPathXmlApplicationContext("/application-context-security.xml");

//SecurityServices securityServices = (SecurityServices)ctx.getBean("securityServices");

//@Resource(name = "securityServices")
@Inject
private SecurityServices securityServices;

//***************************************
//************ Constructors *************
//***************************************

public EntityRealm() {
    super(new MemoryConstrainedCacheManager());
    setName("myapprealm");
    setAuthenticationTokenClass(UsernamePasswordToken.class);

} 

//***************************************
//********** Public Methods *************
//***************************************
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    if (principals == null) throw new AuthorizationException("PrincipalCollection was null, which should not happen");

application-context.xml :

<bean id="realm" class="net.atos.m2m.telecom.ihm.services.EntityRealm">
    <property name="securityServices" ref="securityServices"></property>
</bean> 

<bean id="securityServices" class="net.atos.m2m.telecom.ihm.applicatif.services.security.impl.SecurityServicesImpl">
    <property name="servicesTelSecu" ref="servicesTelSecu"></property>
    <property name="converterSecDSPtoDTO" ref="converterSecDSPtoDTO"></property>
    <property name="converterSecDTOtoDSP" ref="converterSecDTOtoDSP"></property>
</bean>

Can you help me ?

Thank you.


Solution

  • How i say in previous comment, if you create EntityRealm in this way .. new EntityRealm() the inject\autowire does not work.

    You must define EntityRealm as bean .. XML or Annotation.

    <bean id="entityRealm" class="package.EntityRealm"/>
    <bean id="securityServices" class="package.SecurityServices"/>