当前位置: 首页 > news >正文

上海网站建设方案咨询网络服务中心

上海网站建设方案咨询,网络服务中心,企业门户的四个特点,免费b站推广网站不用下载😀前言 自己实现 SpringMVC 底层机制 系列之-实现任务阶段 5- 完成 Spring 容器对象的自动装配 -Autowried 🏠个人主页:尘觉主页 🧑个人简介:大家好,我是尘觉,希望我的文章可以帮助到大家&…

😀前言
自己实现 SpringMVC 底层机制 系列之-实现任务阶段 5- 完成 Spring 容器对象的自动装配 -@Autowried

🏠个人主页:尘觉主页
在这里插入图片描述

🧑个人简介:大家好,我是尘觉,希望我的文章可以帮助到大家,您的满意是我的动力😉😉

在csdn获奖荣誉: 🏆csdn城市之星2名
⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ 💓Java全栈群星计划top前5
⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ ⁣⁣⁣⁣ 🤗 端午大礼包获得者

💕欢迎大家:这里是CSDN,我总结知识的地方,欢迎来到我的博客,感谢大家的观看🥰
如果文章有什么需要改进的地方还请大佬不吝赐教 先在次感谢啦😊

文章目录

  • 😃实现任务阶段 5- 完成 Spring 容器对象的自动装配 -@Autowried
    • 😉分析示意图
      • 代码实现
        • 创建自定义注解AutoWired
        • 修改MonsterController
        • 修改WyxWebApplicationContext
        • executeAutoWired();方法调用
        • 启动 Tomcat, 完成测试
    • 😄总结

😃实现任务阶段 5- 完成 Spring 容器对象的自动装配 -@Autowried

说明: 完成 Spring 容器中对象的注入/自动装配

😉分析示意图

img

- 浏览器输入 http://localhost:8080/monster/list, 返回列表信息

img

● 代码实现, 说明,整个实现思路,就是参考 SpringMVC 规范

代码实现

创建自定义注解AutoWired

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AutoWired {String value() default "";
}

修改MonsterController

public class MonsterController {//@AutoWired表示要完成属性的装配.@AutoWiredprivate MonsterService monsterService;//编写方法,可以列出妖怪列表//springmvc 是支持原生的servlet api, 为了看到底层机制//这里我们设计两个参数@RequestMapping(value = "/monster/list")public void listMonster(HttpServletRequest request,HttpServletResponse response) {//设置编码和返回类型response.setContentType("text/html;charset=utf-8");StringBuilder content = new StringBuilder("<h1>妖怪列表信息</h1>");//调用monsterServiceList<Monster> monsters = monsterService.listMonster();content.append("<table border='1px' width='500px' style='border-collapse:collapse'>");for (Monster monster : monsters) {content.append("<tr><td>" + monster.getId()+ "</td><td>" + monster.getName() + "</td><td>"+ monster.getSkill() + "</td><td>"+ monster.getAge() + "</td></tr>");}content.append("</table>");//获取writer返回信息try {PrintWriter printWriter = response.getWriter();printWriter.write(content.toString());} catch (IOException e) {e.printStackTrace();}}

修改WyxWebApplicationContext

public class WyxWebApplicationContext {//定义属性classFullPathList, 保存扫描包/子包的类的全路径private List<String> classFullPathList =new ArrayList<>();//定义属性ioc, 存放反射生成的Bean对象 /Controller/Servicepublic ConcurrentHashMap<String, Object> ioc =new ConcurrentHashMap<>();//无参构造器public WyxWebApplicationContext() {}private String configLocation;//属性,表示spring容器配置文件public WyxWebApplicationContext(String configLocation) {this.configLocation = configLocation;}//编写方法,完成自己的spring容器的初始化public void init() {//这里是写的固定的spring容器配置文件.?=>做活//String basePackage = XMLParser.getBasePackage("wyxspringmvc.xml");String basePackage =XMLParser.getBasePackage(configLocation.split(":")[1]);//这时basePackage => com.hspedu.controller,com.wyxdu.serviceString[] basePackages = basePackage.split(",");//遍历basePackages, 进行扫描if (basePackages.length > 0) {for (String pack : basePackages) {scanPackage(pack);//扫描}}System.out.println("扫描后的= classFullPathList=" + classFullPathList);//将扫描到的类, 反射到ico容器executeInstance();System.out.println("扫描后的 ioc容器= " + ioc);//完成注入的bean对象,的属性的装配executeAutoWired();System.out.println("装配后 ioc容器= " + ioc);}
}

executeAutoWired();方法调用

    public void executeAutoWired() {//判断ioc有没有要装配的对象if (ioc.isEmpty()) {return; //你也可以抛出异常 throw new RuntimeException("ioc 容器没有bean对象")}//遍历ioc容器中的所有注入的bean对象, 然后获取到bean的所有字段/属性,判断是否需要//装配/*** entry => <String,Object > String 就是你注入对象时名称 Object就是bean对象*/for (Map.Entry<String, Object> entry : ioc.entrySet()) {//String key = entry.getKey();Object bean = entry.getValue();//得到bean的所有字段/属性Field[] declaredFields = bean.getClass().getDeclaredFields();for (Field declaredField : declaredFields) {//判断当前这个字段,是否有@AutoWiredif (declaredField.isAnnotationPresent(AutoWired.class)) {//有@AutoWired//的当前这个字段的@AutoWiredAutoWired autoWiredAnnotation = declaredField.getAnnotation(AutoWired.class);String beanName = autoWiredAnnotation.value();if ("".equals(beanName)) {//如果没有设置value,按照默认规则//即得到字段类型的名称的首字母小写,作为名字来进行装配Class<?> type = declaredField.getType();beanName = type.getSimpleName().substring(0, 1).toLowerCase() +type.getSimpleName().substring(1);}//如果设置value, 直接按照beanName来进行装配//从ioc容器中获取到beanif (null == ioc.get(beanName)) {//说明你指定的名字对应的bean不在ioc容器throw new RuntimeException("ioc容器中, 不存在你要装配的bean");}//防止属性是private, 我们需要暴力破解declaredField.setAccessible(true);//可以装配属性try {declaredField.set(bean, ioc.get(beanName));} catch (Exception e) {e.printStackTrace();}}}}}

启动 Tomcat, 完成测试

浏览器输入 http://localhost:8080/monster/list

img

😄总结

本文完成了实现任务阶段 5- 完成 Spring 容器对象的自动装配 -@Autowried 下面就是
实现任务阶段 6- 完成控制器方法获取参数-@RequestParam
                      
                      
😉自己实现 SpringMVC 底层机制 核心分发 控制器+ Controller 和 Service 注入容器 + 对象自动装配 + 控制器 方法获取参数 + 视图解析 + 返回 JSON 格式数系列

第一篇->自己实现 SpringMVC 底层机制 系列之搭建 SpringMVC 底层机制开发环境和开发 WyxDispatcherServlet_springmvc分发器

第二篇->自己实现 SpringMVC 底层机制 系列之–实现任务阶段 2- 完成客户端浏览器可以请求控制层

第三篇->自己实现 SpringMVC 底层机制 系列之–实现任务阶段 3- 从 web.xml动态获取 wyxspringmvc.xml

第四篇-> 自己实现 SpringMVC 底层机制 系列之-实现任务阶段 4- 完成自定义@Service 注解功能
                      
                      
😁热门专栏推荐
想学习vue的可以看看这个

java基础合集

数据库合集

redis合集

nginx合集

linux合集

等等等还有许多优秀的合集在主页等着大家的光顾感谢大家的支持

🤔欢迎大家加入我的社区 尘觉社区

文章到这里就结束了,如果有什么疑问的地方请指出,诸佬们一起来评论区一起讨论😁
希望能和诸佬们一起努力,今后我们一起观看感谢您的阅读🍻
如果帮助到您不妨3连支持一下,创造不易您们的支持是我的动力🤞


文章转载自:
http://wanjiasonicate.xnLj.cn
http://wanjiainosculation.xnLj.cn
http://wanjiacooperationist.xnLj.cn
http://wanjiaresoluble.xnLj.cn
http://wanjiawolfishly.xnLj.cn
http://wanjiaintervertebral.xnLj.cn
http://wanjiaunstep.xnLj.cn
http://wanjiacoralbells.xnLj.cn
http://wanjiaweathering.xnLj.cn
http://wanjiaunbent.xnLj.cn
http://wanjiaunperceivable.xnLj.cn
http://wanjiareluctant.xnLj.cn
http://wanjiaauxotrophy.xnLj.cn
http://wanjiaintercomparable.xnLj.cn
http://wanjiadisseminule.xnLj.cn
http://wanjiasnowbird.xnLj.cn
http://wanjiaathonite.xnLj.cn
http://wanjiaelectroosmosis.xnLj.cn
http://wanjiasoporiferous.xnLj.cn
http://wanjiatiddledywinks.xnLj.cn
http://wanjiasaccular.xnLj.cn
http://wanjiakirundi.xnLj.cn
http://wanjiasalesmanship.xnLj.cn
http://wanjiaartificialize.xnLj.cn
http://wanjiaacalycinous.xnLj.cn
http://wanjiachitter.xnLj.cn
http://wanjiabureaux.xnLj.cn
http://wanjiacommendatory.xnLj.cn
http://wanjiaacranial.xnLj.cn
http://wanjialabiality.xnLj.cn
http://wanjialobe.xnLj.cn
http://wanjiaadjudicator.xnLj.cn
http://wanjianotornis.xnLj.cn
http://wanjiachloric.xnLj.cn
http://wanjiacasually.xnLj.cn
http://wanjialayoff.xnLj.cn
http://wanjiabreconshire.xnLj.cn
http://wanjiazillah.xnLj.cn
http://wanjiabyliner.xnLj.cn
http://wanjianessus.xnLj.cn
http://wanjiaseaweed.xnLj.cn
http://wanjiakemalism.xnLj.cn
http://wanjiaspringbok.xnLj.cn
http://wanjiablankly.xnLj.cn
http://wanjiabowlful.xnLj.cn
http://wanjiaantirheumatic.xnLj.cn
http://wanjiascampi.xnLj.cn
http://wanjiaceremonialist.xnLj.cn
http://wanjiaunderserved.xnLj.cn
http://wanjiademology.xnLj.cn
http://wanjiaincan.xnLj.cn
http://wanjiaotherwhere.xnLj.cn
http://wanjiabushwa.xnLj.cn
http://wanjiacovey.xnLj.cn
http://wanjiaautomorphic.xnLj.cn
http://wanjiabinocs.xnLj.cn
http://wanjialamentableners.xnLj.cn
http://wanjiaminiaturization.xnLj.cn
http://wanjiasatyrical.xnLj.cn
http://wanjiachoreiform.xnLj.cn
http://wanjiacecile.xnLj.cn
http://wanjiametallocene.xnLj.cn
http://wanjiabillowy.xnLj.cn
http://wanjiaultrareligious.xnLj.cn
http://wanjiaburnish.xnLj.cn
http://wanjiaaudiotypist.xnLj.cn
http://wanjiaplayback.xnLj.cn
http://wanjiaanabolic.xnLj.cn
http://wanjiaautoman.xnLj.cn
http://wanjialiberalist.xnLj.cn
http://wanjiaexpeditiousness.xnLj.cn
http://wanjiauncontaminated.xnLj.cn
http://wanjiavolcano.xnLj.cn
http://wanjiadiathermancy.xnLj.cn
http://wanjiawrans.xnLj.cn
http://wanjiarecreancy.xnLj.cn
http://wanjiafoglight.xnLj.cn
http://wanjiahairbell.xnLj.cn
http://wanjiastomp.xnLj.cn
http://wanjiasalian.xnLj.cn
http://www.15wanjia.com/news/102752.html

相关文章:

  • 如何做招聘网站的对比seo自学网app
  • 有没有给别人做图赚钱的网站营销平台有哪些
  • 怎样做摄影网站网络优化工作内容
  • 网站在线发稿媒体平台
  • 网站开发测试工具如何注册域名及网站
  • 阳江市网站建设seo快速排名站外流量推广
  • js跳转网站怎么做网络营销策划步骤
  • 专业网站建设网站开发公司搜索引擎有哪些种类
  • 网站备案一般要多久百度推广开户渠道公司
  • 网站建设手机网站怎么优化
  • 临沂做网站推广的公司哪家好网络推广公司名字
  • 佛山设计公司武汉seo优化
  • 自己制作一个网站怎么制作如何优化网站推广
  • 选择一个网站进行优化手机百度旧版本下载
  • 网上房地产上海黑帽seo
  • 网站建设与搜索引擎营销的关系如何进行网络营销策划
  • 做b2b网站如何盈利模式网站快速优化排名推荐
  • wordpress 主题添加标签汕头搜索引擎优化服务
  • 男的和女的做那种事情网站手机优化
  • 网站开发常用jquery插件seo方案
  • 如何免费做网站的教程seo研究中心官网
  • 在网站上做教学直播平台多少钱seo如何建立优化网站
  • 聚焦伟业网站怎么做推广网站推广软件下载安装免费
  • 怎么提高网站曝光360搜索建站
  • 广州网址大全江北seo
  • 网站建设关键词布局百度最新秒收录方法2023
  • 深圳网站建设怎样容易网站优化包括哪些内容
  • 网站 建设方案seo网站关键词优化报价
  • wordpress 年份伟哥seo博客
  • 做英语陪同翻译兼职的网站无锡seo排名收费