【问题标题】:Issue with bean from another JAR来自另一个 JAR 的 bean 问题
【发布时间】:2018-04-27 09:14:08
【问题描述】:

我正在导出一个包含类的 JAR:

public class SerializerHelper {
    public String toJson(final Object src) {...}
}

还有trialApplicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="serializerHelper" class="com.trial.SerializerHelper"/>

</beans>

然后,我将该 JAR 导入另一个项目,并在其 applicationContext.xml 中导入试用上下文,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- Replace all @Autowired by its instances -->
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

    <import resource="classpath*:trialApplicationContext"/>

    <bean id="booksSerializer" class="com.trial.BooksSerializerImpl" />
    ...

</beans>

然后,我在课堂上使用该 bean:

public interface BooksSerializer {
    String getBooks();
}

public class BooksSerializerImpl implements BooksSerializer {

    @Autowired
    private SerializerHelper serializerHelper;

    ...

}

但是,代码失败并出现以下错误:

java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: 在 URL [...applicationContext. xml]:合并 bean 定义的后处理失败;嵌套异常是 java.lang.IllegalStateException:无法自省 bean 类 [com.trial.BooksSerializerImpl] 的自动装配元数据:找不到它所依赖的类 java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: 在 URL [jar:file:/... .jar!/applicationContext.xml]:合并 bean 定义的后处理失败;嵌套异常是 java.lang.IllegalStateException:无法自省 bean 类 [com.trial.BooksSerializerImpl] 的自动装配元数据:找不到它所依赖的类 在 org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:921) 在 org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:900) 在 org.apache.catalina.core.StandardHost.addChild(StandardHost.java:684) 在 com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2044) 在 com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1690) 在 com.sun.enterprise.web.WebApplication.start(WebApplication.java:107) 在 org.glassfish.internal.data.EngineRef.start(EngineRef.java:122) java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: 在 URL [jar: file:/....jar!/applicationContext]: 合并 bean 定义的后处理失败;嵌套异常是 java.lang.IllegalStateException:无法自省 bean 类 [com.trial.BooksSerializerImpl] 的自动装配元数据:找不到它所依赖的类 在 com.sun.enterprise.web.WebApplication.start(WebApplication.java:136) 在 org.glassfish.internal.data.EngineRef.start(EngineRef.java:122) java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: 在 URL [jar: file:/....jar!/applicationContext.xml]: 合并 bean 定义的后处理失败;嵌套异常是 java.lang.IllegalStateException:无法自省 bean 类 [com.trial.BooksSerializerImpl] 的自动装配元数据:找不到它所依赖的类 在 com.sun.enterprise.web.WebApplication.start(WebApplication.java:136) 在 org.glassfish.internal.data.EngineRef.start(EngineRef.java:122) 加载应用程序时出现异常:java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: 在 URL [jar: file:/....jar!/applicationContext.xml]: 合并 bean 定义的后处理失败;嵌套异常是 java.lang.IllegalStateException:无法自省 bean 类 [com.trial.BooksSerializerImpl] 的自动装配元数据:找不到它所依赖的类]]

当我尝试在球衣服务器上运行我的项目时发生此错误。

我做错了什么?非常感谢。

【问题讨论】:

  • 你的BooksSerializer接口在哪里?你在那个界面上加注解了吗?
  • 不,接口没有注解。
  • @Service注释你的BooksSerializer接口,让我知道状态。
  • 我试过了,但是很遗憾,没有任何改变
  • 你用过&lt;mvc:component-scan ="package"&gt;吗?

标签: spring spring-mvc intellij-idea jar


【解决方案1】:

请使用上下文组件扫描来扫描你的包,以便你声明的 spring bean 可以被 spring 容器发现。 声明在applicationContext.xml

<context:component-scan base-package="com.jartrial.*, com.trial.*" />

【讨论】:

    猜你喜欢
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    相关资源
    最近更新 更多