文章出處

RESTFul Service中如果要注入EJB實例,常規的@Inject將不起作用,在Jboss中,應用甚至都啟動不起來(因為@Inject注入失敗),解決方法很簡單:將@Inject換成@EJB

參考代碼:

CityInvoker是一個Stateless的EJB

package test;

import javax.ejb.Stateless;
import ...

@Stateless
public class CityInvoker {

 public CityResponse getCity() {
 CityResponse cityResponse = null;
 CityService cityService = ApplicationContextUtils.getCityService();
 try {
 cityResponse = cityService.findCityByCode(RequestBuilder
 .buildCityFindRequest());
 } catch (LMSException e) {
 e.printStackTrace();
 }
 return cityResponse;
 }
...

 

下面是在RESTFul Service中注入的示例:

package test.rest;

import javax.ejb.EJB;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;


@Path("/")
public class TestService {

 final String XMLNS_NAMESPACE = "http://yjmyzz.cnblogs.com/rest/service";
 final String ROOT_NODE = "root";

 @EJB
 CityInvoker cityInvoker;

 @GET
 @Path("/findCity")
 @Produces(MediaType.APPLICATION_XML)
 public JAXBElement<CityDto> findCity() {
 JAXBElement<CityDto> result = new JAXBElement<CityDto>(new QName(
 XMLNS_NAMESPACE, ROOT_NODE), CityDto.class, cityInvoker
 .getCity().getCities().get(0));
 return result;
 }
...

 


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


文章標籤
全站熱搜
創作者介紹
創作者 大師兄 的頭像
大師兄

IT工程師數位筆記本

大師兄 發表在 痞客邦 留言(0) 人氣(1)