以下为@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)
相关推荐
java 自定义注解验证 可自己添加所需要的注解,本案例中只写了三个自定义注解类 本案例只花了半天时间,如有不好之处请多提提意见。谢谢!
包括Java自定义注解以及包括自定义注解的应用实例。以及注解相关知识的注解
这个一个自定义注解实现给注解传递伪动态参数的小案例,可以在此自定义注解的基础上来记录接口的调用记录。
java自定义注解实践,介绍详细!值得参考
通过一个超市的自动售卖机的例子,详细说明了java的自定义注解的使用。
自定义注解实现由类自定生成数据库中的表,适合各种类型的数据库,本案例主要实现的时sql server和oracle
自定义注解和通过反射获取注解
3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 4、想看spring aop 注解实现记录系统日志并入库等 二、能学到什么 1、收获可用源码 2、能够清楚的知道如何用spring aop实现自定义注解以及注解的逻辑...
简单demo,导入即可运行。
Java基础复习笔记12Java自定义注解Annotation的使用
java 元注解+拦截器实现自定义注解 @CmwAutoWired:自定义依赖注入 注意:注入的接口和实现类需要在同一包名下,注解的是类则无限制 @FieldAnnotation:自定义属性注解 @MethodAnnotation:自定义方法注解 @...
利用java注解机制,实现自定义注解实例,例子中实现了简单的注解赋值和获取并打印值。
java自定义 注解 annotation、标签库tag、监听listener、junit简单测试代码
excel导入动态校验,自定义注解动态校验
主要介绍了java 自定义注解的实例详解的相关资料,需要的朋友可以参考下
自定义注解实现拦截sql,并在sql中增加相应的条件 。 对应博客: https://blog.csdn.net/qq_37716298/article/details/120659139
内容:拦截器的使用很简单,定义一个自己的拦截器,向配置中添加一下就可以使用。为了方便,之后又引入了注解。本文就将用简洁的代码构建一个springboot的拦截器。...oNoNeedToken.java 自定义注解
自定义注解结合Hutool对SpringBoot接口返回数据进行脱敏 自定义注解结合Hutool对SpringBoot接口返回数据进行脱敏 自定义注解结合Hutool对SpringBoot接口返回数据进行脱敏 自定义注解结合Hutool对SpringBoot接口返回...
Java自定义注解程序Demo
# 自定义注解的规则: - 1.定义注解 - 2.定义注解的切面 - 3.使用注解