JDK内置的注解
1.自己动手写一个注解类
定义注解类
Target 定义该注解作用在方法和类上
Retention 描述注解作用范围
java RetentionPolicy.SOURCE
java+class RetentionPolicy.CLASS
java+class+jvm RetentionPolicy.RUNTIME
Name注解
@Documented//是否在帮助文档中出现
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface Name {
String value(); //注解有一个属性为value
}
Description注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Description {
String descript() default ""; //注解有一个属性为descript
}
注解的使用
@Name(value="lzl")
public class LzlName {
@Name("gxr")
public void sayHello(){
System.out.println(" 你好!");
}
@Description(descript="是个Java程序员~")
public void descript(){
System.out.println("将来会很牛逼!");
}
}
注解解析类
public class AnnotationParse {
public static void parse(){
Class classt = LzlName.class;
/**
* 解析类上的注解
*/
if(classt.isAnnotationPresent(Name.class)){//判断是否存在注解类
Name name = (Name) classt.getAnnotation(Name.class);
System.out.println(name.value());
}
/**
* 方法中的注解
*/
Method[] methods = classt.getMethods();
for(Method m : methods){
if(m.isAnnotationPresent(Name.class)){
Name n = (Name)m.getAnnotation(Name.class);
System.out.println(n.value());
}
if(m.isAnnotationPresent(Description.class)){
Description d = m.getAnnotation(Description.class);
System.out.println(d.descript());
}
}
}
@Test
public void test(){
AnnotationParse.parse();
}
}
Spring的注解
引入注解文件步骤
导入命名空间
xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
导入依赖注入的注解解析器
<context:annotation-config></context:annotation-config>
把所需的Bean引入进来.
注解 Xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config></context:annotation-config>
<bean id="student" class="com.lzl.test.di.annotation.Student"></bean>
<bean id="person" class="com.lzl.test.di.annotation.Person"></bean>
</beans>
Java类
Person.java类
public class Person {
/*@Resource(name="student")*/
@Autowired //按照类型进行匹配
private Student stu;
private String name;
public void sayPerson(){
this.stu.sayStu();
}
}
Student类
public class Student {
private Long pid;
public void sayStu(){
System.out.println("=hello Stu!=");
}
}
测试类
public class PersonTest extends SpringHelper{
static{
path="com/lzl/test/di/annotation/applicationContext.xml";
}
@Test
public void test(){
Person p = (Person) fileResource.getBean("person");
p.sayPerson();
}
}
Spring注解解析的流程
- spring启动容器
- 创建Person和Student对象
- 解析注解
- 判断@Resource注解是否为空
4.1 如果注解为空,匹配xml文件中id是否与Person类的属性名是否匹配
– 4.1.1 如果匹配,就给对象赋值
– 4.1.2 不匹配,就抛错
4.2 如果注解不为空,就按照注解中的name值与Person类的属性匹配
– 4.2.1 如果匹配,就给对象赋值
– 4.2.1 如果不匹配,就报错
注意:注解只能作用在引用类型上