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);
}
}