日期:2014-05-19 浏览次数:20918 次
package sh.pl;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import javax.ejb.EJB;
public class TestEJBContainer {
    public static void injectEJB(Object obj) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
        if (obj == null) return;
        Field fields[] = obj.getClass().getDeclaredFields();
        for (Field f : fields) {
            Annotation a = f.getAnnotation(EJB.class);
            if (a != null) {
                //注入
                f.setAccessible(true);
                f.set(obj, f.getType().newInstance());
                f.setAccessible(false);
            }
        }
        
    }
    
    public static void main(String[] args) {
        Demo demo = new Demo("p");
        try {
            injectEJB(demo);
            System.out.println(demo.getMyCl().toString());
        } catch (Exception e) {
            e.printStackTrace();
        } 
        
    }
}
------解决方案--------------------
不可以。
从spring容器中拿出的对象才可以。