自定义注解
1. 创建一个自定义注解MyService2. 创建一个类实现BeanDefinitionRegistryPostProcessor接口3. 创建一个实体,添加注解@MyService4. 测试方法CustomAnno代码下载地址
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
{
ClassPathBeanDefinitionScanner scanner
= new ClassPathBeanDefinitionScanner(registry
);
scanner
.addIncludeFilter(new AnnotationTypeFilter(MyService
.class));
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
;
@RunWith(SpringJUnit4ClassRunner
.class)
@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() {
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