【发布时间】:2020-08-10 14:58:20
【问题描述】:
我在构建rest控制器的spring boot应用程序中。我发现ApplicationContext和JdbcTemplate源代码,这两个类没有任何注释。但是它们可以正确注入构造函数。我没有使用任何配置文件,例如'applicationContext.xml'。这两个类什么时候被spring ioc容器扫描?
更新:我把@SpringBootApplication拆分成@Configuration,@EnableAutoConfiguration,@ComponentScan。然后去掉@EnableAutoConfiguration,注入仍然有效,为什么?
@SpringBootApplication
@RestController
@CrossOrigin
@Validated
@RequestMapping(value = "/demo")
public class DemoController {
private ApplicationContext applicationContext;
private JdbcTemplate jdbcTemplate;
@Autowired
public DemoController(ApplicationContext applicationContext, JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.applicationContext = applicationContext;
}}
【问题讨论】:
标签: java spring spring-boot