- 注 音
- ㄓㄨˋ ㄐㄧㄝ ˇ
- 拼 音
- zhù jiě
- 出 自
- 《后汉书·儒林传上·杨伦》
- 中文名
- 注解
-
深入JAVA注解(Annotation):自定义注解
2019-07-31 18:40:53要深入学习注解,我们就必须能定义自己的注解,并使用注解,在定义自己的注解之前,我们就必须要了解Java为我们提供的元注解和相关定义注解的语法。 元注解: 元注解的作用就是负责注解其他注解。Java5.0定义了4...一、基础知识:元注解
要深入学习注解,我们就必须能定义自己的注解,并使用注解,在定义自己的注解之前,我们就必须要了解Java为我们提供的元注解和相关定义注解的语法。
元注解:
元注解的作用就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明。Java5.0定义的元注解:
1.@Target,
2.@Retention,
3.@Documented,
4.@Inherited
这些类型和它们所支持的类在java.lang.annotation包中可以找到。下面我们看一下每个元注解的作用和相应分参数的使用说明。@Target:
@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
取值(ElementType)有:
1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型) 或enum声明使用实例:
@Target(ElementType.TYPE) public @interface Table { /** * 数据表名称注解,默认值为类名称 * @return */ public String tableName() default "className"; } @Target(ElementType.FIELD) public @interface NoDBColumn { }
注解Table 可以用于注解类、接口(包括注解类型) 或enum声明,而注解NoDBColumn仅可用于注解类的成员变量。
@Retention:
@Retention定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotation与class在使用上是被分离的)。使用这个meta-Annotation可以对 Annotation的“生命周期”限制。
作用:表示需要在什么级别保存该注释信息,用于描述注解的生命周期(即:被描述的注解在什么范围内有效)
取值(RetentionPoicy)有:
1.SOURCE:在源文件中有效(即源文件保留)
2.CLASS:在class文件中有效(即class保留)
3.RUNTIME:在运行时有效(即运行时保留)Retention meta-annotation类型有唯一的value作为成员,它的取值来自java.lang.annotation.RetentionPolicy的枚举类型值。具体实例如下:
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Column { public String name() default "fieldName"; public String setFuncName() default "setField"; public String getFuncName() default "getField"; public boolean defaultDBValue() default false; }
Column注解的的RetentionPolicy的属性值是RUTIME,这样注解处理器可以通过反射,获取到该注解的属性值,从而去做一些运行时的逻辑处理
@Documented:
@Documented用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Column { public String name() default "fieldName"; public String setFuncName() default "setField"; public String getFuncName() default "getField"; public boolean defaultDBValue() default false; }
@Inherited:
@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。
注意:@Inherited annotation类型是被标注过的class的子类所继承。类并不从它所实现的接口继承annotation,方法并不从它所重载的方法继承annotation。
当@Inherited annotation类型标注的annotation的Retention是RetentionPolicy.RUNTIME,则反射API增强了这种继承性。如果我们使用java.lang.reflect去查询一个@Inherited annotation类型的annotation时,反射代码检查将展开工作:检查class和其父类,直到发现指定的annotation类型被发现,或者到达类继承结构的顶层。
实例代码:
/** * * @author peida * */ @Inherited public @interface Greeting { public enum FontColor{ BULE,RED,GREEN}; String name(); FontColor fontColor() default FontColor.GREEN; }
二、基础知识:自定义注解
使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节。在定义注解时,不能继承其他的注解或接口。@interface用来声明一个注解,其中的每一个方法实际上是声明了一个配置参数。方法的名称就是参数的名称,返回值类型就是参数的类型(返回值类型只能是基本类型、Class、String、enum)。可以通过default来声明参数的默认值。
定义注解格式:
public @interface 注解名 {定义体}注解参数的可支持数据类型:
1.所有基本数据类型(int,float,boolean,byte,double,char,long,short)
2.String类型
3.Class类型
4.enum类型
5.Annotation类型
6.以上所有类型的数组Annotation类型里面的参数该怎么设定:
第一,只能用public或默认(default)这两个访问权修饰.例如,String value();这里把方法设为defaul默认类型;
第二,参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和 String,Enum,Class,annotations等数据类型,以及这一些类型的数组.例如,String value();这里的参数成员就为String;
第三,如果只有一个参数成员,最好把参数名称设为"value",后加小括号.例:下面的例子FruitName注解就只有一个参数成员。简单的自定义注解和使用注解实例:
package annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 水果名称注解 * @author peida * */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface FruitName { String value() default ""; }
package annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 水果颜色注解 * @author peida * */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface FruitColor { /** * 颜色枚举 * @author peida * */ public enum Color{ BULE,RED,GREEN}; /** * 颜色属性 * @return */ Color fruitColor() default Color.GREEN; }
package annotation; import annotation.FruitColor.Color; public class Apple { @FruitName("Apple") private String appleName; @FruitColor(fruitColor=Color.RED) private String appleColor; public void setAppleColor(String appleColor) { this.appleColor = appleColor; } public String getAppleColor() { return appleColor; } public void setAppleName(String appleName) { this.appleName = appleName; } public String getAppleName() { return appleName; } public void displayName(){ System.out.println("水果的名字是:苹果"); } }
注解元素的默认值:
注解元素必须有确定的值,要么在定义注解的默认值中指定,要么在使用注解时指定,非基本类型的注解元素的值不可为null。因此, 使用空字符串或0作为默认值是一种常用的做法。这个约束使得处理器很难表现一个元素的存在或缺失的状态,因为每个注解的声明中,所有元素都存在,并且都具有相应的值,为了绕开这个约束,我们只能定义一些特殊的值,例如空字符串或者负数,一次表示某个元素不存在,在定义注解时,这已经成为一个习惯用法。
三、自定义注解实例
以上都是一些注解的基础知识,这里讲一下自定义注解的使用。一般,注解都是搭配反射的解析器共同工作的,然后利用反射机制查看类的注解内容。如下:
1 package testAnnotation; 2 3 import java.lang.annotation.Documented; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 7 @Documented 8 @Retention(RetentionPolicy.RUNTIME) 9 public @interface Person{ 10 String name(); 11 int age(); 12 }
package testAnnotation; 2 3 @Person(name="xingoo",age=25) 4 public class test3 { 5 public static void print(Class c){ 6 System.out.println(c.getName()); 7 8 //java.lang.Class的getAnnotation方法,如果有注解,则返回注解。否则返回null 9 Person person = (Person)c.getAnnotation(Person.class); 10 11 if(person != null){ 12 System.out.println("name:"+person.name()+" age:"+person.age()); 13 }else{ 14 System.out.println("person unknown!"); 15 } 16 } 17 public static void main(String[] args){ 18 test3.print(test3.class); 19 } 20 }
运行结果:
testAnnotation.test3 name:xingoo age:25
接下来再讲一个工作中的例子就可以收篇啦!
LoginVerify注解是用于对标注的方法在进行请求访问时进行登录判断。
package com.newsee.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 是否已登录判断 * */ @Documented @Target(ElementType.METHOD) @Inherited @Retention(RetentionPolicy.RUNTIME) public @interface LoginVerify { }
ScanningLoginVerifyAnnotation里的scanning()方法被@PostConstruct修饰,说明它在服务器加载Servlet的时候运行,并且只会被服务器执行一次。
这里再科普一下:
@PostConstruct和@PreDestroy。这两个注解被用来修饰一个非静态的void()方法 。写法有如下两种方式:
@PostConstruct
Public void someMethod() {}
或者public @PostConstruct void someMethod(){}
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct会在构造函数之后,init()方法之前执行。PreDestroy()方法在destroy()方法执行之后执行
scanning方法是在servlet加载完毕后获取所有被加载类,遍历其中的方法,如果有被LoginVerify注解修饰,则该方法名放到一个static的map中存储起来。
package com.newsee.annotation; import java.io.IOException; import java.lang.reflect.Method; import javax.annotation.PostConstruct; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.stereotype.Component; import org.springframework.util.ClassUtils; import com.newsee.constant.LoginVerifyMapping; @Component public class ScanningLoginVerifyAnnotation { private static final String PACKAGE_NAME = "com.newsee.face"; private static final String RESOURCE_PATTERN = "/**/*.class"; @PostConstruct public void scanning() throws IOException, SecurityException, ClassNotFoundException { String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(PACKAGE_NAME) + RESOURCE_PATTERN; ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); Resource[] resources = resourcePatternResolver.getResources(pattern); for (Resource resource : resources) { if (resource.isReadable()) { String className = getClassName(resource.getURL().toString()); Class cls = ScanningRequestCodeAnnotation.class.getClassLoader().loadClass((className)); for (Method method : cls.getMethods()) { LoginVerify requestCode = method.getAnnotation(LoginVerify.class); if (requestCode != null) { </span>LoginVerifyMapping.add(className + "."+ method.getName()); } } } } } private String getClassName(String resourceUrl) { String url = resourceUrl.replace("/", "."); url = url.replace("\\", "."); url = url.split("com.newsee")[1]; url = url.replace(".class", ""); return "com.newsee" + url.trim(); } }
LoginVerifyMapping就是存放被LoginVerify注解修饰的方法名的。
public class LoginVerifyMapping { private static Map<String, Boolean> faceFunctionIsNeedLoginVerify = new HashMap<String, Boolean>(); public static void add(String functionName) { faceFunctionIsNeedLoginVerify.put(functionName, Boolean.TRUE); } public static Boolean getFaceFunctionIsNeedLoginVerify(String functionName) { return faceFunctionIsNeedLoginVerify.get(functionName); } }
以下方法就是请求过来时判断请求的方法是不是在LoginVerifyMapping中,如果是,则需要进行登录校验。
private ResponseContent handleRequests(RequestContent requestContent) throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { String requestCode = requestContent.getRequest().getHead().getNWCode(); String className = RequestCodeMapping.getClassName(requestCode); String beanName = RequestCodeMapping.getBeanName(requestCode); String functionName = RequestCodeMapping.getFunctionName(requestCode); Boolean loginVerify = LoginVerifyMapping.getFaceFunctionIsNeedLoginVerify(className + "." + functionName); if (loginVerify != null && loginVerify) {//需要进行登录校验 boolean isAuthenticated = SecurityUtils.getSubject().isAuthenticated(); if (!isAuthenticated) { String exId=requestContent.getRequest().getHead().getNWExID(); SystemMobileTokenKeyServiceInter systemMobileTokenKeyServiceInter = (SystemMobileTokenKeyServiceInter) SpringContextUtil .getBean("systemMobileTokenKeyServiceInter"); SystemMobileTokenKey systemMobileTokenKey=systemMobileTokenKeyServiceInter.getByExId(exId); if(systemMobileTokenKey==null) throw new BaseException(ResponseCodeEnum.NO_LOGIN); Date keyTime = systemMobileTokenKey.getKeyTime(); if (System.currentTimeMillis() - keyTime.getTime() > 1000 * 60 * 60 * 24 * 3) throw new BaseException(ResponseCodeEnum.NO_LOGIN); } } if (className == null || beanName == null || functionName == null) throw new BaseException(ResponseCodeEnum.REQUEST_CODE_NOT_EXIST); Object object = SpringContextUtil.getBean(beanName); Class cls = Class.forName(className); Method method = cls.getMethod(functionName, RequestContent.class); Object response = method.invoke(object, requestContent); return (ResponseContent) response; } }
-
注解
2018-10-24 16:27:35Annotation 注解如同标签 对类行为的某些角度进行评价与解释 注解通过反射获取 ...可以注解到其他注解上的注解,也就是基本注解,可以用来规范注解的一些行为 @Retention --&gt; 注解的存活周期 ...Annotation
- 注解如同标签
- 对类行为的某些角度进行评价与解释
- 注解通过反射获取
注解的创建
public @interface TestAnnotation{ }
注解的使用
@TestAnnotation public class Test{ }
元注解
可以注解到其他注解上的注解,也就是基本注解,可以用来规范注解的一些行为
- @Retention --> 注解的存活周期
- RetentionPolicy.SOURCE 注解将被编译器丢弃
- RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
- RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。
- @Documented --> 能将注解保存到Javadoc中
- @Target --> 注解应用的地方
- ElementType.ANNOTATION_TYPE 可以给一个注解进行注解
- ElementType.CONSTRUCTOR 可以给构造方法进行注解
- ElementType.FIELD 可以给属性进行注解
- ElementType.LOCAL_VARIABLE 可以给局部变量进行注解
- ElementType.METHOD 可以给方法进行注解
- ElementType.PACKAGE 可以给一个包进行注解
- ElementType.PARAMETER 可以给一个方法内的参数进行注解
- ElementType.TYPE 可以给一个类型进行注解,比如类、接口、枚举
- @inherited --> 如果父类使用了此注解,那么子类可以继承此注解
@Inherited @Retention(RetentionPolicy.RUNTIME) @interface Test {} @Test public class A {} public class B extends A {}
- @Repeatable --> Java8新注解,多次应用的注解
@interface Persons { Person[] value(); } @Repeatable(Persons.class) @interface Person{ String role default ""; } @Person(role="artist") @Person(role="coder") @Person(role="PM") public class SuperMan{ }
容器注解
存放注解的容器,本身也是注解。
注解的属性
注解的属性也叫做成员变量。
- 注解只有成员变量,没有方法
- 以无参的方法来声名
- 方法名为成员名
- 返回值为成员类型
- 类型必须是 8 种基本数据类型
- 类
- 接口
- 注解
- 以上各类型的数组
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface TestAnnotation { int id(); String msg(); }
在使用注解的时候,应该为该注解的属性赋值:
@TestAnnotation(id=3,msg="hello annotation") public class Test { }
注解中属性可以有默认值,默认值需要用 default 关键值指定:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface TestAnnotation { public int id() default -1; public String msg() default "Hi"; }
因此在使用的时候无需再赋初值:
@TestAnnotation() public class Test {}
如果注解中属性名为value,那么可以省略属性名直接赋值使用
public @interface Check { String value(); } @Check("hi") int a;
内置注解
- @Deprecated
- @Override
- @SuppressWarnings
- @FunctionalInterface Java8新特性,函数式接口可以很容易转化为Lamda表达式。
注解与反射
- 首先可以通过 Class 对象的 isAnnotationPresent() 方法判断它是否应用了某个注解:
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {}
- 然后通过 getAnnotation() 方法来获取 Annotation 对象:
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {}
- 或者是 getAnnotations() 方法:
public Annotation[] getAnnotations() {}
- 如果获取到的Annotation不为null,则可以调用它的属性方法了:
@TestAnnotation() public class Test { public static void main(String[] args) { boolean hasAnnotation = Test.class.isAnnotationPresent(TestAnnotation.class); if ( hasAnnotation ) { TestAnnotation testAnnotation = Test.class.getAnnotation(TestAnnotation.class); System.out.println("id:"+testAnnotation.id()); System.out.println("msg:"+testAnnotation.msg()); } } }
程序运行结果:
id:-1 msg:
注解的作用
- 注解本身对程序运行没有直接关系
- 注解的存在主要是为编译器或APT(Annotation Processing Tool)使用,而要使用的话需要开发者手动调用方法来提取并处理Annotation的信息
总结
- 注解的作用是取决于你想要它做什么
-
@PostConstruct注解
2018-09-28 10:41:54@PostConstruct注解好多人以为是Spring提供的。其实是Java自己的注解。 Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet的时候...@PostConstruct基本:
@PostConstruct注解好多人以为是Spring提供的。其实是Java自己的注解。
Java中该注解的说明:@PostConstruct该注解被用来修饰一个非静态的void()方法。被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器执行一次。PostConstruct在构造函数之后执行,init()方法之前执行。
通常我们会是在Spring框架中使用到@PostConstruct注解 该注解的方法在整个Bean初始化中的执行顺序:
Constructor(构造方法) -> @Autowired(依赖注入) -> @PostConstruct(注释的方法)
应用:在静态方法中调用依赖注入的Bean中的方法。
package com.example.studySpringBoot.util; import com.example.studySpringBoot.service.MyMethorClassService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @Component public class MyUtils { private static MyUtils staticInstance = new MyUtils(); @Autowired private MyMethorClassService myService; @PostConstruct public void init(){ staticInstance.myService = myService; } public static Integer invokeBean(){ return staticInstance.myService.add(10,20); } }
那么Java提供的@PostConstruct注解,Spring是如何实现的呢?
需要先学习下BeanPostProcessor这个接口:
public interface BeanPostProcessor { /** * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean * initialization callbacks (like InitializingBean's {@code afterPropertiesSet} * or a custom init-method). The bean will already be populated with property values. * The returned bean instance may be a wrapper around the original. * * 任何Bean实例化,并且Bean已经populated(填充属性) 就会回调这个方法 * */ Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; /** * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean * initialization callbacks (like InitializingBean's {@code afterPropertiesSet} * or a custom init-method). The bean will already be populated with property values. * The returned bean instance may be a wrapper around the original. * * 任何Bean实例化,并且Bean已经populated(填充属性) 就会回调这个方法 * */ Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
那Spring初始化是那里回调这些方法呢?
AbstractApplicationContext.finishBeanFactoryInitialization(...); beanFactory.preInstantiateSingletons(); DefaultListableBeanFactory.getBean(beanName); AbstractBeanFactory.doGetBean(); AbstractAutowireCapableBeanFactory.createBean(....) populateBean(beanName, mbd, instanceWrapper); initializeBean(...) //调用BeanPostProcessor.postProcessBeforeInitialization()方法 applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); //调用BeanPostProcessor.postProcessBeforeInitialization()方法 applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
BeanPostProcessor有个实现类CommonAnnotationBeanPostProcessor,就是专门处理@PostConstruct @PreDestroy注解。
CommonAnnotationBeanPostProcessor的父类InitDestroyAnnotationBeanPostProcessor() InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization() InitDestroyAnnotationBeanPostProcessor.findLifecycleMetadata() // 组装生命周期元数据 InitDestroyAnnotationBeanPostProcessor.buildLifecycleMetadata() // 查找@PostConstruct注释的方法 InitDestroyAnnotationBeanPostProcessor.initAnnotationType // 查找@PreDestroy注释方法 InitDestroyAnnotationBeanPostProcessor.destroyAnnotationType // 反射调用 metadata.invokeInitMethods(bean, beanName);
-
Spring的注解@Qualifier用法
2018-06-07 16:21:26Spring的注解@Qualifier用法 在Controller中需要注入service那么我的这个server有两个实现类如何区分开这两个impl呢? 根据注入资源的注解不同实现的方式有一点小小的区别 下面上铺垫图 ...Spring的注解@Qualifier用法
- 在Controller中需要注入service那么我的这个server有两个实现类如何区分开这两个impl呢?
根据注入资源的注解不同实现的方式有一点小小的区别
下面上铺垫图
请忽略我的红线
##在Controller中使用 @Autowired注入时
Qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,添加@Qualifier注解,需要注意的是@Qualifier的参数名称为我们之前定义@Service注解的名称之一。##使用@Resource注入时
使用@resource注入时比较简单了注解自带了“name”的val就是@Service注解的名称之一。 -
秒懂,Java 注解 (Annotation)你可以这样学
2017-06-27 21:48:30文章开头先引入一处...Annotation 中文译过来就是注解、标释的意思,在 Java 中注解是一个很重要的知识点,但经常还是有点让新手不容易理解。 我个人认为,比较糟糕的技术文档主要特征之一就是:用专业名词来... -
swagger2 注解说明 ( @ApiImplicitParams )
2018-10-17 12:25:36tags="说明该类的作用,可以在UI界面上看到的注解" value="该参数没什么意义,在UI界面上也看到,所以不需要配置" @ApiOperation:用在请求的方法上,说明方法的用途、作用 value="说明方法的用途、作用" ... -
Swagger 常用注解使用详解
2018-03-16 09:18:24刚开始的时候,在controller层使用@RequestParam的时候,发现这个参数是必须要输入值的,但是我们有时候必须查询的时候允许参数为空,使用这个注解就不行了。在集成了swagger2后,找了半天的原因,发现使用@... -
SpringBoot注解梳理
2019-10-06 16:27:03一、注解(annotations)列表 二、注解(annotations)详解 三、JPA注解 四、springMVC相关注解 五、全局异常处理 一、注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan、@Configuration和@... -
SpringBoot注解最全详解(整合超详细版本)
2018-07-30 14:53:25使用注解的优势: 1.采用纯java代码,不在需要配置繁杂的xml文件 2.在配置中也可享受面向对象带来的好处 3.类型安全对重构可以提供良好的支持 4.减少复杂配置文件的同时亦能享受到springIoC容器提供的功能 ... -
@PathVariable注解使用
2018-11-18 00:46:03@PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值 语法: @PathVariable("xxx") 通过 @PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariable(“xxx“) ... -
Java中的注解以及自定义注解
2019-05-30 16:21:531、Annotation(注解) 概述 (1)、注解起到标识做用,比如Junit的@Test注解。 Junit会在运行时检查方法上是否存在此注解,如果存在,就通过反射来运行你的方法。注意标红的反射两个字,反射在注解里相当重要,写完你... -
自定义注解详细介绍
2018-07-10 16:03:211 注解的概念 1.1 注解的官方定义 首先看看官方对注解的描述: An annotation is a form of metadata, that can be added to Java source code. Classes, methods, variables, parameters and ... -
@Mapper注解的使用
2018-07-16 13:36:38@Mapper注解的的作用 1:为了把mapper这个DAO交給Spring管理 http://412887952-qq-com.iteye.com/blog/2392672 2:为了不再写mapper映射文件 https://blog.csdn.net/weixin_39666581/article/details/103899495 3:... -
Spring的@Resource注解报错
2020-03-31 00:15:19Spring的@Resource注解报错 最近在学习Spring框架的过程中,遇到Spring的@Resource注解报错的情况。 我使用的是IDEA 一开始使用@@Resource提示无法是通,在IDEA中表现为编译时出错。代码并没有任何问题,但是idea... -
深入理解Java注解类型(@Annotation)
2017-05-21 10:51:43【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) ... 出自【zejian的博客】 关联文章: 深入理解Java类型信息(Class对象)与反射机制 ...深入理解Java注解类型(@Annotation) 深入理解 -
java注解-最通俗易懂的讲解
2018-06-05 10:24:36来源:秒懂,Java 注解 (Annotation)你可以这样学 Annotation 中文译过来就是注解、标释的意思,在 Java 中注解是一个很重要的知识点,但经常还是有点让新手不容易理解。 我个人认为,比较糟糕的技术文档主要特征... -
@Valid注解是什么
2018-07-09 21:31:30用于验证注解是否符合要求,直接加在变量user之前,在变量中添加验证信息的要求,当不符合要求时就会在方法中返回message 的错误提示信息。 @RestController @RequestMapping("/user") public class ... -
Java四大元注解
2020-05-26 11:13:37目录Annotation(注解)meta-annotation(元注解)@Target注解@Retention注解@Documented注解@Inherited注解注解应用举例 Annotation(注解) 从JDK 1.5开始, Java增加了对元数据(MetaData)的支持,也就是 Annotation... -
springboot注解详解
2019-05-17 16:08:08一、注解列表 @SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。 @ComponentScan组件扫描,可自动发现和装配一些Bean,让spring Boot扫描到Configuration类并把它... -
@JsonIgnore注解
2020-03-05 18:23:12注解名称:@JsonIgnore 作用:在实体类向前台返回数据时用来忽略不想传递给前台的属性或接口。 Eg:User实体中会有字段password字段,当返回用户信息给前台的时候,当然是不希望将password值也一并返回。所以, 这个... -
浅谈Spring @Order注解的使用
2019-01-26 11:03:19注解@Order的作用是定义Spring容器加载Bean的顺序,接下来我们通过分析源码和示例测试详细的学习。 1.@Order的注解源码解读 注解类: @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.... -
自定义注解+反射 实现给注解添加功能的效果
2020-12-01 02:51:16注解我们经常会用到,或者在jdk源码中也会看到,例如: @Deprecated 以及我们在spring或者springboot中经常用到@Controller、@Service、@Repository、@Entity等注解。 是否思考过他们是怎么工作的? 下面我们使用 ... -
注解 @EnableFeignClients 工作原理
2019-01-28 17:38:37概述 在Spring cloud应用中,当我们要使用...使用注解@EnableFeignClients启用feign客户端; 示例 : @SpringBootApplication @EnableFeignClients public class TestApplication { public static void main(String[... -
@MapperScan注解
2018-05-08 14:27:32之前是,直接在Mapper类上面添加注解@Mapper,这种方式要求每一个mapper类都需要添加此注解,麻烦。通过使用@MapperScan可以指定要扫描的Mapper类的包的路径,比如:@SpringBootApplication@MapperScan("... -
@Table 注解详解
2018-11-22 13:36:07spring @Table注解 作用是:声明此对象映射到数据库的数据表,通过它可以为实体指定表(talbe) 常用的两个属性: 1、name 用来命名 当前实体类 对应的数据库 表的名字 @Table(name = "tab_user") 2、... -
Java注解
2019-08-31 23:28:04为什么要引入注解? 使用【注解】之前(甚至在使用之后),【XML】被广泛的应用于描述元数据,得到各大框架的青睐,它以松耦合的方式完成了框架中几乎所有的配置,但是随着项目越来越庞大,【XML】的内容也越来越复杂... -
精进Spring—Spring常用注解【经典总结】
2017-07-29 11:02:21Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式。 Spring注解方式减少了配置文件内容,更加便于管理,并且使用注解可以大大...
-
一天学完MySQL数据库
-
xamarin 中后台线程更新UI代码和启动一个Timer
-
Json学习总结(9)——放弃FastJson!Jackson的功能原来如此之牛
-
H81M_for_XP.zip
-
Linux GPIO使用以及debug 方法
-
shenlan-3d-reconstruction:我对shenlanxueyuan进行的3D重建课程的作业-源码
-
恒虚警检测(CFAR)处理技术
-
CAP定理与BASE定理
-
spark大数据分析与实战
-
多线程编程(6)之三种实现有返回值的多线程
-
并发学习笔记
-
白话:java从入门到实战
-
2019年中山大学677基础医学综合考研真题
-
Node.js 中 package.json 的 devdependencies、dependencies 区别
-
FastDFS 分布式文件系统部署
-
区块链应用开发实战(Go语言方向)
-
School_District_Analysis:哥伦比亚数据科学模块3-Python,Pandas和Jupyter Notebook-报告学区内学校的进度和绩效-源码
-
JS如何判断一个对象是否为空、是否有某个属性
-
MHA 高可用 MySQL 架构与 Altas 读写分离
-
基于DMSP和NPP夜间灯光数据GDP空间化教程.mp4