[英]Spring: couldn't autowire field - no beans of type found
I'm struggling this for couple of hours, but couldn't figure what is wrong with my settings... 我为此奋斗了几个小时,但无法确定我的设置有什么问题...
Currently, in the TestController
, the engine field is marked with the error 当前,在
TestController
,engine字段标记有错误
"could not autowire. No beans of 'ServerEngine' type found".
“无法自动装配。找不到'ServerEngine'类型的bean”。
I already tried to replace @SpringBootApplication
with @Configuration
, @EnableAutoConfiguration
and @ComponentScan
but still getting the error. 我已经尝试用
@Configuration
@EnableAutoConfiguration
, @ComponentScan
@EnableAutoConfiguration
和@ComponentScan
替换@SpringBootApplication
,但是仍然出现错误。
below are the relevant files: 以下是相关文件:
Application.java 应用程序
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applictionContext.xml");
SpringApplication.run(Application.class, args);
}
}
ServerEngine.java (act as the main system's singleton) ServerEngine.java(充当主系统的单例)
public class ServerEngine {
@Autowired
private DataLayer dataLayer;
public DataLayer getDal(){
return dataLayer;
}
@Autowired
private UsersDal usersDal;
public UsersDal getUsersDal(){
return usersDal;
}
}
TestController.java TestController.java
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
ServerEngine engine;
@RequestMapping(value = "/users", method = RequestMethod.POST)
public void test(HttpServletRequest request, HttpServletResponse response){
engine.getUsersDal().addOrUpdateUser(...);
}
}
applictionContext.xml applictionContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="serverEngine" class="partners.dataaccess.ServerEngine"/>
<bean id="usersDal" class="partners.dataaccess.UsersDal"/>
<bean id="dataLayer" class="partners.dal.DataLayer">
<constructor-arg name="username" value="..."/>
<constructor-arg name="password" value="..."/>
<constructor-arg name="url" value="..."/>
</bean>
</beans>
In main
, you're instantiating an ApplicationContext applicationContext
, but then you do nothing with it - you don't pass it to the SpringApplication
. 在
main
,你实例化一个ApplicationContext applicationContext
,但你什么都不做吧-你不将它传递给SpringApplication
。
I'm not familiar with Spring Boot, but I see no place in your code in which the XML config's name is given to the SpringApplication
either. 我对Spring Boot不熟悉,但是在您的代码中也看不到将XML配置的名称赋予
SpringApplication
任何地方。 So it probably only uses annotation config. 因此,它可能仅使用注释配置。
And finally, since ServerEngine
isn't annotated with @Service
, it won't be instantiated as a Spring bean during component-scan. 最后,由于
ServerEngine
并未使用@Service
注释,因此在组件扫描期间不会将其实例化为Spring Bean。
The easiest and best solution would be to annotate the classes that you are autowiring in you application like ServerEngine, UsersDal and DataLayer with the annotation @Service or @Component. 最简单,最好的解决方案是,使用注解@Service或@Component注释应用程序中自动装配的类,例如ServerEngine,UsersDal和DataLayer。 If you do that, you don't have to explicitly create beans for them in xml or java configurations.
如果这样做,则不必在xml或java配置中为它们显式创建bean。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.