【问题标题】:How does spring boot inject the instance of ApplicationContext and JdbcTemplate using @Autowired without @Component annotation and xml configuration?spring boot如何在没有@Component注解和xml配置的情况下使用@Autowired注入ApplicationContext和JdbcTemplate的实例?
【发布时间】: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


    【解决方案1】:

    Spring Boot 做了很多自动配置。

    我假设您使用的是 spring-data-jdbc 或 spring-data-jpa 并且 JdbcTemplate 是自动配置的。

    ApplicationContext 无论如何都会存在,因为这是以编程方式访问 bean 的 bean 工厂类。

    您可以在这里查看源代码:https://github.com/spring-projects/spring-boot

    最有趣的项目是:spring-boot-autoconfigure,所有魔法都发生在这里。

    你会在那里找到 JdbcTemplateConfiguration.java

    @Configuration(proxyBeanMethods = false)
    @ConditionalOnMissingBean(JdbcOperations.class)
    class JdbcTemplateConfiguration {
    
        @Bean
        @Primary
        JdbcTemplate jdbcTemplate(DataSource dataSource, JdbcProperties properties) {
            JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
            JdbcProperties.Template template = properties.getTemplate();
            jdbcTemplate.setFetchSize(template.getFetchSize());
            jdbcTemplate.setMaxRows(template.getMaxRows());
            if (template.getQueryTimeout() != null) {
                jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
            }
            return jdbcTemplate;
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      这里主要的 Spring 引导目标

      • 避免在 Spring 中进行复杂的 XML 配置
      • 以更简单的方式开发生产就绪的 Spring 应用程序
      • 减少开发时间并独立运行应用程序
      • 提供更简单的应用程序入门方式

      Spring boot 通过启动器依赖的概念在configuration by exception 中工作。

      它会根据您在项目中添加的 JAR 依赖项自动配置您的 Spring 应用程序。

      starter-jdbc 为例,您无需任何xml 配置即可自动注入JdbcTemplate

      使用starter-test,您会在类路径中自动获得这些库Spring Test, JUnit, Hamcrest, and Mockito

      使用starter-mail,您有一个开箱即用的JavaMail bean,您可以将其注入您的服务...

      等等……

      所有这些魔法都伴随着这个注解@EnableAutoConfiguration

      这个注解会触发 Spring Application Context 的自动配置,尝试猜测和配置你可能需要的 bean。自动配置类通常根据您的类路径应用。

      使用@SpringBootApplication 时,上下文的自动配置会自动启用,因此添加此注解不会产生额外的影响。

      【讨论】:

        猜你喜欢
        • 2019-10-22
        • 1970-01-01
        • 2016-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-14
        • 2014-07-16
        • 2019-06-24
        相关资源
        最近更新 更多