![](/img/trans.png)
[英]how to call MongoTemplate function on Autowired instance of MongoTemplate in Spring boot
[英]How to use @Autowired MongoTemplate in multiple classes in spring boot
嗨,我是春季引導的新手,我正嘗試通過自動裝配使用mongotemplate,這是我的課程
@SpringBootApplication
public class Test implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(ProducerConsumerApplication.class, args).close();
}
@Autowired
private MongoTemplate mongoTemplate;
@Override
public void run(String... strings) throws Exception {
new Myclass().insert();
}
這是我的application.yml的樣子。
spring:
kafka:
bootstrap-servers: 192.168.155.100:9092
consumer:
group-id: foo
data:
mongodb:
host: 192.168.155.100
port: 27017
database: test
我試圖在Myclass中使用mongo模板,並且從Test類調用它,但是mongotemplate為null。 我應該怎么做? 任何幫助表示贊賞
@Component
注釋@Component
或從@Bean
方法返回它) Test
類中的MyClass
bean run
方法中,對該字段調用insert()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.