所以我尝试使用在另一个类中生成的 bean 以在主应用程序中使用
package com.simon.spring.basics.properties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SomeExternalService {
@Value("${external.service.url}")
private String url;
public String returnServiceURL(){
return url;
}
}
主要应用程序在这里:
package com.simon.spring.basics.springin5steps;
import com.simon.spring.basics.properties.SomeExternalService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@SpringBootApplication
//@PropertySource("classpath:application.properties")
@ComponentScan()
public class SpringIn5StepsPropertiesApplication {
public static void main(String[] args) {
ApplicationContext applicationContext =
SpringApplication.run(SpringIn5StepsPropertiesApplication.class, args);
SomeExternalService service = applicationContext.getBean(SomeExternalService.class);
System.out.println(service);
}
}
所以基本上线程“main”org.springframework.beans.factory.NoSuchBeanDefinitionException中的异常:没有抛出类型为“com.simon.spring.basics.properties.SomeExternalService”的合格bean。我可以做什么来修复此错误并避免以后出现同样的问题
泛舟湖上清波郎朗
相关分类