Spring Boot从一开始就告诉你,它更喜欢基于Java的配置,即注解的方式。所以它提供了你一大堆注解,并让你习惯使用注解。
@Bean
Indicates that a method produces a bean to be managed by the Spring container.
@Configuration
Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime
@EnableAutoConfiguration
Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined.
@ComponentScan
Configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML’s <context:component-scan> element.
@EnableTransactionManagement的作用:Enables Spring’s annotation-driven transaction management capability, similar to the support found in Spring’s <tx:*> XML namespace。
ConfigurableApplicationContextctx=SpringApplication.run(Application.class,args);System.out.println("Let's inspect the beans provided by Spring Boot:");ObjectdataSource=ctx.getBean("dataSource");ObjecttransactionManager=ctx.getBean("transactionManager");ObjectentityManagerFactory=ctx.getBean("entityManagerFactory");System.out.println(dataSource);System.out.println(entityManagerFactory);System.out.println(transactionManager);System.out.println(((JpaTransactionManager)transactionManager).getDataSource());System.out.println(((JpaTransactionManager)transactionManager).getEntityManagerFactory());