当前位置:   article > 正文

java自定义注解实例_Java 自定义注解与注解解析实例

自定义注解 getbeanswithannotation

package mydefineComponent;

import org.springframework.beans.factory.FactoryBean;

import org.springframework.beans.factory.InitializingBean;

import org.springframework.cglib.core.SpringNamingPolicy;

import org.springframework.cglib.proxy.Enhancer;

public class FactoryBeanTest implements InitializingBean, FactoryBean {

private String innerClassName;

public void setInnerClassName(String innerClassName) {

this.innerClassName = innerClassName;

}

@Override

public void afterPropertiesSet() throws Exception {

// TODO Auto-generated method stub

}

@Override

public T getObject() throws Exception {

Class innerClass = Class.forName(innerClassName);

if (innerClass.isInterface()) {

return (T) InterfaceProxy.newInstance(innerClass);

} else {

Enhancer enhancer = new Enhancer();

enhancer.setSuperclass(innerClass);

enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);

enhancer.setCallback(new MethodInterceptorImpl());

return (T) enhancer.create();

}

}

@Override

public Class> getObjectType() {

try {

return Class.forName(innerClassName);

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

return null;

}

@Override

public boolean isSingleton() {

// TODO Auto-generated method stub

return true;

}

}

package mydefineComponent;

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

import java.lang.reflect.Proxy;

public class InterfaceProxy implements InvocationHandler {

@Override

public Object invoke(Object proxy, Method method, Object[] args)

throws Throwable {

System.out.println("ObjectProxy execute:" + method.getName());

return method.invoke(proxy, args);

}

public static T newInstance(Class innerInterface) {

ClassLoader classLoader = innerInterface.getClassLoader();

Class[] interfaces = new Class[] { innerInterface };

InterfaceProxy proxy = new InterfaceProxy();

return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy);

}

}

package mydefineComponent;

import java.lang.reflect.Method;

import org.springframework.cglib.proxy.MethodInterceptor;

import org.springframework.cglib.proxy.MethodProxy;

public class MethodInterceptorImpl implements MethodInterceptor{

@Override

public Object intercept(Object o, Method method, Object[] objects,

MethodProxy methodProxy) throws Throwable {

System.out.println("MethodInterceptorImpl:" + method.getName());

return methodProxy.invokeSuper(o, objects);

}

}

测试代码:

package mydefineComponent;

@MyDefineComponent

public class ScanClass1 {

public void print() {

System.out.print("scanclass1");

}

}

package mydefineComponent;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MyDefineComponentTest {

public static void main(String[] args) {

AnnotationConfigApplicationContext acc = new AnnotationConfigApplicationContext("mydefineComponent");

ScanClass1 scanClass = acc.getBean(ScanClass1.class);

scanClass.print();

}

}

输出:

MethodInterceptorImpl:print

scanclass1

三、自定义的注解 ,可通过Spring快速的获取所有使用该注解的类或方法或属性,以及注解内的值。

自定义一个注解:

package com.my.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;

@Target({ ElementType.TYPE, ElementType.METHOD }) //可以用在方法或者类上面

@Retention(RetentionPolicy.RUNTIME)

@Documented

public @interface Fooish {

String[] tags() default { "all" };

}

自定义注解的解析功能

package com.my.annotation;

import java.lang.reflect.Method;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Map;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.InitializingBean;

import org.springframework.context.ApplicationContext;

import org.springframework.context.ApplicationContextAware;

import org.springframework.stereotype.Component;

@Component

public class MyFooishHandler implements ApplicationContextAware, InitializingBean{

private ApplicationContext applicationContext;

private List allFooish = new ArrayList<>();

@Override

public void afterPropertiesSet() throws Exception {

scanFooishClass();

scanFooishMethod();

System.out.println(allFooish);

}

/**

* 查找 用 Fooish 注解的 方法

*/

private void scanFooishMethod() throws Exception{

final Map permissionMap = applicationContext.getBeansWithAnnotation(Fooish.class);

System.out.println("this is permissionMap" + permissionMap.toString());

for (final Object permissionObject : permissionMap.values()) {

final Class extends Object> permissionClass = permissionObject.getClass();

final Fooish annotation = permissionClass.getAnnotation(Fooish.class);

if(annotation != null) {

allFooish.addAll(Arrays.asList(annotation.tags()));

}

}

}

private void scanFooishClass() throws Exception{

final Map controllerMap = applicationContext.getBeansWithAnnotation(Fooish.class);

for (final Object controllerObject : controllerMap.values()) {

final Class extends Object> controllerClass = controllerObject.getClass();

for (Method method : controllerClass.getDeclaredMethods()) {

Fooish fooish = method.getAnnotation(Fooish.class);

if (fooish != null) {

allFooish.addAll(Arrays.asList(fooish.tags()));

}

}

}

}

@Override

public void setApplicationContext(ApplicationContext applicationContext)

throws BeansException {

this.applicationContext = applicationContext;

}

}

测试:

package com.my.controller;

import java.io.File;

import java.io.IOException;

import java.util.List;

import javax.annotation.Resource;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

import com.my.annotation.Fooish;

import com.my.model.Student;

import com.my.service.FirstPageService;

import com.my.service.QRCodeUtil;

@RestController

@Fooish(tags={"this_is_class"})

public class FirstPageController {

@Value(value = "${erweima.location:D:/Workspaces/MyEclipse 2015/entrance/src/main/resources/erweima.png}")

private String imgPath;

@Resource

private FirstPageService firstPageService;

@RequestMapping(value = "/", method = RequestMethod.GET)

@Fooish(tags={"this_is_method"})

String home() {

return firstPageService.getString();

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/240259
推荐阅读
相关标签
  

闽ICP备14008679号