主:
@SpringBootApplication
@ComponentScan(basePackageClasses = Application.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
测试类:
public class Test {
@Bean
public Test test(){
return new Test();
}
}
当我试图自动装配时,我得到了这个例外:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field test in TestWithAutowire required a bean of type 'Test' that could not be found.
Action:
Consider defining a bean of type 'rcms.backend.exception.Test' in your configuration.
Process finished with exit code 1
有些事情我做错了,但我找不到。
答案 0 :(得分:1)
您可以创建新配置,例如SpringConfiguration
,
package my.pkg.config;
@Configuration
public class SpringConfiguration {
@Bean
public Test test(){
return new Test();
}
}
在Application
类中,您可以添加@ComponentScan
注释以及您希望Spring扫描类的基础包,
@SpringBootApplication
@ComponentScan(basePackageClasses = {"my.pkg.config", "my.pkg.example"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
现在,您可以在任何Spring组件中自动装配Test
。例如,
package my.pkg.example;
@Component
public class TestExample {
@Autowired
private Test tst;
}