在Spring Boot中,Autowired注入方式有以下几种:
@Service
public class MyService {
private final MyRepository myRepository;
@Autowired
public MyService(MyRepository myRepository) {
this.myRepository = myRepository;
}
}
@Service
public class MyService {
@Autowired
private MyRepository myRepository;
}
@Service
public class MyService {
private MyRepository myRepository;
@Autowired
public void setMyRepository(MyRepository myRepository) {
this.myRepository = myRepository;
}
}
@Service
public class MyService {
@Autowired
private MyRepository myRepository;
}
@Service
public class MyService implements MyInterface {
@Autowired
private MyRepository myRepository;
}
以上是Spring Boot中常用的@Autowired注入方式,开发者可以根据具体需求选择适合的方式来注入依赖对象。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:springboot自动注入的方式有哪些