【问题标题】:Zero beans when importing spring context导入spring上下文时的零bean
【发布时间】:2015-04-24 15:26:44
【问题描述】:

我尝试像这样(在 spring-config-client.xml 中)从 WEB-INF/lib/ 中的 jar 文件加载 spring 上下文:

<import resource="classpath*:spring-configuration.xml" />

但是当我尝试使用它时:

ApplicationContext factory = new ClassPathXmlApplicationContext("classpath:spring-config-client.xml");
int k = factory.getBeanDefinitionCount();

k 等于 0。如何使用 spring-config-client.xml 中的 bean?

当我在 spring-config-client.xml 中编写 bean 时,一切都很好,但我需要 spring-config-client.xml 中的 bean。

【问题讨论】:

  • 声明“当我在 spring-config-client.xml 中编写 bean 时,一切都很好,但我需要 spring-config-client.xml 中的 bean。”没有任何意义。
  • 对不起我的英语。该语句意味着如果我写 那么我有 0 个 bean,但是如果我写 比我有 1 个 bean。

标签: java spring spring-mvc applicationcontext


【解决方案1】:

您可以尝试使用以下解决方法来解决此问题:

在您的依赖项目中,创建一个如下所示的配置 bean:

   @Configuration
   @ImportResource("classpath:spring-configration.xml")
   public class MyContextConfiguration {

   }

现在您可以像任何其他 bean 一样在依赖项目的 Spring 上下文中实例化这个配置 bean:

<bean id="appWatcherConfig" class="MyContextConfiguration"/>

对我来说工作得很好。

【讨论】:

  • 每次我尝试使用外部 jar 中的类创建 bean 时,我都会遇到“java.lang.ClassNotFoundException”。但这仅适用于我自己的课程。例如,我可以制作 bean 。我不明白为什么
  • 我唯一能想到的就是你的 JAR 文件所在的目录不在你的类路径中。确保您的类路径配置正确。代表您正在使用的 IDE 检查构建路径设置,如果缺少 JAR,请手动添加它。将 JAR 文件放在像 WEB-INF 这样的目录中并不能保证它的类可以被引导程序、扩展程序或应用程序类加载器找到。
  • 我使用 maven 进行依赖。我可以写INewsDAO newsDAO = new NewsDAOImpl();,也不例外。所以我猜classpath没问题。
【解决方案2】:

这取决于您在 bean 上下文中的配置,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-3.1.xsd   
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.1.xsd   
       http://www.springframework.org/schema/jee 
       http://www.springframework.org/schema/jee/spring-jee-3.1.xsd    
       http://www.springframework.org/schema/lang
       http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
       http://www.springframework.org/schema/security 
       http://www.springframework.org/schema/security/spring-security-3.1.xsd" lazy-init="false">

尝试在您的 spring 上下文文件中设置:lazy-init="false" 或 default-lazy-init="false",您必须确保您尝试加载的上下文文件已经包含 bean 定义。

希望对您有所帮助!

【讨论】:

  • cvc-complex-type.3.2.2:属性“lazy-init”不允许出现在元素“beans”中。
  • 更改为 default-lazy-init="false",但没有任何反应
  • 我需要“spring-configuration.xml”文件中的更多详细信息,所以我可以为您提供更多帮助。
猜你喜欢
  • 2013-03-12
  • 1970-01-01
  • 2010-12-26
  • 2013-07-22
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多