@component和@autowired没有解析依赖bean

zysjyyx4  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(332)

beans.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"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd"
            >

        <!-- bean definitions here --> 

        <bean id="point" class="com.example.demo.Point">
            <qualifier value="circleRelated" />
            <property name="x" value="0"></property>
            <property name="y" value="0"></property>
        </bean>

        <context:annotation-config/>

        <context:component-scan base-package="com.example.demo" />

使用main方法初始化:

package com.example.demo;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    @SpringBootApplication
    public class SpringSecondApplication {

        public static void main(String[] args) {
            SpringApplication.run(SpringSecondApplication.class, args);

            ApplicationContext context = new ClassPathXmlApplicationContext ("beans.xml");

            Shape shape = (Shape) context.getBean("circle");
            shape.draw();

        }

    }

圆圈.java

package com.example.demo;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
@ComponentScan(basePackages="com.example.demo")
public class Circle implements Shape {

    @Autowired
    @Qualifier("circleRelated")
    private Point point;

    public Circle(Point point) {
        super();
        this.point = point;
    }

    public Point getPoint() {
        return point;
    }

    public void setPoint(Point point) {
        this.point = point;
    }

    public void draw() {
        System.out.println("Draw is called");
        System.out.println("center.x:" + point.getX());
        System.out.println("center.y:" + point.getY());
    }

}

点.java

package com.example.demo;

import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
public class Point {

    private int x;
    private int y;
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }

}

形状.java

package com.example.demo;

public interface Shape {

    public void draw();

}

获取错误如下:
上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.unsatifieddependencyException:创建名为“circle”的bean时出错:通过字段“point”表示的未满足的依赖关系;嵌套异常为org.springframework.beans.factory.nosuchbeandefinitionexception:没有类型为“com.example.demo.point”的合格bean可用:至少需要1个符合autowire候选的bean。依赖项注解:{@org.springframework.beans.factory.annotation.autowired(required=true),@org.springframework.beans.factory.annotation.qualifier(value=circlerrelated)}[2m2021-03-28 12:17:31.958[0;39m[32m信息[0;39m[35m2747[0;39m[2m---[0;39m[2m[干管][0;39m[36M条件评估报告日志监听器[0;39m[2m:[0;3900万
启动applicationcontext时出错。要显示条件报告,请在启用“调试”的情况下重新运行应用程序[2m2021-03-28 12:17:31.977[0;39米[31米];39m[35m2747[0;39m[2m---[0;39m[2m[干管][0;39m[36mo.s.b.d.记录故障分析报告器[0;39m[2m:[0;3900万
应用程序启动失败
说明:
com.example.demo.circle中的字段点需要找不到类型为“com.example.demo.point”的bean。
注入点具有以下注解:-@org.springframework.beans.factory.annotation.autowired(required=true)-@org.springframework.beans.factory.annotation.qualifier(value=circlerrelated)
找到以下候选对象,但无法注入:-用户定义的bean
行动:
考虑重新访问上面的条目或在配置中定义类型为“com.example.demo.point”的bean。

xxb16uws

xxb16uws1#

您可以通过 @ImportResource 只是 @Autowire 或通过 EventListener .
下面是一个在应用程序准备就绪时绘制圆的示例( ApplicationReadyEvent )

@SpringBootApplication
@ImportResource("classpath:beans.xml")
public class SpringSecondApplication {

   public static void main(String[] args) {
      SpringApplication.run(SpringSecondApplication.class, args);
   }

   @EventListener
   public void init(ApplicationReadyEvent event){
      Shape shape = (Shape) event.getApplicationContext().getBean("circle");
      shape.draw();
   }

编辑:回顾我的答案 @ImportResource("classpath:beans.xml") 不是严格需要的,但为清楚起见,建议使用( beans.xml 将自动检测)
如果您想在main方法中获得一个bean(这里是 circle )你可以,但在这里,我不推荐。
这应该起作用:

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);

        Shape shape = (Shape) context.getBean("circle");
        shape.draw();
    }
}

代码中存在冲突或可能导致冲突的内容: @ComponentPoint 和你肚子里的豆子一样
beans.xml @ComponentScan(basePackages="com.example.demo")Circle :这毫无意义。。。 ApplicationContext context = new ClassPathXmlApplicationContext ("beans.xml")SpringApplication.run(...) 返回一个 ApplicationContext

相关问题