Spring 实现自定义注解

1. 创建一个自定义注解MyService

在这里插入图片描述

package org.example.annotation;

import java.lang.annotation.*;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyService {

    String value() default "";

}

2. 创建一个类实现BeanDefinitionRegistryPostProcessor接口

在这里插入图片描述

package org.example.annotation;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Component;

@Component
public class AnnotationTest implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        // 1.创建扫描器
        ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry);
        // 2.设置要被扫描到的注解名称,加入要被扫描到的注解的集合IncludeFilter中
        scanner.addIncludeFilter(new AnnotationTypeFilter(MyService.class));
        // 3.设置扫描路径
        scanner.scan("org.example");
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}

3. 创建一个实体,添加注解@MyService

在这里插入图片描述

package org.example.bean;

import org.example.annotation.MyService;

@MyService
public class MyAnnotationClass {

    public String name = "MyAnnotationClassTest";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

4. 测试方法CustomAnno

  • 单元测试,如果要使用@Autowired注入时,就必须使用@RunWith注解和@ContextConfiguration注解

在这里插入图片描述

package org.example.test;

import org.example.bean.MyAnnotationClass;
import org.example.bean.Student;
import org.example.beanDefinition.UserClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

//如果要使用@Autowired注入时,就必须使用@RunWith注解和@ContextConfiguration注解
//让测试运行于Spring测试环境
@RunWith(SpringJUnit4ClassRunner.class)
//Spring整合JUnit4测试时,使用注解引入配置文件
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class MyTest {
    @Autowired
    MyAnnotationClass myAnnoClass;

    @Test
    public void CustomAnno() {
        System.out.println("CustomAnno--->" + myAnnoClass.getName());
    }

    @Test
    public void test1() {
        AnnotationConfigApplicationContext appcationContext =
                new AnnotationConfigApplicationContext("org.example");
        Student student = (Student)appcationContext.getBean("student");
        System.out.println(student.getUsername());
    }

    @Test
    public void test2() {
        //基于xml的方式加载spring的配置文件,启动spring容器
        ClassPathXmlApplicationContext appcationContext =
                new ClassPathXmlApplicationContext("spring.xml");
        Student student = (Student)appcationContext.getBean("student");
        System.out.println(student.getUsername());
    }

    @Test
    public void test3() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext("org.example");
        UserClass userClass = (UserClass)applicationContext.getBean("UserClass");
        System.out.println("UserClass-->" + userClass.getUsername());
    }

}
  • 查看打印

在这里插入图片描述

代码下载地址

https://gitee.com/fisher3652/spring.git

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fisher3652

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值