Spring学习心得(三)——注解装配
在前两篇文章中我大致介绍了Spring框架的搭建、配置文件注入、log4j日志配置和对数据库的操作,虽然利用这些东西我可以使用Spring构建web工程,但是丝毫感受不到Spring的优势所在,而且多了一大堆的配置文件需要编写。本文开始我则从优化WEB工程,简化程序员编码的角度来继续实战Spring。
第一步,检查需要的外包,本工程所依赖的jar包有:aopalliance、commons-logging、log4j、
spring-beans、spring-context、spring-context-support、spring-core、spring-tx、spring-web、spring-webmvc。这些包在我前两篇博客的附件中都可以找到,把这些所需要的外包直接复制到“WebRoot/WEB-INF/lib/”中即可。
第二步,编写配置文件.
1.在web.xml文件中加入Spring所依赖的配置信息,在前一篇文章所编写的基础上还要加上注解自动装配,字符集过滤器、Spring 分发器这些设置。
<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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
这几行配置是利用Spring框架中的字符过滤类对所有到达的请求进行字符集过滤,将编码格式统一设置成UTF-8格式。
<servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.lzy</url-pattern> </servlet-mapping>
这几行是配置Spring 分发器,该配置中将拦截所有以.lzy结尾的请求,并使用Spring的DispatcherServlet类进行分发,并依参数形式说明了servlet配置文件的路径。
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param>
这里将Spring全局的配置文件制定为applicationContext.xml来做整体控制。
这样完全配置的web.xml文件为:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>MySpringMVC</display-name> <!-- 应用路径 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>spring.webapp.root</param-value> </context-param> <!-- Log4J 配置 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/conf/log4j.properties</param-value> </context-param> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>60000</param-value> </context-param> <!--Spring上下文 配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 字符集 过滤器 --> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring 监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- Spring 分发器 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.lzy</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>2.applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <import resource="service.xml" /> <import resource="dao.xml" /> </beans>applicationContext.xml什么都不干,它只管涉及到整体需要的配置,并且集中管理。这里引入了两个配置文件service.xml和dao.xml分别用于业务、数据处理。