温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Framework中@Component组件如何使用

发布时间:2021-08-12 14:31:37 来源:亿速云 阅读:124 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关Spring Framework中@Component组件如何使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

@Component 简单使用

@Component注解是一个元注解,即可以标注在其它的注解上。在spring中,任何被@Component注解标识的组件均为组件扫描的候选对象,并且被@Component元注解标注的注解,在任何组件标注它时,也被视作组件扫描的候选对象。简单来说,就是在spring中,一个普通的javaBean被@Component注解标记后,在使用基于注解配置和类路径扫描时,会被作为候选组件,添加到spring容器中

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容器启动后,控制台打印的结果:

context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c
TestComponent bean : TestComponent(id=@Component)

如此,在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容器启动后,控制台打印的结果:

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标注
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@FirstAnnotation
public @interface SecondAnnotation {
    String value() default "";
}

将上面TestComponent类的注解换成@SecondAnnotation,结果也是相同的

/**
 * spring扫描的候选组件
 */
@Data
@SecondAnnotation
public class TestComponent {
    private String id = "@SecondAnnotation";
}

spring容器启动后,控制台打印的结果:

context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c
TestComponent bean : TestComponent(id=@SecondAnnotation)

上述就是小编为大家分享的Spring Framework中@Component组件如何使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI