自定义注解
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