求助:Spring 如何在程序中调用其它jar包中装配好的对象

WLFIGHTER 2016-06-25 04:11:58
我在一个jar包中使用spring配置好了一个对象hello,我想让该对象被其它jar包调用。但是我调用该对象的时候,spring总是报找不到该对象。麻烦各位大神帮我看一下。

错误信息如下:

Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@57baeedf: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,proxy,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'proxy': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.learn.HelloProxy.setHello(com.learn.Hello); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.learn.Hello] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=hello)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)


这是调用的代码:

package com.learn;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:**/bean.xml");
HelloProxy obj = (HelloProxy) context.getBean("proxy");
System.out.println(obj.getHello().getMessage());
}
}



package com.learn;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class HelloProxy {
Hello hello;

@Autowired
public void setHello(@Qualifier("hello")Hello hello) {
this.hello = hello;
}

public Hello getHello() {
return this.hello;
}
}


spring配置文件:

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://cxf.apache.org/transports/http/configuration
http://cxf.apache.org/schemas/configuration/http-conf.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd"
default-autowire="byName">
<context:annotation-config />


<bean id="proxy" class="com.learn.HelloProxy">
</bean>
</beans>
...全文
292 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
a-ray-of-sunshine 2016-06-26
  • 打赏
  • 举报
回复
不行,不能获取到。不同的jar ,自然是不同的 ApplicationContext,hello对象是在你当前的 context 并不可见,所以无法获取到。
WLFIGHTER 2016-06-26
  • 打赏
  • 举报
回复
@a-ray-of-sunshine 我想直接获取另一个jar中spring已经配置好的‘hello’对象,这样能实现吗?
WLFIGHTER 2016-06-26
  • 打赏
  • 举报
回复
我想直接获取另一个jar中spring已经配置好的‘hello’对象,这样能实现吗?
a-ray-of-sunshine 2016-06-25
  • 打赏
  • 举报
回复
spring 容器在创建 proxy 对象的时候,也就是 初始化 com.learn.HelloProxy 对象的时候,需要注入 hello 对象

    @Autowired
    public void setHello(@Qualifier("hello")Hello hello) {
        this.hello = hello;
    }
而配置文件并没有 hello 对象的相关 bean 的配置。所以,上面方法中的 hello 对象注入失败,spring 容器就启动失败了。

67,516

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧