摘要:本文主要向大家介绍了MFC编程实例:从零实现MVC框架之依赖注入IOC(7),通过具体的内容向大家展示,希望对大家学习MFC编程实例有所帮助。
本文主要向大家介绍了MFC编程实例:从零实现MVC框架之依赖注入IOC(7),通过具体的内容向大家展示,希望对大家学习MFC编程实例有所帮助。
我们的目的是在学习MVC的实现方式,而不是真的做一个MVC框架,所以就一切从简,所以这里我们只做Controller中的依赖注入。
也就是说只有Controller中才可以使用我们下面创建的Inject注解。如果再其他的地方使用是注入不成功的哦。
注解
[java] view plain copy
1. package com.hc.annotation;
2.
3. import java.lang.annotation.ElementType;
4. import java.lang.annotation.Retention;
5. import java.lang.annotation.RetentionPolicy;
6. import java.lang.annotation.Target;
7. /**
8. *
9. * @author chuer
10. * @date 2014-7-16 下午4:34:14
11. * @version V1.0
12. */
13. @Retention(RetentionPolicy.RUNTIME)
14. @Target({ElementType.FIELD})
15. public @interface Inject {
16.
17. public String className();
18. }
修改AbstractHcAction
[java] view plain copy
1. Class<?> cls = controllerClassInfo.getCls();
2. Object newInstance = cls.getConstructor(new Class[]{}).newInstance(new Object[]{});
3.
4. //依赖注入
5. Field[] declaredFields = cls.getDeclaredFields();
6. for(Field field : declaredFields){
7. if(field.isAnnotationPresent(Inject.class)){
8. Inject inject = field.getAnnotation(Inject.class);
9. String setMethod = "set"+field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
10. Class<?> forName = Class.forName(inject.className());
11. Object newInstance2 = forName.getConstructor(new Class[]{}).newInstance(new Object[]{});
12. Object newInstance3 = TransactionProxyCache.cache.get(forName);
13. if(newInstance3 != null){
14. newInstance2 = newInstance3;
15. }
16. Method method = cls.getMethod(setMethod, newInstance2.getClass().getInterfaces());
17. method.invoke(newInstance, new Object[]{newInstance2});
18. }
19. }
20.
21. //调用controller方法
22. Method method = controllerClassInfo.getMethodMap().get(methodParam);
修改UserController
[java] view plain copy
1. @Inject(className="com.hc.sample.service.UserServiceImpl")
2. private UserService userService;
以上就介绍了VC/MFC的学习,希望对VC/MFC有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言VC/MFC频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号