我正在尝试使用Spring构建一个独立的应用程序(不在应用程序服务器中运行),我面临以下问题:
我的独立应用程序(启用弹簧)取决于另一个项目(捆绑为jar),其中包含许多服务com.application.service(带注释@Service).
外部项目中没有与spring相关的配置,独立应用程序上下文非常简单,它只包含:
<context:component-scan base-package="com.application" />
以下是依赖于无法获取的服务的Class示例:
@Service
public class StandaloneService {
@Autowired
private SomeService someService;
// ...
}
Run Code Online (Sandbox Code Playgroud)
StandaloneServiceSomeService在外部jar 中,它包含在独立应用程序中.
错误 :
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.application.SomeService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
以下是我创建ApplicationContext并尝试获取服务的方式:
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
BeanFactory factory = (BeanFactory) context; …Run Code Online (Sandbox Code Playgroud)