Context param(如何取得Spring管理的bean)

2024-06-23 11:30:03 :125

context param(如何取得Spring管理的bean)

其实context param的问题并不复杂,但是又很多的朋友都不太了解如何取得Spring管理的bean,因此呢,今天小编就来为大家分享context param的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!

本文目录

如何取得Spring管理的bean

如何取得Spring管理的bean (请用第3种方法):

1、servlet方式加载时,【web.xml】

Xml代码

《servlet》 《servlet-name》springMVC《/servlet-name》 《servlet-class》org.springframework.web.servlet.DispatcherServlet《/servlet-class》 《init-param》 《param-name》contExtConfigLocation《/param-name》 《param-value》classpath*:/springMVC.xml《/param-value》 《/init-param》 《load-on-startup》1《/load-on-startup》 《/servlet》

spring容器放在ServletContext中的key是org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC注意后面的springMVC,是你的servlet-name配置的值,注意适时修改。

Java代码

ServletContext sc=略 WebApplicationContext attr = (WebApplicationContext)sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.springMVC");

2、listener方式加载时:【web.xml】

Xml代码

《context-param》 《param-name》contextConfigLocation《/param-name》 《param-value》/WEB-INF/applicationContext《/param-value》 《/context-param》 《listener》 《listener-class》org.springframework.web.context.ContextLoaderListener《/listener-class》 《/listener》

【jsp/servlet】可以这样取得

Java代码

ServletContext context = getServletContext(); WebApplicationContext applicationContext = WebApplicationContextUtils .getWebApplicationContext(context);

3、通用的方法来了,神器啊,前的 1、2两种方法并不通用,可以抛弃了。在配置文件中加入:

Xml代码

《!-- 用于持有ApplicationContext,可以使用SpringContextHolder.getBean(’xxxx’)的静态方法得到spring bean对象 --》 《bean class="com.xxxxx.SpringContextHolder" lazy-init="false" /》

Java代码

import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候中取出ApplicaitonContext. * */ public class SpringContextHolder implements ApplicationContextAware { private static ApplicationContext applicationContext; /** * 实现ApplicationContextAware接口的context注入函数, 将其存入静态变量. */ public void setApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext = applicationContext; // NOSONAR } /** * 取得存储在静态变量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { checkApplicationContext(); return applicationContext; } /** * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static 《T》 T getBean(String name) { checkApplicationContext(); return (T) applicationContext.getBean(name); } /** * 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static 《T》 T getBean(Class《T》 clazz) { checkApplicationContext(); return (T) applicationContext.getBeansOfType(clazz); } /** * 清除applicationContext静态变量. */ public static void cleanApplicationContext() { applicationContext = null; private static void checkApplicationContext() { if (applicationContext == null) { throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder"); } } }

servlet 从java中获取参数

在web.xml里面可以定义两种参数:一种是全局范围的参数,一种是servlet内的参数。 web.xml里定义参数的应用举例:在做分页功能时,可以在代码中直给定pageSize的值,这样,写死在代码中,灵活性差。改进方法:将pageSize配置在web.xml中,然后再在代码中获取。 1. 全局范围的参数,存放在ServletContext对象中,在web.xml中的配置如下:1 《context-param》2 《param-name》page-size《/param-name》3 《param-value》2《/param-value》4 《/context-param》在servlet中的获取:1 ServletContext sc = this.getServletContext();2 String strPageSize = sc.getInitParameter("page-size");3 int pageSize=Integer.parseInt(strPageSize);或1 int pageSize=Integer.parseInt(this.getServletContext().getInitParameter("page-size"));2. Servlet范围内的参数,只能在servlet的inti()方法中取得,在web.xml中的配置如下:1 《servlet》 2 《servlet-name》SearchItemServlet《/servlet-name》 3 《servlet-class》 4 com.bjpowernode.drp.basedata.web.SearchItemServlet 5 《/servlet-class》 6 《init-param》 7 《param-name》page-size《/param-name》 8 《param-value》3《/param-value》 9 《/init-param》10 《/servlet》 在servlet中的获取:1 int pageSize=Integer.parseInt(this.getServletConfig().getInitParameter("page-size"));getServletContext()获得的是 《context-param》 《/context-param》配置的参数信息getServletConfig()获得的是 《init-param》 《/init-param》配置的参数信息

context param 可以有多个吗

web.xml里面可以定义两种参数: (1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下: context/param avalible during application (2)servlet范围内的参数,只能在servlet的init()方法中取得

java 问题:在web.xml中,标签的作用是什么

1.启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: 《listener》《/listener》 和 《context-param》《/context-param》2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.3.容器将《context-param》《/context-param》转化为键值对,并交给ServletContext.4.容器创建《listener》《/listener》中的类实例,即创建监听.5.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中获得ServletContext = ServletContextEvent.getServletContext();context-param的值 = ServletContext.getInitParameter("context-param的键");6.得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.换句话说,这个时候,你对《context-param》中的键值做的操作,将在你的WEB项目完全启动之前被执行.7.举例.你可能想在项目启动之前就打开数据库.那么这里就可以在《context-param》中设置数据库的连接方式,在监听类中初始化数据库的连接.8.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.9.... 以上资料来自《《Head First Servlet&JSP》》

在WEB.XML中可以配置多对<context-param>吗为什么我配置的多个只有第一个读入了程序

配好Spring 后,在spring-web.jar包下找,org.springframework.web.context包,再找这个包下的ContextLoaderListener类,对着它点右键复制限定名就ok了在WEB.XML中可以配置多对<context-param>吗?为什么我配置的多个只有第一个读入了程序?这里有视频教程可以看下 http://www.alisoho.com

context-param元素含有一对参数名和参数值,作用是为应用的servlet上下文初始化参数。application范围内的参数,存放在servletcontext中,在web.xml中配置如下:《context-param》 《param-name》context/param《/param-name》 《param-value》avalible during application《/param-value》《/context-param》

一个web项目web.xml的配置中配置是起什么作用的

在任一个 servlet 或 filter 中使用: @Overridepublic void init(ServletConfig config) throws ServletException {String paramValue1 = config.getServletContext().getInitParameter("ParamName1");}//or@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String paramValue1 = this.getServletContext().getInitParameter("ParamName1");...}...}另外,配置在servlet中的init-param的用法为:《servlet》 《servlet-name》A Servlet《/servlet-name》 《servlet-class》com.controller.TestServlet《/servlet-class》 《init-param》 《description》This is an init parameter example《/description》 《param-name》InitParam1《/param-name》 《param-value》init param value《/param-value》 《/init-param》 《/servlet》@Overridepublic void init(ServletConfig config) throws ServletException {String initParam1 = config.getInitParameter("InitParam1");}//or@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String initParam1 = getServletConfig().getInitParameter("InitParam1");...}比较一下:getServletContext().getInitParameter();getServletConfig().getInitParameter();

启动tomcat后,web项目下的web.xml里面的context-param的内容貌似没加载,会先加载servlet

  • 一个原因就是你的程序有问题,另一个原因可能是你的 《load-on-startup》1《/load-on-startup》没有配置。

  • 哪有,这只是设置容器的参数而已。

  • 把context-param放在 servlet前面加载呗

文章分享结束,context param和如何取得Spring管理的bean的答案你都知道了吗?欢迎再次光临本站哦!

context param(如何取得Spring管理的bean)

本文编辑:admin
Copyright © 2022 All Rights Reserved 威海上格软件有限公司 版权所有

鲁ICP备20007704号

Thanks for visiting my site.