写在前面java
在spring大行其道的今天,对于spring的使用和掌握乃是不可缺乏的必备技能。可是spring的整个体系尤其庞大,对它的学习,还得从基础一点一滴的慢慢积累。本文主要介绍
@Component
注解在spring中的简单使用,以及注解的派生性
和层次性
spring
@Component
注解是一个元注解,便可以标注在其它的注解上。在spring中,任何被@Component
注解标识的组件均为组件扫描的候选对象,而且被@Component
元注解标注的注解,在任何组件标注它时,也被视做组件扫描的候选对象。简单来讲,就是在spring中,一个普通的javaBean被@Component
注解标记后,在使用基于注解配置和类路径扫描时,会被做为候选组件,添加到spring容器中app
package com.spring.study.ioc.register;
/** * spring扫描的候选组件 * * @author TangFD * @since 2019/6/25. */
@Data
@Component
public class TestComponent {
private String id = "@Component";
}
复制代码
添加spring启动引导类,以及spring启动时须要扫描的类路径学习
/** * spring 容器启动引导类 * * @author TangFD * @since 2019/6/25. */
@ComponentScan("com.spring.study.ioc.register")
public class TestComponentBootstrap {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext(TestComponentBootstrap.class);
System.out.println("context id : " + applicationContext.getId());
TestComponent bean = applicationContext.getBean(TestComponent.class);
System.out.println("TestComponent bean : " + bean);
applicationContext.close();
}
}
复制代码
spring容器启动后,控制台打印的结果:spa
context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c TestComponent bean : TestComponent(id=@Component)code
如此,在spring中经过简单在一个普通的javaBean上添加@Component
注解,再加上扫描类路径,就能够将该javaBean添加到spring容器中。spring就能够对这个Bean的生命周期进行管理。对象
前面提到
@Component
是一个元注解,当它标记在另外一个注解上时,该组件一样会具备被spring扫描,并识别组件的能力。在spring中,被@Component
标记的注解有不少,例如:@Controller
,@Service
,@Repository
,当一个普通的javaBean被这些注解标注时,spring容器启动时一样会把该Bean视为候选组件,添加到容器中。继承
将上面TestComponent
类的注解换成@Service
,结果也是相同的生命周期
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
@AliasFor(annotation = Component.class)
String value() default "";
}
复制代码
/** * spring扫描的候选组件 * * @author TangFD * @since 2019/6/25. */
@Data
@Service
public class TestComponent {
private String id = "@Service";
}
复制代码
spring容器启动后,控制台打印的结果:get
context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c TestComponent bean : TestComponent(id=@Service)
派生性
和层次性
这两个概念源自慕课网中小马哥的课程介绍,严格上讲,注解是没有派生性和层次性的,之因此这样讲,是由于在spring中的不少注解都是有着派生性和层次性的结构。经过这两种特性,咱们也能够自定义本身的注解,利用@Component
元注解,来将普通的javaBean扫描添加到spring容器中
派生性 自定义一个注解@FirstAnnotation
,被@Component元注解标识,并保持相同的签名,当有组件使用@FirstAnnotation 注解标注时,就会被spring容器扫描并加载
/** * 自定义注解@FirstAnnotation,被@Component标注 * @author TangFD * @since 2019/6/10. */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface FirstAnnotation {
String value() default "";
}
复制代码
将上面TestComponent
类的注解换成@FirstAnnotation
,结果也是相同的
/** * spring扫描的候选组件 * * @author TangFD * @since 2019/6/25. */
@Data
@FirstAnnotation
public class TestComponent {
private String id = "@FirstAnnotation";
}
复制代码
spring容器启动后,控制台打印的结果:
context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c TestComponent bean : TestComponent(id=@FirstAnnotation)
层次性
Spring模式注解并不具备真正的派生性和层次性,只是像java类同样,具备相似继承和层次结构的功能
自定义一个注解@SecondAnnotation
,被@FirstAnnotation
注解标识,当有组件使用@SecondAnnotation
注解标注时,一样会被spring容器扫描并加载
/** * 自定义注解@SecondAnnotation ,被@FirstAnnotation标注 * * @author TangFD * @since 2019/6/11. */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@FirstAnnotation
public @interface SecondAnnotation {
String value() default "";
}
复制代码
将上面TestComponent
类的注解换成@SecondAnnotation
,结果也是相同的
/** * spring扫描的候选组件 * * @author TangFD * @since 2019/6/25. */
@Data
@SecondAnnotation
public class TestComponent {
private String id = "@SecondAnnotation";
}
复制代码
spring容器启动后,控制台打印的结果:
context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c TestComponent bean : TestComponent(id=@SecondAnnotation)
本文介绍了spring中
@Component
元注解的简单使用,并经过示例说明了注解的继承和层次性功能。文章内容很是简单,只要对spring有所了解和入门,都不会有什么问题。
之因此把这些简单的内容拿出来写,一是给本身在写博客,或者说是记录学习笔记的路上开一个简单的头,二是将本身认为会的东西进行梳理,进一步理解,巩固本身的知识。
学习永远都不是一件简单的事情,能够有迷茫,能够懒惰,可是前进的脚步永远都不能中止。
不积跬步,无以致千里;不积小流,无以成江海;