spring全部注解,只要你想学,都可以获得到

最近在研究spring spring中的注解太多了,没有系统的学习方案。只能遇到一个学一个,在这里便写了一段代码,来获取这些注解。

首先参见

引用参考 http://www.cnblogs.com/davidwang456/p/4432410.html

这位仁兄总结的也挺多。

不过有强迫症的兄弟可以继续往下看。

1spring-context
org.springframework.scheduling.annotation.Schedules
org.springframework.scheduling.annotation.EnableScheduling
org.springframework.scheduling.annotation.Async
org.springframework.scheduling.annotation.Scheduled
org.springframework.scheduling.annotation.EnableAsync
org.springframework.context.annotation.Lazy
org.springframework.context.annotation.ImportResource
org.springframework.context.annotation.EnableLoadTimeWeaving
org.springframework.context.annotation.PropertySource
org.springframework.context.annotation.Import
org.springframework.context.annotation.Scope
org.springframework.context.annotation.Configuration
org.springframework.context.annotation.Description
org.springframework.context.annotation.Bean
org.springframework.context.annotation.DependsOn
org.springframework.context.annotation.Role
org.springframework.context.annotation.PropertySources
org.springframework.context.annotation.ComponentScans
org.springframework.context.annotation.Profile
org.springframework.context.annotation.Primary
org.springframework.context.annotation.EnableAspectJAutoProxy
org.springframework.context.annotation.Conditional
org.springframework.context.annotation.ComponentScan
org.springframework.context.annotation.ComponentScan$Filter
org.springframework.context.annotation.EnableMBeanExport
org.springframework.context.event.EventListener
org.springframework.validation.annotation.Validated
org.springframework.jmx.export.annotation.ManagedResource
org.springframework.jmx.export.annotation.ManagedNotification
org.springframework.jmx.export.annotation.ManagedOperationParameter
org.springframework.jmx.export.annotation.ManagedOperation
org.springframework.jmx.export.annotation.ManagedNotifications
org.springframework.jmx.export.annotation.ManagedAttribute
org.springframework.jmx.export.annotation.ManagedMetric
org.springframework.jmx.export.annotation.ManagedOperationParameters
org.springframework.cache.annotation.Caching
org.springframework.cache.annotation.CacheEvict
org.springframework.cache.annotation.EnableCaching
org.springframework.cache.annotation.Cacheable
org.springframework.cache.annotation.CachePut
org.springframework.cache.annotation.CacheConfig
org.springframework.format.annotation.NumberFormat
org.springframework.format.annotation.DateTimeFormat
org.springframework.stereotype.Controller
org.springframework.stereotype.Repository
org.springframework.stereotype.Service
org.springframework.stereotype.Component

2spring-web
org.springframework.web.bind.annotation.CrossOrigin
org.springframework.web.bind.annotation.RequestHeader
org.springframework.web.bind.annotation.RequestParam
org.springframework.web.bind.annotation.ResponseStatus
org.springframework.web.bind.annotation.MatrixVariable
org.springframework.web.bind.annotation.ModelAttribute
org.springframework.web.bind.annotation.ExceptionHandler
org.springframework.web.bind.annotation.CookieValue
org.springframework.web.bind.annotation.PostMapping
org.springframework.web.bind.annotation.RequestPart
org.springframework.web.bind.annotation.RequestMapping
org.springframework.web.bind.annotation.GetMapping
org.springframework.web.bind.annotation.RequestBody
org.springframework.web.bind.annotation.InitBinder
org.springframework.web.bind.annotation.PatchMapping
org.springframework.web.bind.annotation.Mapping
org.springframework.web.bind.annotation.SessionAttribute
org.springframework.web.bind.annotation.RestController
org.springframework.web.bind.annotation.ControllerAdvice
org.springframework.web.bind.annotation.SessionAttributes
org.springframework.web.bind.annotation.ResponseBody
org.springframework.web.bind.annotation.RestControllerAdvice
org.springframework.web.bind.annotation.RequestAttribute
org.springframework.web.bind.annotation.DeleteMapping
org.springframework.web.bind.annotation.PutMapping
org.springframework.web.bind.annotation.PathVariable
org.springframework.web.context.annotation.SessionScope
org.springframework.web.context.annotation.RequestScope
org.springframework.web.context.annotation.ApplicationScope

3spring-webmvc
org.springframework.web.servlet.config.annotation.EnableWebMvc

  

这里列举了3个jar中的注解,jar中的全部注解,妈妈再也不担心我学习有遗漏了。

获取方法代码如下

package com.co.example;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import com.google.common.collect.Lists;

public class PackageTest {

    /**
     * 从jar获取某包下所有类
     *
     * @param jarPath
     *                        jar文件路径
     * @param childPackage
     *                        是否遍历子包
     * @return 类的完整名称
     */
    private static List<String> getClassNameByJar(String jarPath, boolean childPackage) {
        List<String> myClassName = new ArrayList<String>();
        String[] jarInfo = jarPath.split("!");
        String jarFilePath = jarInfo[0].substring(jarInfo[0].indexOf("/"));
        String packagePath = jarInfo[1].substring(1);
        try (JarFile jarFile = new JarFile(jarFilePath);) {
            Enumeration<JarEntry> entrys = jarFile.entries();
            while (entrys.hasMoreElements()) {
                JarEntry jarEntry = entrys.nextElement();
                String entryName = jarEntry.getName();
                if (entryName.endsWith(".class")) {
                    if (childPackage) {
                        if (entryName.startsWith(packagePath)) {
                            entryName = entryName.replace("/", ".").substring(0, entryName.lastIndexOf("."));
                            myClassName.add(entryName);
                        }
                    } else {
                        int index = entryName.lastIndexOf("/");
                        String myPackagePath;
                        if (index != -1) {
                            myPackagePath = entryName.substring(0, index);
                        } else {
                            myPackagePath = entryName;
                        }
                        if (myPackagePath.equals(packagePath)) {
                            entryName = entryName.replace("/", ".").substring(0, entryName.lastIndexOf("."));
                            myClassName.add(entryName);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return myClassName;
    }

    /**
     * 从所有jar中搜索该包,并获取该包下所有类
     *
     * @param urls
     *                        URL集合
     * @param packagePath
     *                        包路径
     * @param childPackage
     *                        是否遍历子包
     * @return 类的完整名称
     */
    private static List<String> getClassNameByJars(String[] urls, String packagePath, boolean childPackage) {
        List<String> myClassName = new ArrayList<String>();
        if (urls != null) {
            for (int i = 0; i < urls.length; i++) {
                String urlPath = urls[i];
                // 不必搜索classes文件夹
                if (urlPath.endsWith("classes/")) {
                    continue;
                }
                String jarPath = urlPath + "!" + packagePath;
                myClassName.addAll(getClassNameByJar(jarPath, childPackage));
            }
        }
        return myClassName;
    }
    public static void main(String[] args) {
        List<String> annotationsList = Lists.newArrayList();
        String BasePath="D:/repository/maven/org/springframework";
        List<String> jars = Lists.newArrayList();
    //    jars.add(BasePath+"/spring-context/4.3.8.RELEASE/spring-context-4.3.8.RELEASE.jar");
    //    jars.add(BasePath+"/spring-web/4.3.8.RELEASE/spring-web-4.3.8.RELEASE.jar");
        jars.add(BasePath+"/spring-webmvc/4.3.8.RELEASE/spring-webmvc-4.3.8.RELEASE.jar");
        String[] strArr = new String[jars.size()];
        jars.toArray(strArr);

      String packagePath=" org"; //此处有个空格
      List<String> className = getClassNameByJars(strArr, packagePath, true);

for (String cn : className) {
            try {
                Class<?> onwClass = Class.forName(cn);
                if(onwClass !=null){

                    boolean annotationFlg = onwClass.isAnnotation();
                    if(annotationFlg){
                        annotationsList.add(cn);
                    }
                }
            } catch (ClassNotFoundException e) {
                continue;
            }catch(NoClassDefFoundError f){
                continue;
            }
        }

        for (String str : annotationsList){
            System.out.println(str);
        }

    }

}

你想获得更多jar中的注解:

 1.修改 一下main方法中的BasePath,也就是本地jar的位置即可。

2.修改 一下main方法中的packagePath, org 或com

引用参考

获取所有类名的部分代码参考 http://blog.csdn.net/wangpeng047/article/details/8206427

时间: 2024-08-27 10:26:42

spring全部注解,只要你想学,都可以获得到的相关文章

spring(6)--注解式控制器

6.1.注解式控制器简介 一.Spring2.5之前,我们都是通过实现Controller接口或其实现来定义我们的处理器类.已经@Deprecated.   二.Spring2.5引入注解式处理器支持,通过@Controller 和 @RequestMapping注解定义我们的处理器类. 并且提供了一组强大的注解: 需要通过处理器映射DefaultAnnotationHandlerMapping和处理器适配器AnnotationMethodHandlerAdapter来开启支持@Controll

使用轻量级Spring @Scheduled注解执行定时任务

WEB项目中需要加入一个定时执行任务,可以使用Quartz来实现,由于项目就一个定时任务,所以想简单点,不用去配置那些Quartz的配置文件,所以就采用了Spring @Scheduled注解来实现了定时任务.在这里做个备注. spring配置文件  xmlns中加入一段: xmlns:task="http://www.springframework.org/schema/task" 然后xsi:schemaLocation多加下面的内容: http://www.springframe

Spring 使用注解方式进行事务管理

使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation

Spring常用注解总结

传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件,那么.xml文件又会非常多.总之这将导致配置文件的可读性与可维护性变得很低.2.在开发中在.java文件和.xml文件之间不断切换,是一件麻烦的事,同时这种思维上的不连贯也会降低开发的效率.为了解决这两个问题,Spring引入了注解,通过"@XXX"的方式,让注解与Java Bean紧密

【转】Spring 使用注解方式进行事务管理

Spring 使用注解方式进行事务管理 原文链接 http://www.cnblogs.com/younggun/archive/2013/07/16/3193800.html#top 使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-i

Spring事务注解

使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间 <beans xmlns="http://www.springframework.org/schema/beans"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:tx="http://www.springframework.org/schema/tx"  xsi:schemaLoca

spring(7)--注解式控制器的数据验证、类型转换及格式化

7.1.简介 在编写可视化界面项目时,我们通常需要对数据进行类型转换.验证及格式化. 一.在Spring3之前,我们使用如下架构进行类型转换.验证及格式化: 流程: ①:类型转换:首先调用PropertyEditor的setAsText(String),内部根据需要调用setValue(Object)方法进行设置转换后的值: ②:数据验证:需要显示调用Spring的Validator接口实现进行数据验证: ③:格式化显示:需要调用PropertyEditor的getText进行格式化显示. 使用

spring mvc 注解@Controller @RequestMapping @Resource的详细例子

现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过要想灵活运用Spring MVC来应对大多数的Web开发,就必须要掌握它的配置及原理. 一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.jar.comm

spring security 注解@EnableGlobalMethodSecurity详解

 1.Spring Security默认是禁用注解的,要想开启注解,需要在继承WebSecurityConfigurerAdapter的类上加@EnableGlobalMethodSecurity注解,来判断用户对某个控制层的方法是否具有访问权限 @Configuration@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled = true)public class OAuth2SecurityConfiguration ext

Spring使用注解方式就行事务管理

使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation