栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring boot 注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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配置就是

@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后,并没有配置扫描包,一样能扫描到。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/877207.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号