⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 injectinterceptor.java

📁 精通EJB3源码
💻 JAVA
字号:
package com.foshanshop.ejb3.impl;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import javax.annotation.PostConstruct;
import javax.ejb.EJBException;
import javax.interceptor.InvocationContext;

import com.foshanshop.ejb3.Person;
import com.foshanshop.ejb3.PersonInject;

public class InjectInterceptor {
	   @PostConstruct
	   public void jndiInject(InvocationContext invocation) {
	      Object target = invocation.getTarget( );
	      Field[] fields = target.getClass().getDeclaredFields( );
	      Method[] methods = target.getClass().getDeclaredMethods( );

	      try {
	         for (Method method : methods) {
	        	PersonInject inject = method.getAnnotation(PersonInject.class);
	            if (inject != null) {
	               method.setAccessible(true);
	               method.invoke(target, new Person(inject.name(), inject.age()));
	            }
	         }
	         for (Field field : fields) {
	        	PersonInject inject = field.getAnnotation(PersonInject.class);
	            if (inject != null) {
	               field.setAccessible(true);
	               field.set(target, new Person(inject.name(), inject.age()));
	            }
	         }
	         invocation.proceed( );
	      } catch (Exception ex) {
	         throw new EJBException("执行@PersonInject失败", ex);
	      }
	   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -