【问题标题】:Can I inject spring bean from JAR file?我可以从 JAR 文件中注入 spring bean 吗?
【发布时间】:2012-09-26 06:54:13
【问题描述】:

我在我的项目中使用 Spring 3。然后我从 JAR 文件中注入 spring bean 时遇到问题。在 JAR 文件中,有类似的类;

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注解不起作用。我可以从JAR文件中注入spring bean吗?

【问题讨论】:

  • 请显示您在jar文件类中定义@Resource注解的位置?
  • @Japs 我可以在我的 jar 文件类中声明“@Resource”注释吗?目前不是。
  • 当然可以。您需要定义它,否则您将无法自动装配它。

标签: annotations spring-3


【解决方案1】:

如果在运行时您的 CommonService 类位于类路径上并且位于您使用组件扫描指定的基本包中,那么您应该可以继续使用。尝试使用@Autowired 而不是@Resource。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多