spring boot 注解

@SpringBootApplication

注解中组合了@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan。因此,在实践过程中也可以使用这三个注解来替代@SpringBootApplication。

@SpringBootConfiguration 

  1. 标注这个类是一个配置类;

  2. 它只是@Configuration注解的派生注解;

  3. 它与@Configuration注解的功能一致;

  4. 只不过@SpringBootConfiguration是springboot的注解,而@Configuration是spring的注解。

@EnableAutoConfiguration

将所有符合条件的@Configuration中的bean定义加载到IoC容器。(扫描各个(redis,jpa等框架jar)jar包的META-INF目录下的spring.factories文件,并加载其中注册的AutoConfiguration类等),spring boot所以能大量减少用户的配置工作量是因为默认编写了很多配置类(被@Configuration注解的类。

@Configuration(SpringBootConfiguration和@Configuration作用是相同的

标注这个类是一个配置类;,在springboot中我们大多用配置类来配置(此注解相当于配置文件)。

@Bean

任何一个标注了@Bean的方法,将作为一个对象注册到Spring的IoC容器,方法名将默认成该bean名称(此注解相当于xml配置文件中的bean配置)。

@Configuration
public class MockConfiguration{

 @Bean
 public DependencyService dependencyService(){
     return new DependencyServiceImpl();
 }
}

@ComponentScan

扫描指定注解的类注册到IOC容器中,会被自动装配的注解包括@Controller、@Service、@Component、@Repository等等 (相对应的XML配置就是<context:component-scan/>)

@ComponentScan(value="com.maple.learn",
   excludeFilters = {@ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class)},
   includeFilters = {@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class})}
        )
public class SampleClass{
   ……

注意:类上有@SpringBootApplication注解的包及其子包都会扫描
所以当我们配置了@Controller后,并没有配置扫描包,一样能扫描到。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值