【问题标题】:Using Spring @Value systemProperties into Managed Bean在托管 Bean 中使用 Spring @Value systemProperties
【发布时间】:2011-08-29 18:08:53
【问题描述】:

我想将 systemProperties 注入 JSF 托管 Bean (jsf 1.2)。

可以做到这一点的唯一方法是我要么使用托管属性,要么创建一个 systemProperties bean 并将其注入到我想要的托管 bean 中,对吗?我不能像这样使用@Value;为了让我这样做,它必须是春豆。

#{systemProperties['databaseName']}

#{systemProperties.databaseName}

托管财产

    <managed-bean>
    <managed-bean-name>fooUI</managed-bean-name>
    <managed-bean-class>test.foo</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
        <property-name>systemPropertyExample</property-name>
        <value>#{systemProperties['systemPropertyExample']}</value>
    </managed-property>  
</managed-bean>  

【问题讨论】:

    标签: spring jsf


    【解决方案1】:

    JSF 管理的 bean可以 由 spring 管理。你只需要在 faces-config.xml 中配置一个 spring 特定的&lt;el-resolver&gt;

    <el-resolver>
         org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver> 
    

    它将在 spring 上下文中解析托管 bean(当然,您需要 web.xml 中的 ContextLoaderListener 来引导 spring,但我假设您有)。然后你可以在你的 jsf bean 中使用@Value、依赖注入等。

    唯一的区别是您将使用 @Controller @Scope("request") 而不是 xml 来定义它们。

    【讨论】:

    • 这就是我的想法,我确实定义了 EL-RESOLVER,并通过 ContextLoaderListener 加载我的 spring xml。我必须将@Controller 注释与@Value 结合使用吗?现在我只在 faces-config xml 中定义了我的托管 bean。
    • 对大写感到抱歉,对此的编辑内容不允许我进行更改以降低该上限
    • @Controller 不是托管 bean。它是Spring bean。问题是关于 JSF bean。
    【解决方案2】:

    您可以使用@Configurable 注释JSF Bean,然后您可以像使用spring bean 一样使用它。 (注意这需要使用AspectJ)

    【讨论】:

    • 当你说需要使用 AspectJ 时,你的意思是说 Spring AspectJ 的 jar 必须在类路径上还是需要一些配置?
    • 我的意思是 AspectJ 编译时编织——但我猜 Bozho 的回答是我更好的解决方案
    猜你喜欢
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2011-03-25
    • 2012-08-27
    相关资源
    最近更新 更多