以下为@Controller的源码
/*
*Copyright 2002-2007 the original author or authors.
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/
package org.springframework.stereotype;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*Indicates that an annotated class is a "Controller" (e.g. a webcontroller).
*
*<p>This annotation serves as a specialization of {@link Component@Component},
*allowing for implementation classes to be autodetected through classpathscanning.
* Itis typically used in combination with annotated handler methods based on the
*{@link org.springframework.web.bind.annotation.RequestMapping} annotation.
*
*@author Arjen Poutsma
*@author Juergen Hoeller
*@since 2.5
*@see Component
*@see org.springframework.web.bind.annotation.RequestMapping
*@see org.springframework.context.annotation.ClassPathBeanDefinitionScanner
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
/**
* The value may indicate a suggestion for alogical component name,
* to be turned into a Spring bean in case ofan autodetected component.
* @return the suggested component name, if any
*/
Stringvalue() default "";
}
/*
*自定义注解
*/
@Target()
@Retention()
Public @interface Name {
StringXXXName default "";
}
1. @Target表示该注解用于什么地方,可能的ElementType参数包括:
ElementType.CONSTRUCTOR 构造器声明
ElementType.FIELD 域声明
ElementType.LOCAL_VARIABLE 局部变量声明
ElementType.METHOD 方法声明
ElementType.PACKAGE 包声明
ElementType.PARAMETER 参数声明
ElementType.TYPE 类,接口(包括注解类型)或enum声明
2. @Retention表示在什么级别保存该注解信息。可选RetentionPolicy参数包括:
RetentionPolicy.SOURCE 注解将被编译器丢弃
RetentionPolicy.CLASS 注解在class文件中可用,但会被vm丢弃
RetentionPolicy.RUNTIME vm讲在运行期也保留注释,因此可以通过反思机制读取注解信息
/*
* 元注解@Target,@Retention,@Documented,@Inherited
*
* @Target 表示该注解用于什么地方,可能的ElemenetType 参数包括:
* ElemenetType.CONSTRUCTOR 构造器声明
* ElemenetType.FIELD 域声明(包括 enum 实例)
* ElemenetType.LOCAL_VARIABLE 局部变量声明
* ElemenetType.METHOD 方法声明
* ElemenetType.PACKAGE 包声明
* ElemenetType.PARAMETER 参数声明
* ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
*
* @Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括:
* RetentionPolicy.SOURCE 注解将被编译器丢弃
* RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
* RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
*
* @Documented 将此注解包含在 javadoc 中
*
* @Inherited 允许子类继承父类中的注解
*
*/
example
/**
* Copyright 2011 by ekupeng
*/
package annotationTest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author liuyuansheng
* create date 2011-8-27 下午06:25:13
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Own {
public int id() default 0;
public String description() default "no description";
}
/**
* Copyright 2011 by ekupeng
*/
package annotationTest;
import java.lang.reflect.Method;
import ClassTest.Test;
/**
* @author liuyuansheng
* create date 2011-8-27 下午06:27:37
*/
public class Test_1 {
/**
* @param args
*/
@Own(id = 1,description = "hello method_1")
public void method_1() {
}
@Own(id = 2)
public void method_2() {
}
@Own(id = 3,description = "last method")
public void method_3() {
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Method[] methods = Test_1.class.getDeclaredMethods();
for (Method method : methods) {
boolean hasAnnotation = method.isAnnotationPresent(Own.class);
if (hasAnnotation) {
Own annotation = method.getAnnotation(Own.class);
System.out.println("Test( method = " + method.getName()
+ " ,id = " + annotation.id() + ", description = "
+ annotation.description() + ")");
}
}
}
}
output:
Test( method = method_1 ,id = 1, description = hello method_1)
Test( method = method_2 ,id = 2, description = no description)
Test( method = method_3 ,id = 3, description = last method)