利用反射实现简单的Spring注解注入实例

一时兴起,这两天看了看Spring的源代码,就想写一个简单的Spring利用注解注入!!Spring的注解注入方式给了我们很方便的好处!大家应该都了解Spring的注入原理,在这里写下一个非常简单的使用注解来注入的实例,做个小小的笔记!

要使用注解,那就绝对和反射离不开。摘抄一段

Reflection是Java 程序开发语言的特征之一,它允许运行中的 Java 程序对自身进行检查,或者说”自审”,并能直接操作程序的内部属性。例如,使用它能获得 Java 类中各成员的名称并显示出来。 Java 的这一能力在实际应用中也许用得不是很多,但是在其它的程序设计语言中根本就不存在这一特性。例如,Pascal、C 或者 C++ 中就没有办法在程序中获得函数定义相关的信息

有了反射才能让我们的注解使用得更加的灵活

先来看一个判断该类是否可以用我们的注解的判断注解,就好像类似SpringMVC的

package org.xie.annotation.device;

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;
/**
 * 用来判断该类是否可以用来使用注入
 * @author 骚年、编程去
 *
 */
@Target(ElementType.TYPE)
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface IsAnnotation {


}

然后就是我们要用到的注解了

package org.xie.annotation.device;

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;
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SetterAnnotation {
    //自定义注解
    public Class nation() ;  
}

这个注解里面返回了一个Class对象,在这里我们并没有像spring那样通过自定义的名字来注入,而是通过该类的类型直接注入,都说了。。。。是一个小例子
然后就是我们要用到的接口了

package org.xie.Interface;

public interface IUser {



    void login();
}

以及两个实现类
首先是Chinese

package org.xie.Interface.impl;

import org.xie.Interface.IUser;

public class ChineseUserImpl implements IUser{



    public void login() {

        System.out.println("注入中文");


    };
}

然后是English

package org.xie.Interface.impl;

import org.xie.Interface.IUser;

public class EnglishUserImpl implements IUser {

    @Override
    public void login() {
        // TODO Auto-generated method stub
        System.out.println("English");

    }

}

接着我们来看看我们要注入的类的信息

package org.xie.relfect.first;

import org.xie.Interface.IUser;
import org.xie.Interface.impl.ChineseUserImpl;
import org.xie.Interface.impl.EnglishUserImpl;
import org.xie.annotation.device.AnnotationTest;
import org.xie.annotation.device.IsAnnotation;
import org.xie.annotation.device.SetterAnnotation;

@IsAnnotation
public class SetterBean {

    private IUser userdao;
    @SetterAnnotation(nation=EnglishUserImpl.class)
    public void setUserdao(IUser userdao) {
        this.userdao = userdao;
    }

    public void login_Test(){
        userdao.login();    
    }
}

接着是我们的注解解析类

package org.xie.relfect.first;

import java.lang.reflect.Method;
import java.nio.channels.SeekableByteChannel;

import org.xie.Interface.IUser;
import org.xie.Interface.impl.ChineseUserImpl;
import org.xie.annotation.device.IsAnnotation;
import org.xie.annotation.device.SetterAnnotation;

/**
 * 类似spring容器
 * @author Administrator
 *
 */
public class SpringWine {

    public static SetterBean getBean(){

        SetterBean bean=new SetterBean();
        boolean isAnnotation=SetterBean.class.isAnnotationPresent(IsAnnotation.class);

        if(isAnnotation){

            Method[] method=SetterBean.class.getDeclaredMethods();

            for (Method method2 : method) {
                if(method2.isAnnotationPresent(SetterAnnotation.class)){
                    SetterAnnotation setterAnnotation=method2.getAnnotation(SetterAnnotation.class);
                     System.out.println("AnnotationTest(field=" + method2.getName()  
                                + ",nation=" + setterAnnotation.nation() + ")");                 
                    try {
                    Class<!--?--> clazz=setterAnnotation.nation();  
                    IUser iuser=(IUser) clazz.newInstance();    
                    bean.setUserdao(iuser);
                        //return bean;
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                        return null;
                    }                
                }            
           }        
        }    
          return bean;      
    }   
}

测试类

package org.xie.relfect.first;

import javax.swing.Spring;

public class Test {

    public static void main(String[] args) {

        SetterBean bean=SpringWine.getBean();

        bean.login_Test();

    }

}

通过改变不同的注解,可以得到不同的结果,当然,这个小例子要优化的东西还有很多很多。。。。如果要做成Spring那样,要做的东西还是有点工作量的。

转自:利用反射实现简单的Spring注解注入实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值