Java 如何自动连接在其他类中用@service注释的bean

Java 如何自动连接在其他类中用@service注释的bean,java,spring,spring-boot,spring-bean,Java,Spring,Spring Boot,Spring Bean,在SPRING启动应用程序中我有一个类,如下所示 @Service public class XYZ{ } 我想在其他课程中使用以上内容 public class ABC{ @Autowired private XYZ xyx; } 它抛出找不到XYZ的错误。在编写主方法的类中,我已经有@SpringBootApplication。因此,这将自动在包上启用@ComponentScan。如何在其他类中访问该bean而不使用xml配置?您的ABC类还需要@Service或@Component注释

SPRING启动应用程序中
我有一个类,如下所示

@Service
public class XYZ{
}
我想在其他课程中使用以上内容

public class ABC{
@Autowired
private XYZ xyx;
}

它抛出找不到XYZ的错误。在编写主方法的类中,我已经有@SpringBootApplication。因此,这将自动在包上启用@ComponentScan。如何在其他类中访问该bean而不使用xml配置?

您的ABC类还需要@Service或@Component注释。否则,您将收到带有以下消息的警告

必须在有效的Springbean(@Component |@Service |…)中定义自动连接的成员


您的类ABC不在spring引导上下文中,这就是您无法获取bean的原因。您可以通过以下方式获得:

public class ABC {
   public void method() {
      XYZ xyz = ApplicationContextProvider.getApplicationContext().getBean(XYZ.class);
   }
}
创建ApplicationContextProvider

@Component
public class ApplicationContextProvider implements ApplicationContextAware {
    private static ApplicationContext context;

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        context = applicationContext;
    }
}
之后需要按以下方式调用:

public class ABC {
   public void method() {
      XYZ xyz = ApplicationContextProvider.getApplicationContext().getBean(XYZ.class);
   }
}

你的班级ABC也应该是春季管理的

您可以通过将其作为一个组件来实现这一点

@Component
public class ABC{
  @Autowired
  private XYZ xyx;
}
或者作为豆子提供

 @Configuration
 public SomeConfig{

    @Bean
    public ABC abc(){
        return new ABC();
    }
 }
在Spring boot application runner类中使用
@ComponentScan(“”
),以便它检查所有组件并创建可以自动连接的bean。调用类还应该是一个用@Componenent注释的组件。如果将ABC的对象创建为new ABC(),则

ContextProvider实现

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author dpoddar
 *
 */
@Component("ContextProvider")
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ContextProvider implements ApplicationContextAware {

    @Autowired
    private static ApplicationContext CONTEXT;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CONTEXT = applicationContext;
    }
    /**
     * @return the cONTEXT
     */
    public static ApplicationContext getApplicationContext() {
        return CONTEXT;
    }


    /**
     * Get a Spring bean by type.
     **/
    public static <T> T getBean(Class<T> beanClass) {
        return CONTEXT.getBean(beanClass);
    }

    /**
     * Get a Spring bean by type.
     **/
    public static <T> T getBean(String beanName,Class<T> beanClass) {
        return CONTEXT.getBean(beanName, beanClass);
    }

    /**
     * Get a Spring bean by name.
     **/
    public static Object getBean(String beanName) {
        return CONTEXT.getBean(beanName);
    }

}
import org.springframework.beans.BeansException;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.context.ApplicationContext;
导入org.springframework.context.ApplicationContextAware;
导入org.springframework.core.Ordered;
导入org.springframework.core.annotation.Order;
导入org.springframework.stereotype.Component;
/**
*@作者dpoddar
*
*/
@组件(“上下文提供者”)
@顺序(有序。最高优先级)
公共类ContextProvider实现ApplicationContextAware{
@自动连线
私有静态应用上下文上下文;
@凌驾
public void setApplicationContext(ApplicationContext ApplicationContext)抛出BeansException{
上下文=应用上下文;
}
/**
*@返回上下文
*/
公共静态应用程序上下文getApplicationContext(){
返回上下文;
}
/**
*按类型获取Springbean。
**/
公共静态T getBean(类beanClass){
返回CONTEXT.getBean(beanClass);
}
/**
*按类型获取Springbean。
**/
公共静态T getBean(字符串beanName,类beanClass){
返回CONTEXT.getBean(beanName,beanClass);
}
/**
*按名称获取Springbean。
**/
公共静态对象getBean(字符串beanName){
返回CONTEXT.getBean(beanName);
}
}

如果您不想配置@ComponentScan。当询问错误时,需要将类XYZ和ABC与Spring boot application runner类放在同一级别目录中

,发布实际相关代码,以及错误的完整和准确堆栈跟踪。我的猜测是,这个错误与不尊重这里描述的约定有关:如果Spring扫描这个类,就不需要创建bean。@Debopam您提供了一种有效的方法来将ABC注册为bean
autowireBean(ABC)
。。。因此abc是spring管理根据代码abc不是spring管理没有标记注释。@Debopam但如果您想让
@Autowired
工作,则必须这样做没有其他工作方式。这就是我举这个例子的原因。即使在执行new ABC()之后,Spring也可以将实例与对象自动关联。检查我的例子。你应该删除上下文中的@Autowired,你不能在Spring中自动连接静态字段。(注意:将通过setApplicationContext设置上下文)
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author dpoddar
 *
 */
@Component("ContextProvider")
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ContextProvider implements ApplicationContextAware {

    @Autowired
    private static ApplicationContext CONTEXT;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CONTEXT = applicationContext;
    }
    /**
     * @return the cONTEXT
     */
    public static ApplicationContext getApplicationContext() {
        return CONTEXT;
    }


    /**
     * Get a Spring bean by type.
     **/
    public static <T> T getBean(Class<T> beanClass) {
        return CONTEXT.getBean(beanClass);
    }

    /**
     * Get a Spring bean by type.
     **/
    public static <T> T getBean(String beanName,Class<T> beanClass) {
        return CONTEXT.getBean(beanName, beanClass);
    }

    /**
     * Get a Spring bean by name.
     **/
    public static Object getBean(String beanName) {
        return CONTEXT.getBean(beanName);
    }

}