2012-09-26 49 views
3

我在我的項目中使用Spring 3。然後,我從JAR文件注入spring bean時遇到問題。在JAR文件中,有類似的;我可以從JAR文件注入spring bean嗎?

package test; 

@Service("CommonService") 
public class CommonService { 
} 

而且我已經這樣使用它;

package com.java.test.app; 

@Service(value = "OtherService") 
public class OtherService { 
    @Resource(name = "CommonService") 
    private CommonService service; 
} 

在我的spring-beans.xml中;

<context:component-scan base-package="com.java.test.app, test"> 
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> 
</context:component-scan> 

@Resource批註不work.Can我從JAR文件中注入春天豆?

+0

請說明你在jar文件類中定義了@Resource註解的地方嗎? –

+0

@Japs可以在我的jar文件類中聲明'@Resource'註釋。目前不是。 –

+0

當然可以。你需要定義它,否則你不會自動裝配它。 –

回答

0

如果在運行時CommonService類位於類路徑中並位於您使用component-scan指定的基本包內,那麼您應該很好。嘗試使用@Autowired而不是@Resource。