JSF 2 使用 @ManagedProperty 注入 Spring bean/service 而没有 xml

JSF 2 inject Spring bean/service with @ManagedProperty and no xml(JSF 2 使用 @ManagedProperty 注入 Spring bean/service 而没有 xml)
本文介绍了JSF 2 使用 @ManagedProperty 注入 Spring bean/service 而没有 xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想使用 jsf 注释和一些 spring将 spring bean/服务注入 jsf 托管 bean 的注释.(在 jsf bean 上我只想使用 jsf 注释)我不想使用像 @named/@inject 这样的注解.

I would like to use jsf annotations and some spring annotations to inject a spring bean/service into a jsf managed bean. (on the jsf bean i only want to use jsf annotations) I dont want to use annotations like @named / @inject.

我试图在网上找到解决方案,但没有任何运气.

I have tried to find a solution on the net but without any luck.

例子

@ManagedBean
@ViewScoped 
public class MyBean {

    @ManagedProperty(value = "#{mySpringBean}")
    private MySpringBean mySpringBean;

    public void setMySpringBean(MySpringBean mySpringBean) {
        this.mySpringBean = mySpringBean;
    }

    public void doSomething() {
    //do something with mySpringBean
    }
}

在不使用 xml 的情况下,这样的事情是否可行.例如,我不想使用类似的东西

Is something like this possible without the use of xml. For example, I would NOT like to use something like

FacesContextUtils.getWebApplicationContext(context).getBean("MySpringBean");

或在 faces-config.xml

<managed-bean>
    <managed-bean-name>myBean</managed-bean-name>
    <managed-bean-class>com.mytest.MyBean</managed-bean-class>
    <managed-bean-scope>view</managed-bean-scope>
    <managed-property>
        <property-name>mySpringBean</property-name>
        <value>#{mySpringBean}</value>
    </managed-property>
</managed-bean>

上面的内容是否可能有注释和没有注释定义所有 jsf beans/properties 和 spring beans/properties配置 xml 文件中的每个 bean?

Is something like the above possible with annotations and without defining all the jsf beans/properties and the spring beans/properties for every bean in the config xml files?

推荐答案

如果你已经有了 Spring 容器,为什么不使用它的 @Autowired 注解.为此,请按照 Boni 的建议更新您的 faces-config.xml.然后在此之后将这些侦听器添加到您的 web.xml 中

If you already have Spring container why not use its @Autowired annotation. For that, Update your faces-config.xml as suggested by Boni. Then add these listeners to your web.xml after this

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
  <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

然后将这些添加到您的 applicationContext.xml

Then add these to your applicationContext.xml

<context:component-scan base-package="com.examples" />

现在您可以使用 Spring 注释,您的 bean 将是这样的:

Now you can use Spring annotations and your bean will be something like this:

package com.examples;
@Component
@Scope(value="request")
public class MyBean {
    @Autowired
    private MySpringBeanClass mySpringBean;
}

使用 @Service 注释您的 MySpringBeanClass

Annotate your MySpringBeanClass with @Service

另见:

  • @Scope("request") 不起作用

这篇关于JSF 2 使用 @ManagedProperty 注入 Spring bean/service 而没有 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)