自定义注解模拟spring注入

controller类

package com.hwj.anno.autowired.reqMapp;

import com.hwj.anno.autowired.Inject;
import com.hwj.anno.autowired.UserService;

@ReqMapping(method = ReqMethod.POST, val = "类")
public class UserControllerH {
	@ReqValue(value1 = "张三")
	public String userName;

	@ReqValue(value2 = "密码")
	public String pswd;
	
	@Inject
	UserService userService;

	@ReqMapping(method = ReqMethod.GET,val="/test01")
	public void get() {
         System.out.println("get方法");
	}

	@ReqMapping(method = ReqMethod.POST)
	public void post() {
        System.out.println("post方法");
	}

	@ReqMapping(val = { "a", "b" })
	public void other() {
          System.out.println("other方法");
	}
}

自定义注解ReqValue

package com.hwj.anno.autowired.reqMapp;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ReqValue {
	String value1() default "";
	String value2() default "";
}

自定义注解ReqMapping

package com.hwj.anno.autowired.reqMapp;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ReqMapping {
	ReqMethod [] method() default {};

    String[] val() default "";
}

自定义注解Inject相当于@Autowired

package com.hwj.anno.autowired;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public  @interface Inject {

}

枚举ReqMethod

package com.hwj.anno.autowired.reqMapp;

public enum ReqMethod {
	GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE
}

UserService类

package com.hwj.anno.autowired;

public class UserService {
	    @Inject
	    private UserDao userDao;
	 
	    @Inject
	    private ClassDao classDao;
	 
	    public void save() {
	        userDao.save();
	       
	    }
	 
	    public UserService() {
	    }

}

UserDao类

package com.hwj.anno.autowired;

public class UserDao {
	 public UserDao() {
	    }
	 
	    public void save() {
	        System.out.println("UserDao...");
	      
	    }

}

SpringIOC容器

package com.hwj.anno.autowired.reqMapp;

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

import com.hwj.anno.autowired.Inject;

public class SpringIOC {
	public static <Q> Q getBean(Class<Q> clazz) throws Exception {
		Q result = null;
		result = clazz.newInstance();
		// 得到所有属性信息
		Field[] fields = clazz.getDeclaredFields();
		// 得到所有方法信息
		Method[] methods = clazz.getMethods();
		//方法注解处理
		for(Method method:methods) {
			 if(method.isAnnotationPresent(ReqMapping.class)) {
				 //方法注解的值
				 ReqMapping reqMapping=method.getAnnotation(ReqMapping.class);
				 ReqMethod[] reqMethod=reqMapping.method();
				 for(ReqMethod reqMethod2:reqMethod) {
					 System.out.println("请求方式:"+reqMethod2.toString());
				 }
				 String[] value=reqMapping.val();
				 for(String s:value) {
					 System.out.println("请求路径:"+s);
				 }
			 }
		 }
		
		for (Field field : fields) {
			Inject inject = field.getAnnotation(Inject.class);
			ReqValue reqValue=field.getAnnotation(ReqValue.class);
			// 该属性有inject注解 需要注入独享
			if (inject != null) {
				// 得到 需要注入属性的对象
				Object object = getBean(field.getType());
				// 如果字段是私有的
				if (!field.isAccessible()) {
					field.setAccessible(true);
				}
				field.set(result, object);
			}else if(reqValue!=null) {
				//该属性是reqValue注解 
				if (!field.isAccessible()) {
					field.setAccessible(true);
				}
				//注入属性值
				field.set(result, reqValue.value1());
			}
		}
		return result;
	}
}

测试类

package com.hwj.anno.autowired.reqMapp;

public class TestSpringIOC {
	public static void main(String[] args) throws Exception {
		UserControllerH userControllerH=SpringIOC.getBean(UserControllerH.class);
		userControllerH.get();
		userControllerH.userService.save();
		System.out.println(userControllerH.userName);
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值