java将类上成员字段自动注入其对象

源码如下:

import java.lang.annotation.*;
/**
 * @date: 2022/6/16 15:52
 * @desc: 装配注解
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZyAutowired {
    String value() default "";
}
import java.lang.annotation.*;
/**
 * @date: 2022/6/14 22:11
 * @desc: 控制层注解
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZyController {
    String value() default "";
}
import java.lang.annotation.*;
/**
 * @date: 2022/6/15 20:36
 * @desc: 请求映射注解
 */
@Target({ElementType.TYPE, ElementType.METHOD}) // 作用在类型或方法上
@Retention(RetentionPolicy.RUNTIME) // 注解信息在运行时出现
@Documented
public @interface ZyRequestMapping {
    String value() default "";
}
import java.lang.annotation.*;
/**
 * @date: 2022/6/16 15:42
 * @desc: 服务层注解
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ZyService {
    String value() default "";
}
/**
 * @date: 2022/6/14 22:09
 * @desc: 控制类
 */
@ZyController
@ZyRequestMapping(value = "testController")
public class TestController {
    @ZyAutowired(value = "testService")
    private TestService testService;
    public void testServiceIoc() {
        testService.firstAttribute = "测试服务类属性1";
        System.out.println("输出:" + testService.firstAttribute);
    }
}
/**
 * @date: 2022/6/16 15:41
 * @desc: 测试注解服务类
 */
@ZyService(value = "testService")
public class TestService {
    public String firstAttribute;
}
        // 测试代码
        Map mapBeans = new HashMap();
        mapBeans.put("testController", new TestController());
        mapBeans.put("testService", new TestService());
        CreateClsObj createClsObj = new CreateClsObj();
        createClsObj.cckage(mapBeans);
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章