在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注入方式,开发者可以根据具体需求选择适合的方式来注入依赖对象。