簡體   English   中英

如何將Injected / Autowired對象從Spring傳遞到ManagedBean?

[英]How to pass Injected/Autowired object from Spring to ManagedBean?

我正在使用Spring和EJB / Primefaces進行項目,我想將值從spring上下文傳遞到托管bean。 我將通過示例代碼進行演示以進一步闡明。

假設我們有以下域類(為了簡化可讀性,我將其簡化了):

public class Store {
    @JsonProperty("store_name")
    private String storeName;

    //constructors, getters and setters...
}

@JsonProperty的原因是因為我是從另一個將Json張貼到以下Controller的應用程序中獲取此值的:

@Controller
@RequestMapping("/store")
public class StoreController {
    @Autowired
    private Store store;

    @RequestMapping(method = RequestMethod.POST)
    public String getStoreResponse(@RequestBody String store) throws JsonParseException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        LOGGER.info("Store Before Post: " + store.getName());
        store = mapper.readValue(request, Store.class);
        LOGGER.info("Store After Post: " + store.getName());
        return "store";
    }

}

我已經在BeanConfig類中配置了存儲bean:

@Configuration
public class BeanConfig {

    @Bean(name = "store")
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Store store() {
        Store store = new Store();
        store.setName("Test Store Name");
        return store;
    }
}

這是我的托管bean:

@ManagedBean
@SessionScoped
public class StoreView extends SpringBeanAutowiringSupport {

    private static final Logger LOGGER = LoggerFactory.getLogger(Store.class);

    //@ManagedProperty("#{store}")
    @Autowired
    private Store store;

    public void test() {
        LOGGER.info("TEST " + store.getName());
    }

    //getters and setters
}

最后是我的xhtml:

<h:panelGrid columns="3">
    <p:outputLabel for="j_store" value="#{messages['storeview.name']}" />
    <p:inputText id="j_store" value="#{storeView.store.name}" />
    <p:message for="j_store" />
    <h:panelGroup />
    <p:commandButton value="#{messages['storeview.test']}" action="#{storeView.test}" update="@form" ajax="false" />                                                                        
</h:panelGrid>

當我第一次使用郵遞員發布樣本數據時,記錄器將輸出:

10:35:57,433 INFO  [com.store.test.controllers.StoreController] (default task-2) Store Before Post: Test Store Name
10:35:57,488 INFO  [com.store.test.controllers.StoreController] (default task-2) Store After Post: posted store name

如果我繼續調用控制器,我將繼續獲得“已發布的商店名稱”,因此它保留了該值。

但是,當我轉到store.xhtml並單擊“測試”按鈕提交表單時,它仍具有在Bean配置文件中設置的值(“測試存儲名稱”),從那時起,它保留了我提交的值在inputText中。

我懷疑這與Spring和Faces上下文有關,我不知道我想做的事是否可行。 如果是這樣,請指出我應該進行哪些更改才能使其正常運行,否則,請向我提供替代解決方案。

提前致謝。

您正在混合@Autowired@ManagedBean批注。 @Autowired由Spring管理,而@ManagedBean由JSF管理。 這意味着您可能有兩個Store實例,一個由控制器修改的實例與托管Bean使用的實例不同。

您應該將托管屬性中的store屬性注釋為@ManagedProperty("#{store}") ,並定義getter和setter。 為了使其正常工作,您還必須在faces-config.xml中定義spring表達式語言解析器

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

由於jsf會話不同於mvc會話,因此您還必須在Store對象的定義中使用單例作用域。

@Scope(value = "singleton"........

暫無
暫無

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

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