现在我非常困惑,我想在我的一个Controller类中使用@Autowired MongoClient属性,但没有成功。它的棘手部分是@Autowired正在从我的@RestController工作。Spring Boot - Autowired MongoClient
@RestController
public final class WebController {
/** mongoClient */
@Autowired
private MongoClient mongoClient; <- here it's working ...
...
}
但:
@Controller
public final class MongoUsersDAO {
/** mongoClient */
@Autowired
private MongoClient mongoClient; <- not working ...
...
}
在这里我得到空。
我不认为这个问题将是组件扫描,而我@SpringBootApplication位于XYZ,我在XYZT @RestController和我在xyzk包@Controller,因此他们的摊位应由Spring扫描。 (Eclipse也将我的@Controller标记为Spring类)
那么还有什么问题呢?
注: 如果我添加这对我@Controller它的正常工作,但仍@Autowired工作wount:
@PostConstruct
public void init() {
System.out.println("INIT");
}
注:在提到MongoUsersDAO的自动装配Autowired的事情是根本不工作,我我试图从application.properties中获得一个简单的属性,但没有成功。
奇怪。任何日志?你叫什么新的MongoUsersDAO()? 。奇怪为什么DAO类是用@Controller注释的? – Barath
我试着用服务,组件等对它进行注释。新的请求发出时,新的mongoUsersDAO()在WebController中调用。 (这是一个测试实现) –
这就是问题所在。不要调用新的mongoUserdDao()。如果你这样做,自动装配将不会发生。 – Barath