簡體   English   中英

為 spring bean 分配注釋

[英]Assign spring beans with annotations

我是注釋的新手,我正在嘗試 jsf(2.0) spring (3.1) 集成,我可以集成這兩個框架,但我在 JSF 中沒有 viewScope,因為它不可用。 我想使用注解在 jsf managedBean 中自動注入 spring bean,但我不能,因為 Spring 只支持會話和請求范圍 bean。

我使用檢索 bean 的 Util 類。 “SprigUtil.getBean('bean')”。 並在需要時手動檢索 spring bean。

我想做一些這樣的

@CustomAnnotation('beanTest')
private Bean bean;

因此,bean 屬性將使用 beanTest bean 進行設置。

我的目標(把春天放在一邊)是知道如何做一些這樣的

@assign('House')
private String place;

當我調用getMethod獲得“House”。 instance.getPlace(), 返回 'House'

注意:我知道@Autowired 但我不能使用它,因為 ViewScope 在 spring-jsf 集成中不可用。

我閱讀了有關手動實現視圖范圍的信息,但想嘗試不同的解決方案。

編輯:

我的 FacesConfig:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config 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/web-facesconfig_2_1.xsd"
    version="2.1">
    <application>
        <el-resolver>             org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>
</faces-config>

還有我的 appContext

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">
 
    <context:component-scan base-package="*" />
 
</beans>

我的春豆

@Component
public class ProductService{

}

我的托管 Bean

@Component
@Scope("request")//I need to use @Scope("view") but it doesn't exist
public ProductBean implements Serializable{
@Autowired
ProductService productoService;

}

如果我使用 jsf 注釋 @ManagedBean 和 @ViewScoped productService 沒有注入(為空)

您可以使用@ManagedProperty將 spring bean 注入視圖范圍的托管 bean

對於名為 B 的彈簧組件

@ManagedProperty("#{B}")
private B b;

public void setB(B b) {
this.b = b;
}

應該管用。

至於您發布的代碼,Component 是 Spring 注釋,要使用 ViewScoped,您必須使用 ManagedBean 注釋對您的類進行注釋:

@ManagedBean
@ViewScoped
public ProductBean implements Serializable {
@ManagedProperty("#{productService}")
private ProductService productService;

public void setProductService(ProductService productService) {
    this.productService = productService;
  }
 }

您可能想查看以下鏈接以更好地了解 JSF 2.0 中 JSF 2.0 通信中的范圍

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM