Spring 注解学习手札一 构建简单Web应用.docx
《Spring 注解学习手札一 构建简单Web应用.docx》由会员分享,可在线阅读,更多相关《Spring 注解学习手札一 构建简单Web应用.docx(20页珍藏版)》请在冰豆网上搜索。
Spring注解学习手札一构建简单Web应用
我们将用到如下jar包:
引用
aopalliance-1.0.jar
commons-logging-1.1.1.jar
log4j-1.2.15.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-context-support-2.5.6.jar
spring-core-2.5.6.jar
spring-tx-2.5.6.jar
spring-web-2.5.6.jar
spring-webmvc-2.5.6.jar
先看web.xml
Xml代码
1.
xml version="1.0" encoding="UTF-8"?
>
2.3. xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
4. xmlns="
5. xmlns:
web="
6. xsi:
schemaLocation="
7. id="WebApp_ID"
8. version="2.5">
9. spring
10.
-- 应用路径 -->
11.
12. webAppRootKey
13. spring.webapp.root
14.
15.
-- Log4J 配置 -->
16.
17. log4jConfigLocation
18. classpath:
log4j.xml
19.
20.
21. log4jRefreshInterval
22. 60000
23.
24.
--Spring上下文 配置 -->
25.
26. contextConfigLocation
27. /WEB-INF/applicationContext.xml
28.
29.
-- 字符集 过滤器 -->
30.
31. CharacterEncodingFilter
32. org.springframework.web.filter.CharacterEncodingFilter
33.
34. encoding
35. UTF-8
36.
37.
38. forceEncoding
39. true
40.
41.
42.
43. CharacterEncodingFilter
44. /*
45.
46.
-- Spring 监听器 -->
47.
48. org.springframework.web.context.ContextLoaderListener
49.
50.
51. org.springframework.web.util.Log4jConfigListener
52.
53.
-- Spring 分发器 -->
54.
55. spring
56. org.springframework.web.servlet.DispatcherServlet
57.
58. contextConfigLocation
59. /WEB-INF/servlet.xml
60.
61.
62.
63. spring
64. *.do
65.
66.
67. index.html
68. index.htm
69. index.jsp
70. default.html
71. default.htm
72. default.jsp
73.
74.
有不少人问我,这段代码是什么:
Xml代码
1.
-- 应用路径 -->
2.
3. webAppRootKey
4. spring.webapp.root
5.
这是当前应用的路径变量,也就是说你可以在其他代码中使用${spring.webapp.root}指代当前应用路径。
我经常用它来设置log的输出目录。
为什么要设置参数log4jConfigLocation?
Xml代码
1.
-- Log4J 配置 -->
2.
3. log4jConfigLocation
4. classpath:
log4j.xml
5.
6.
7. log4jRefreshInterval
8. 60000
9.
这是一种基本配置,spring中很多代码使用了不同的日志接口,既有log4j也有commons-logging,这里只是强制转换为log4j!
并且,log4j的配置文件只能放在classpath根路径。
同时,需要通过commons-logging配置将日志控制权转交给log4j。
同时commons-logging.properties必须放置在classpath根路径。
commons-logging内容:
Properties代码
1.mons.logging.Log=mons.logging.impl.Log4JLogger
最后,记得配置log4j的监听器:
Xml代码
1.
2. org.springframework.web.util.Log4jConfigListener
3.
接下来,我们需要配置两套配置文件,applicationContext.xml和servlet.xml。
applicationContext.xml用于对应用层面做整体控制。
按照分层思想,统领service层和dao层。
servlet.xml则单纯控制controller层。
Xml代码
1.
xml version="1.0" encoding="UTF-8"?
>
2.3. xmlns="http:
//www.springframework.org/schema/beans"
4. xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
5. xmlns:
context="http:
//www.springframework.org/schema/context"
6. xsi:
schemaLocation="http:
//www.springframework.org/schema/beans http:
//www.springframework.org/schema/beans/spring-beans.xsd
7. http:
//www.springframework.org/schema/context http:
//www.springframework.org/schema/context/spring-context.xsd">
8. 9. resource="service.xml" />
10. 11. resource="dao.xml" />
12.
applicationContext.xml什么都不干,它只管涉及到整体需要的配置,并且集中管理。
这里引入了两个配置文件service.xml和dao.xml分别用于业务、数据处理。
service.xml
Xml代码
1.
xml version="1.0" encoding="UTF-8"?
>
2.3. xmlns="http:
//www.springframework.org/schema/beans"
4. xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
5. xmlns:
context="http:
//www.springframework.org/schema/context"
6. xsi:
schemaLocation="http:
//www.springframework.org/schema/beans http:
//www.springframework.org/schema/beans/spring-beans.xsd
7. http:
//www.springframework.org/schema/context http:
//www.springframework.org/schema/context/spring-context.xsd">
8. component-scan
9. base-package="org.zlex.spring.service" />
10.
注意,这里通过component-scan/>标签指定了业务层的基础包路径——“org.zlex.spring.service”。
也就是说,业务层相关实现均在这一层。
这是有必要的分层之一。
dao.xml
Xml代码
1.
xml version="1.0" encoding="UTF-8"?
>
2.3. xmlns="http:
//www.springframework.org/schema/beans"
4. xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
5. xmlns:
aop="http:
//www.springframework.org/schema/aop"
6. xmlns:
context="http:
//www.springframework.org/schema/context"
7. xmlns:
tx="http:
//www.springframework.org/schema/tx"
8. xsi:
schemaLocation="http:
//www.springframework.org/schema/beans http:
//www.springframework.org/schema/beans/spring-beans.xsd
9. http:
//www.springframework.org/schema/aop http:
//www.springframework.org/schema/aop/spring-aop.xsd
10. http:
//www.springframework.org/schema/context http:
//www.springframework.org/schema/context/spring-context.xsd
11. http:
//www.springframework.org/schema/tx http:
//www.springframework.org/schema/tx/spring-tx.xsd">
12. component-scan
13. base-package="org.zlex.spring.dao" />
14.
dao层如法炮制,包路径是"org.zlex.spring.dao"。
从这个角度看,注解还是很方便的!
最后,我们看看servlet.xml
Xml代码
1.
xml version="1.0" encoding="UTF-8"?
>
2.3. xmlns="http:
//www.springframework.org/schema/beans"
4. xmlns:
xsi="http:
//www.w3.org/2001/XMLSchema-instance"
5. xmlns:
context="http:
//www.springframework.org/schema/context"
6. xsi:
schemaLocation="http:
//www.springframework.org/schema/beans http:
//www.springframework.org/schema/beans/spring-beans.xsd
7. http:
//www.springframework.org/schema/context http:
//www.springframework.org/schema/context/spring-context.xsd">
8. component-scan
9. base-package="org.zlex.spring.controller" />
10. 11. id="urlMapping"
12. class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
13. 14. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
15.
包路径配置就不细说了,都是一个概念。
最重要的时候后面两个配置,这将使得注解生效!
“org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping”是默认实现,可以不写,Spring容器默认会默认使用该类。
“org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”直接关系到多动作控制器配置是否可用!
简单看一下代码结构,如图:
Account类是来存储账户信息,属于域对象,极为简单,代码如下所示:
Account.java
Java代码
1./**
2. * 2010-1-23
3. */
4.package org.zlex.spring.domain;
5.
6.import java.io.Serializable;
7.
8./**
9. *
10. * @author zlex.dongliang@">梁栋
11. * @version 1.0
12. * @since 1.0
13. */
14.public class Account implements Serializable {
15.
16. /**
17. *
18. */
19. private static final long serialVersionUID = -533698031946372178L;
20.
21. private String username;
22. private String password;
23.
24. /**
25. * @param username
26. * @param password
27. */
28. public Account(String username, String password) {
29. this.username = username;
30. this.password = password;
31. }
32.
33. /**
34. * @return the username
35. */
36. public String getUsername() {
37. return username;
38. }
39.
40. /**
41. * @param username the username to set
42. */
43. public void setUsername(String username) {
44. this.username = username;
45. }
46.
47. /**
48. * @return the password
49. */
50. public String getPassword() {
51. return password;
52