一时兴起,这两天看了看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那样,要做的东西还是有点工作量的。