2012-11-08 69 views
2

我想了解是否可以使用,以及如何使用@Autowired構造函數和參數來做Bean。Spring Bean和帶自變量的@Autowired構造函數

@Component 
public class Routes 
{ 
    private Foo req; 

    @Autowired 
    public Routes(Foo req) 
    { 
     this.req = req; 
    } 
    public String getUrl(String destin) 
    { 
     return req.getContextPath() + destin; 
    } 
} 

@Component 
public class HomeController 
{ 
    @Autowired 
    private Routes routes; 

    public HomeController(Foo req) 
    { 
     String foo = routes.getUrl("something"); 
    } 
} 

REAL CODE ------ EDITING --------------- 的異常就行發生: 字符串富= rt.getUrl(「CACA 「);

public class AppRun extends HttpServlet { 

    private static final long serialVersionUID = -3308874705513248491L; 

    private ApplicationContext context; 

    @Override 
    public void service(HttpServletRequest req, HttpServletResponse resp) 
      throws ServletException, IOException 
    { 
     context = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 

     FooTest ft = new FooTest(); 

     HomeControllerTest hc = (HomeControllerTest) context.getBean("homeControllerTest", new Object[]{ft}); 
    } 
} 

@Component 
@Scope("prototype") 
public class FooTest { 

    public String mensagem() 
    { 
     System.out.println("funcionou"); 
     return "ok"; 
    } 

} 

@Component 
@Scope("prototype") 
public class RoutesTest { 

    private FooTest req; 

    @Autowired 
    public RoutesTest(FooTest req) 
    { 
     this.req = req; 
    } 

    public String getUrl(String destin) 
    { 
     return req.mensagem().concat(destin); 
    } 

} 

@Component 
@Scope("prototype") 
public class HomeControllerTest { 

    @Autowired 
    private RoutesTest rt; 

    public HomeControllerTest(FooTest req) 
    { 
     String foo = rt.getUrl("caca"); 

     System.out.println(foo); 
    } 

} 
+0

你試過了嗎?是的,它會工作,但構造函數的所有參數必須以@Components(或其他類似的註釋)的形式提供。 – jalynn2

+0

我嘗試了這個,但是我得到這個異常:2012-11-08 14:49:15.908:WARN ::嵌套在org.springframework.beans.factory.BeanCreationException中:創建名爲'homeControllerTest'的Bean在文件[D :\ Eclipse \ workspace \ summer-mvc \ target \ classes \ br \ gov \ pr \ maringa \ test \ HomeControllerTest.class]:Bean實例化失敗;嵌套異常是org.springframework.beans.BeanInstantiationException:無法實例化bean類[br.gov.pr.maringa.test.HomeControllerTest]:構造函數拋出異常;嵌套異常是java.lang.NullPointerException: java.lang.NullPointerException – claudioivp

+0

請發佈HomeControllerTest的構造函數。這門課是嚴格測試嗎?它屬於生產代碼的一部分嗎? – jalynn2

回答

5

的問題是,你是在一個尚未看重的構造訪問屬性:春天必須創建對象纔可以設置@Autowired屬性RT。將參數RoutesTest rt添加到構造函數,或將您的構造函數邏輯移動到afterPropertiesSet方法。

這裏是如何改變你的類,它是失敗的,使用構造函數:

@Component 
@Scope("prototype") 
public class HomeControllerTest { 

    private RoutesTest rt; 

    @Autowired 
    public HomeControllerTest(FooTest req, RoutesTest rt) 
    { 
     this.rt = rt; 
     String foo = rt.getUrl("caca"); 

     System.out.println(foo); 
    } 

} 
+0

Thx請注意。將RoutesTest rt添加到HomeControllerTest()?你能解釋一下嗎? – claudioivp

+0

我編輯了答案,通過更改構造函數向我展示了我的意思。 – jalynn2

+0

嗯好。我獲得成功!謝謝。 – claudioivp

相關問題