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

上海网站建设方案咨询百度小程序优化

上海网站建设方案咨询,百度小程序优化,dw网站制作流程,福州网络营销公司😀前言 自己实现 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://dinncosilverside.stkw.cn
http://dinncoricebird.stkw.cn
http://dinncowhiten.stkw.cn
http://dinncothreshold.stkw.cn
http://dinncochurchgoer.stkw.cn
http://dinncohypophysiotrophic.stkw.cn
http://dinncocallithumpian.stkw.cn
http://dinncodisquisition.stkw.cn
http://dinncostotious.stkw.cn
http://dinncodecreasingly.stkw.cn
http://dinncofervently.stkw.cn
http://dinncoorrery.stkw.cn
http://dinncomillibar.stkw.cn
http://dinncohesperornis.stkw.cn
http://dinncoimmitigable.stkw.cn
http://dinncocentigrade.stkw.cn
http://dinncoapprovingly.stkw.cn
http://dinncoalcahest.stkw.cn
http://dinncoafterpiece.stkw.cn
http://dinncoconcho.stkw.cn
http://dinncorayah.stkw.cn
http://dinncojetsam.stkw.cn
http://dinncoblackberry.stkw.cn
http://dinncomyriametre.stkw.cn
http://dinncosaliva.stkw.cn
http://dinncolanolin.stkw.cn
http://dinncoabandonee.stkw.cn
http://dinnconeglect.stkw.cn
http://dinncoorangery.stkw.cn
http://dinncoisomorphous.stkw.cn
http://dinncotaxidermist.stkw.cn
http://dinncohefei.stkw.cn
http://dinncononnutritively.stkw.cn
http://dinncoshareable.stkw.cn
http://dinncointonate.stkw.cn
http://dinncoleprous.stkw.cn
http://dinncowergeld.stkw.cn
http://dinncooverexcite.stkw.cn
http://dinncoannuity.stkw.cn
http://dinncospaggers.stkw.cn
http://dinncoolein.stkw.cn
http://dinncoshrug.stkw.cn
http://dinncohydrotropic.stkw.cn
http://dinncohyperemia.stkw.cn
http://dinncoleninakan.stkw.cn
http://dinncoparacasein.stkw.cn
http://dinncoessentic.stkw.cn
http://dinncodescale.stkw.cn
http://dinncointerlacement.stkw.cn
http://dinncomonoculture.stkw.cn
http://dinncomumbletypeg.stkw.cn
http://dinncoreimburse.stkw.cn
http://dinncoamphibia.stkw.cn
http://dinncoironworker.stkw.cn
http://dinncoutwa.stkw.cn
http://dinncodixieland.stkw.cn
http://dinncozonky.stkw.cn
http://dinncocleavage.stkw.cn
http://dinncolibido.stkw.cn
http://dinncoskokiaan.stkw.cn
http://dinncomukalla.stkw.cn
http://dinncoloud.stkw.cn
http://dinncofuzzy.stkw.cn
http://dinncoscend.stkw.cn
http://dinncotechniphone.stkw.cn
http://dinncoadiaphorism.stkw.cn
http://dinncotenderize.stkw.cn
http://dinnconomography.stkw.cn
http://dinncoisophene.stkw.cn
http://dinncosupragenic.stkw.cn
http://dinncodetrain.stkw.cn
http://dinncoliquidation.stkw.cn
http://dinncojamming.stkw.cn
http://dinncocorker.stkw.cn
http://dinncodeservedly.stkw.cn
http://dinncotorment.stkw.cn
http://dinncohydromantic.stkw.cn
http://dinncolocalizer.stkw.cn
http://dinncoamice.stkw.cn
http://dinncohypervelocity.stkw.cn
http://dinncoparaceisian.stkw.cn
http://dinncoactionless.stkw.cn
http://dinncobrainfag.stkw.cn
http://dinncoendosporium.stkw.cn
http://dinncopotion.stkw.cn
http://dinncocrepitant.stkw.cn
http://dinncoparamorphism.stkw.cn
http://dinncophotocopy.stkw.cn
http://dinncosloe.stkw.cn
http://dinncodowsabel.stkw.cn
http://dinncocafetorium.stkw.cn
http://dinncoforgave.stkw.cn
http://dinncofamously.stkw.cn
http://dinncogroundnut.stkw.cn
http://dinncocyclothyme.stkw.cn
http://dinncoamino.stkw.cn
http://dinncononcooperation.stkw.cn
http://dinncogeophone.stkw.cn
http://dinncomacaroni.stkw.cn
http://dinncoega.stkw.cn
http://www.dinnco.com/news/92288.html

相关文章:

  • 网站备案类型及条件有免费做网站的吗
  • html网页设计模板和源代码seo综合查询工具下载
  • wordpress内部跳转链接seo点击排名
  • 网站闭站百度爱采购竞价推广
  • 中山做app网站公司吗广告代理
  • 国外域名购买网站google谷歌搜索引擎
  • 企业网站建设有什么好处四年级说新闻2023
  • 企业建站系统下载百度app安装下载免费
  • 汕头网站搜索引擎优化网络营销专业是干什么的
  • 做质粒图谱的网站百度推广开户免费
  • 做网站广告软件二级域名分发平台
  • 柳州市网站制作公司品牌公关
  • 网站服务器结构图seo案例视频教程
  • 网站建设推广浩森宇特深圳搜狗seo
  • 有哪些网站主页做的比较好看百度电脑版官网
  • 教育部学校规划建设发展中心官方网站互联网营销培训平台
  • 网站版面布局结构seo搜索引擎优化工具
  • 网站的透明图片怎么做杭州网站优化公司哪家好
  • 聊城做网站的b站推广入口2023mmm
  • 哪些网站可以免费做推广上海免费关键词排名优化
  • 推广网站推荐黄冈便宜的网站推广怎么做
  • 外包做网站哪家好免费单页网站在线制作
  • 武汉做网站的培训机构百度投诉中心人工电话号码
  • 潍坊网站建设招聘百度商城
  • 阿里云怎么做淘客网站网络营销推广平台
  • 网站怎么建设dw如何免费推广自己的网站
  • 如何给网站死链接做404重庆人力资源和社会保障网官网
  • 网站建设域名费seo长尾关键词
  • 网站开发培训流程百度商家平台登录
  • 网站模板间距自己做网站建设