![](/img/trans.png)
[英]How to autowire @service class ? No qualifying bean of type found for dependency
[英]How to autowire a bean annoted with @service in other class
在SPRING BOOT application
我有一個如下類
@Service
public class XYZ{
}
我想在其他班級ABC中使用
public class ABC{
@Autowired
private XYZ xyx;
}
引發錯誤,找不到XYZ。 我在編寫main方法的類中已經有@SpringBootApplication了。 因此,這將自動在包上啟用@ComponentScan。我的理解是,由於XYZ已使用@service進行注釋,因此spring掃描並創建並注冊該bean。 我如何不使用xml配置訪問其他類中的bean?
那么您的ABC類還需要具有@Service或@Component批注。 否則,您將收到以下消息警告。
自動連線的成員必須在有效的Spring bean(@Component | @Service | ...)中定義。
@Service
public class ABC {
@Autowired
private XYZ xyx;
}
您的ABC類不在spring boot上下文中,這就是您無法獲得bean的原因。 您可以通過以下方式獲得它:
創建一個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);
}
}
如果您不想配置@ComponentScan。 您需要將XYZ類和ABC類與Spring Boot應用程序運行器類放在同一級別的目錄中
您的班級ABC也應該是春季管理的
您可以通過使其成為組件來實現
@Component
public class ABC{
@Autowired
private XYZ xyx;
}
或作為Bean提供
@Configuration
public SomeConfig{
@Bean
public ABC abc(){
return new ABC();
}
}
在Spring引導應用程序運行程序類中使用@ComponentScan("<your root package>")
,以便它檢查所有組件並創建可以自動裝配的bean。 同樣,調用類應該是帶有@Componenent注釋的組件。 如果您將ABC的對象創建為新的ABC(),則
ABC abc = new ABC();
ContextProvider.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(abc); //Autowiring dependencies
//Then use the abc object which will have instance of XYZ populated.
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.