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

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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值