<context-param>
作用:该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数
param-name 设定上下文的参数名称。必须是唯一名称
param-value 设定的参数名称的值
- 初始化过程:
- 在启动Web项目时,容器(比如Tomcat)会读web.xml配置文件中的两个节点<listener>和<contex-param>。
- 接着容器会创建一个ServletContext(上下文),应用范围内即整个WEB项目都能使用这个上下文。
- 接着容器会将读取到<context-param>转化为键值对,并交给ServletContext。
- 容器创建<listener></listener>中的类实例,即创建监听(备注:listener定义的类可以是自定义的类但必须需要继承ServletContextListener)。
- 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比如说数据库连接的关闭。
- 得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早。
- 页面中
${initParam.contextConfigLocation}
- Servlet中
String paramValue=getServletContext().getInitParameter("contextConfigLocation")
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param>
Listener:
Java项目中用到的spring监听器
ContextLoaderListener
作用:
启动Web容器时,自动装配ApplicationContext的配置信息。
用法:
第一种:直接将applicationContext.xml(spring的配置文件)放到/WEB-INF下,只在web.xml中声明一个listener
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
第二种:将之放到classpath下,但是此时要在web.xml中加入<context-param>,用它来指明你的applicationContext.xml的位置以供web容器来加载。如果有多个xml文件,可以写在一起并用“,”号分隔。
<listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<load-on-startup>:
正数的值越小,启动该servlet的优先级越高
Filter-FilterMapping:characterEncodingFilter
<filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
servlet-servletMapping:DispatcherServlet
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
applicationContext.xml
<context:component-scan base-package="com" annotation-config="true"/>
applicationContext.xml 和 dispatch-servlet.xml形成了两个父子关系的上下文。
1) 一个bean如果在两个文件中都被定义了(比如两个文件中都定义了component scan扫描相同的package), spring会在application context和 servlet context中都生成一个实例,他们处于不同的上下文空间中,他们的行为方式是有可能不一样的。
2) 如果在application context和 servlet context中都存在同一个 @Service 的实例, controller(在servlet context中) 通过 @Resource引用时, 会优先选择servlet context中的实例。
不过最好的方法是:在applicationContext和dispatcher-servlet定义的bean最好不要重复, dispatcher-servlet最好只是定义controller类型的bean。
ApplicationContext.xml 是spring 全局配置文件,用来控制spring 特性的
dispatcher-servlet.xml 是spring mvc里面的,控制器、拦截uri转发view
使用applicationContext.xml文件时是需要在web.xml中添加listener的:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
注解:
@Service服务层组件,用于标注业务层组件,表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,例如Chinese实例化为chinese,如果需要自己改名字则:@Service("你自己改的bean名")。
@Controller用于标注控制层组件(如struts中的action)
@Repository持久层组件,用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Autowired与@Resource都可以用来装配bean.都可以写在字段上,或写在setter方法上。@Autowired默认按类型装配,@Resource(这个注解属于J2EE的),默认安装名称进行装配。
classpath: 和 classpath* :
classpath是指 WEB-INF文件夹下的classes目录
解释classes含义:
1.存放各种资源配置文件 eg.init.properties log4j.properties struts.xml
2.存放模板文件 eg.actionerror.ftl
3.存放class文件 对应的是项目开发时的src目录编译文件
总结:这是一个定位资源的入口
如果你知道开发过程中有这么一句话:惯例大于配置 那么也许你会改变你的想法
对于第二个问题
这个涉及的是lib和classes下文件访问优先级的问题: lib>classes
对于性能的影响应该不在这个范畴
classpath 和 classpath* 区别:
classpath:只会到你的class路径中查找找文件;
classpath*:不仅包含class路径,还包括jar文件中(class路径)进行查找
用classpath*:需要遍历所有的classpath,所以加载速度是很慢的,因此,在规划的时候,应该尽可能规划好资源文件所在的路径,尽量避免使用classpath*。