如何在Tapestry服务中注入bean

时间:2014-08-12 09:27:56

标签: spring tapestry tynamo

我想在Tapestry服务中注入bean(不在页面中)。

目前,我用这个:

public class EntityRealm extends AuthorizingRealm {

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

它有效,但我想用这个:

public class EntityRealm extends AuthorizingRealm {

@Inject
private SecurityServices securityServices;

我的applicationContext在web.xml中。 在第二种情况下,注射不起作用。为什么?

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);
}
}

和EntityRealm.java:

公共类EntityRealm扩展了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>

你能帮助我吗?

谢谢。

2 个答案:

答案 0 :(得分:3)

我在之前的评论中如何说,如果你以这种方式创建EntityRealm .. new EntityRealm()inject \ autowire不起作用。

您必须将EntityRealm定义为bean .. XML或Annotation。

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

答案 1 :(得分:1)

您可以改用@Resource

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

确保Spring加载 application-context-security.xml 文件。