前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring中通过配置文件注入的方法

spring中通过配置文件注入的方法

作者头像
马克java社区
修改2020-11-13 17:10:33
8450
修改2020-11-13 17:10:33
举报
文章被收录于专栏:java大数据

2.通过配置文件注入的方法 上面的注入方法是通过@Service的注解方法。类似的还有@Repository、@Component、@Constroller,功能大体一样,就是实例化以后放到Spring容器当中接受管理。当然你肯定乐意在service类前放@Service而不愿意放@Repository而故意迷惑自己。另外注意,缺省的情况都是单态的。(省我们事了,但要注意线程安全)。除了注解注入,我们还有配置文件的方法来注入。相比注解的方法来讲,配置文件的方法比较集中,但缺乏灵活性。怎么讲呢?a处和b处想按不同的方式来处理?不行。因为统一一个地方处理。a和b必须统一,所以缺少了灵活性。 例 1.2 package com; import javax.annotation.Resource; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.support.RequestContextUtils; import service.interfac.ILoginService; @Controller public class HelloWorldController { private ILoginService loginServic; @RequestMapping("/helloa") public ModelAndView helloWorld(HttpServletRequest request, HttpServletResponse response, HttpSession sesssion) { ServletContext sc=RequestContextUtils.getWebApplicationContext(request).getServletContext(); WebApplicationContext wac=WebApplicationContextUtils.getRequiredWebApplicationContext(sc); ILoginService loginServic=(ILoginService)wac.getBean("loginService"); loginServic.login(); System.out.println("after loginServic.login()"); return new ModelAndView("/helloq", "message", "你好"); } }

更多请见:https://blog.csdn.net/qq_44591615/article/details/109206207

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档