- 浏览: 111618 次
- 性别:
- 来自: 上海
-
文章分类
- 全部博客 (87)
- SQL语句优化技术分析 (3)
- SQL SERVER查询优化 (2)
- JDBC查询优化 (1)
- 服务器调优 (1)
- 100 Best Quotes On Leadership (1)
- Java NIO Chinese Tutorial (1)
- spring security (1)
- JAVA线程池的分析和使用 (1)
- Java并发 (1)
- Java NIO框架Mina (1)
- Netty (1)
- Grizzly比较 (1)
- What Factors Justify the Use of Apache Hadoop? (1)
- Instagram工程师教你如何改善App的性能 (1)
- The Ubuntu's Directory Tree (1)
- JVM调优总结 -Xms -Xmx -Xmn -Xss (1)
- Tomcat configuration recommendations (1)
- Java 7 并发编程指南 (1)
- Java完整并发教程 (1)
- Java公钥私钥签名工具包应用实例 (1)
- Java集群解决方案 (1)
- Memcache和Redis比较 (1)
- 大型网站系统架构的演化 (1)
- 天猫浏览型应用的CDN静态化架构演变 (1)
- 淘宝的架构 (1)
- 大规模网站架构的缓存机制和几何分形学 (1)
- 敏捷开发 (1)
- 什么时候用MongoDB? (1)
- Java算法 (1)
- OAuth 2.0概览 (1)
- 全面理解OAuth 2.0 (1)
- Java工程师面试必问题目 (1)
最新评论
-
zhanggeng22:
[b][i][u]引用[list]
[*][img][url] ...
tomcat 的server.xml配置详解,看完你100%熟练配置tomcat -
yangsong158:
好文章,必需顶一个
大规模网站架构的缓存机制和几何分形学 -
卖火柴的老特工:
request.getparameter是获取通过get或则p ...
Request的getParameter()和getAttribute()方法的区别 -
zhuzhiguosnail:
终于能恢复了
十三个价值连城的成功习惯 -
liuwenbo200285:
sturts1 action是单例模式,线程是安全的。 ...
Java常碰面试题
先来构建一个极为简单的web应用,从controller到dao。不考虑具体实现,只是先对整体架构有一个清晰的了解。
我们将用到如下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.<web-app
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns="http://java.sun.com/xml/ns/javaee"
5. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
6. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
7. id="WebApp_ID"
8. version="2.5">
9. <display-name>spring</display-name>
10. <!-- 应用路径 -->
11. <context-param>
12. <param-name>webAppRootKey</param-name>
13. <param-value>spring.webapp.root</param-value>
14. </context-param>
15. <!-- Log4J 配置 -->
16. <context-param>
17. <param-name>log4jConfigLocation</param-name>
18. <param-value>classpath:log4j.xml</param-value>
19. </context-param>
20. <context-param>
21. <param-name>log4fRefreshInterval</param-name>
22. <param-value>60000</param-value>
23. </context-param>
24. <!--Spring上下文 配置 -->
25. <context-param>
26. <param-name>contextConfigLocation</param-name>
27. <param-value>/WEB-INF/applicationContext.xml</param-value>
28. </context-param>
29. <!-- 字符集 过滤器 -->
30. <filter>
31. <filter-name>CharacterEncodingFilter</filter-name>
32. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
33. <init-param>
34. <param-name>encoding</param-name>
35. <param-value>UTF-8</param-value>
36. </init-param>
37. <init-param>
38. <param-name>forceEncoding</param-name>
39. <param-value>true</param-value>
40. </init-param>
41. </filter>
42. <filter-mapping>
43. <filter-name>CharacterEncodingFilter</filter-name>
44. <url-pattern>/*</url-pattern>
45. </filter-mapping>
46. <!-- Spring 监听器 -->
47. <listener>
48. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
49. </listener>
50. <listener>
51. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
52. </listener>
53. <!-- Spring 分发器 -->
54. <servlet>
55. <servlet-name>spring</servlet-name>
56. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
57. <init-param>
58. <param-name>contextConfigLocation</param-name>
59. <param-value>/WEB-INF/servlet.xml</param-value>
60. </init-param>
61. </servlet>
62. <servlet-mapping>
63. <servlet-name>spring</servlet-name>
64. <url-pattern>*.do</url-pattern>
65. </servlet-mapping>
66. <welcome-file-list>
67. <welcome-file>index.html</welcome-file>
68. <welcome-file>index.htm</welcome-file>
69. <welcome-file>index.jsp</welcome-file>
70. <welcome-file>default.html</welcome-file>
71. <welcome-file>default.htm</welcome-file>
72. <welcome-file>default.jsp</welcome-file>
73. </welcome-file-list>
74.</web-app>
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<web-app
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns="http://java.sun.com/xml/ns/javaee"
5. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
6. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
7. id="WebApp_ID"
8. version="2.5">
9. <display-name>spring</display-name>
10. <!-- 应用路径 -->
11. <context-param>
12. <param-name>webAppRootKey</param-name>
13. <param-value>spring.webapp.root</param-value>
14. </context-param>
15. <!-- Log4J 配置 -->
16. <context-param>
17. <param-name>log4jConfigLocation</param-name>
18. <param-value>classpath:log4j.xml</param-value>
19. </context-param>
20. <context-param>
21. <param-name>log4fRefreshInterval</param-name>
22. <param-value>60000</param-value>
23. </context-param>
24. <!--Spring上下文 配置 -->
25. <context-param>
26. <param-name>contextConfigLocation</param-name>
27. <param-value>/WEB-INF/applicationContext.xml</param-value>
28. </context-param>
29. <!-- 字符集 过滤器 -->
30. <filter>
31. <filter-name>CharacterEncodingFilter</filter-name>
32. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
33. <init-param>
34. <param-name>encoding</param-name>
35. <param-value>UTF-8</param-value>
36. </init-param>
37. <init-param>
38. <param-name>forceEncoding</param-name>
39. <param-value>true</param-value>
40. </init-param>
41. </filter>
42. <filter-mapping>
43. <filter-name>CharacterEncodingFilter</filter-name>
44. <url-pattern>/*</url-pattern>
45. </filter-mapping>
46. <!-- Spring 监听器 -->
47. <listener>
48. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
49. </listener>
50. <listener>
51. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
52. </listener>
53. <!-- Spring 分发器 -->
54. <servlet>
55. <servlet-name>spring</servlet-name>
56. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
57. <init-param>
58. <param-name>contextConfigLocation</param-name>
59. <param-value>/WEB-INF/servlet.xml</param-value>
60. </init-param>
61. </servlet>
62. <servlet-mapping>
63. <servlet-name>spring</servlet-name>
64. <url-pattern>*.do</url-pattern>
65. </servlet-mapping>
66. <welcome-file-list>
67. <welcome-file>index.html</welcome-file>
68. <welcome-file>index.htm</welcome-file>
69. <welcome-file>index.jsp</welcome-file>
70. <welcome-file>default.html</welcome-file>
71. <welcome-file>default.htm</welcome-file>
72. <welcome-file>default.jsp</welcome-file>
73. </welcome-file-list>
74.</web-app>
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5">
<display-name>spring</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>classpath:log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4fRefreshInterval</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>*.do</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>
有不少人问我,这段代码是什么:
Xml代码
1.<!-- 应用路径 -->
2.<context-param>
3. <param-name>webAppRootKey</param-name>
4. <param-value>spring.webapp.root</param-value>
5.</context-param>
Xml代码
1.<!-- 应用路径 -->
2.<context-param>
3. <param-name>webAppRootKey</param-name>
4. <param-value>spring.webapp.root</param-value>
5.</context-param>
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>spring.webapp.root</param-value>
</context-param>
这是当前应用的路径变量,也就是说你可以在其他代码中使用${spring.webapp.root}指代当前应用路径。我经常用它来设置log的输出目录。
为什么要设置参数log4jConfigLocation?
Xml代码
1.<!-- Log4J 配置 -->
2. <context-param>
3. <param-name>log4jConfigLocation</param-name>
4. <param-value>classpath:log4j.xml</param-value>
5. </context-param>
6. <context-param>
7. <param-name>log4fRefreshInterval</param-name>
8. <param-value>60000</param-value>
9. </context-param>
Xml代码
1.<!-- Log4J 配置 -->
2. <context-param>
3. <param-name>log4jConfigLocation</param-name>
4. <param-value>classpath:log4j.xml</param-value>
5. </context-param>
6. <context-param>
7. <param-name>log4fRefreshInterval</param-name>
8. <param-value>60000</param-value>
9. </context-param>
<!-- Log4J 配置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4fRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
这是一种基本配置,spring中很多代码使用了不同的日志接口,既有log4j也有commons-logging,这里只是强制转换为log4j!并且,log4j的配置文件只能放在classpath根路径。同时,需要通过commons-logging配置将日志控制权转交给log4j。同时commons-logging.properties必须放置在classpath根路径。
commons-logging内容:
Properties代码
1.org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
Properties代码
1.org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
最后,记得配置log4j的监听器:
Xml代码
1.<listener>
2. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
3.</listener>
Xml代码
1.<listener>
2. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
3.</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
接下来,我们需要配置两套配置文件,applicationContext.xml和servlet.xml。
applicationContext.xml用于对应用层面做整体控制。按照分层思想,统领service层和dao层。servlet.xml则单纯控制controller层。
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <import
9. resource="service.xml" />
10. <import
11. resource="dao.xml" />
12.</beans>
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <import
9. resource="service.xml" />
10. <import
11. resource="dao.xml" />
12.</beans>
<?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分别用于业务、数据处理。
service.xml
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <context:component-scan
9. base-package="org.zlex.spring.service" />
10.</beans>
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <context:component-scan
9. base-package="org.zlex.spring.service" />
10.</beans>
<?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">
<context:component-scan
base-package="org.zlex.spring.service" />
</beans>
注意,这里通过<context:component-scan />标签指定了业务层的基础包路径——“org.zlex.spring.service”。也就是说,业务层相关实现均在这一层。这是有必要的分层之一。
dao.xml
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <context:component-scan
13. base-package="org.zlex.spring.dao" />
14.</beans>
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <context:component-scan
13. base-package="org.zlex.spring.dao" />
14.</beans>
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan
base-package="org.zlex.spring.dao" />
</beans>
dao层如法炮制,包路径是"org.zlex.spring.dao"。从这个角度看,注解还是很方便的!
最后,我们看看servlet.xml
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <context:component-scan
9. base-package="org.zlex.spring.controller" />
10. <bean
11. id="urlMapping"
12. class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
13. <bean
14. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
15.</beans>
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <context:component-scan
9. base-package="org.zlex.spring.controller" />
10. <bean
11. id="urlMapping"
12. class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
13. <bean
14. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
15.</beans>
<?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">
<context:component-scan
base-package="org.zlex.spring.controller" />
<bean
id="urlMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>
包路径配置就不细说了,都是一个概念。最重要的时候后面两个配置,这将使得注解生效!
“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 <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
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. }
53.
54. /**
55. * @param password the password to set
56. */
57. public void setPassword(String password) {
58. this.password = password;
59. }
60.
61.
62.}
Java代码
1./**
2. * 2010-1-23
3. */
4.package org.zlex.spring.domain;
5.
6.import java.io.Serializable;
7.
8./**
9. *
10. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
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. }
53.
54. /**
55. * @param password the password to set
56. */
57. public void setPassword(String password) {
58. this.password = password;
59. }
60.
61.
62.}
/**
* 2010-1-23
*/
package org.zlex.spring.domain;
import java.io.Serializable;
/**
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
public class Account implements Serializable {
/**
*
*/
private static final long serialVersionUID = -533698031946372178L;
private String username;
private String password;
/**
* @param username
* @param password
*/
public Account(String username, String password) {
this.username = username;
this.password = password;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
通常,在构建域对象时,需要考虑该对象可能需要进行网络传输,本地缓存,因此建议实现序列化接口Serializable
我们再来看看控制器,这就稍微复杂了一点代码如下所示:
AccountController .java
Java代码
1./**
2. * 2010-1-23
3. */
4.package org.zlex.spring.controller;
5.
6.import javax.servlet.http.HttpServletRequest;
7.import javax.servlet.http.HttpServletResponse;
8.
9.import org.springframework.beans.factory.annotation.Autowired;
10.import org.springframework.stereotype.Controller;
11.import org.springframework.web.bind.ServletRequestUtils;
12.import org.springframework.web.bind.annotation.RequestMapping;
13.import org.springframework.web.bind.annotation.RequestMethod;
14.import org.zlex.spring.service.AccountService;
15.
16./**
17. *
18. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
19. * @version 1.0
20. * @since 1.0
21. */
22.@Controller
23.@RequestMapping("/account.do")
24.public class AccountController {
25.
26. @Autowired
27. private AccountService accountService;
28.
29. @RequestMapping(method = RequestMethod.GET)
30. public void hello(HttpServletRequest request, HttpServletResponse response)
31. throws Exception {
32.
33. String username = ServletRequestUtils.getRequiredStringParameter(
34. request, "username");
35. String password = ServletRequestUtils.getRequiredStringParameter(
36. request, "password");
37. System.out.println(accountService.verify(username, password));
38. }
39.}
Java代码
1./**
2. * 2010-1-23
3. */
4.package org.zlex.spring.controller;
5.
6.import javax.servlet.http.HttpServletRequest;
7.import javax.servlet.http.HttpServletResponse;
8.
9.import org.springframework.beans.factory.annotation.Autowired;
10.import org.springframework.stereotype.Controller;
11.import org.springframework.web.bind.ServletRequestUtils;
12.import org.springframework.web.bind.annotation.RequestMapping;
13.import org.springframework.web.bind.annotation.RequestMethod;
14.import org.zlex.spring.service.AccountService;
15.
16./**
17. *
18. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
19. * @version 1.0
20. * @since 1.0
21. */
22.@Controller
23.@RequestMapping("/account.do")
24.public class AccountController {
25.
26. @Autowired
27. private AccountService accountService;
28.
29. @RequestMapping(method = RequestMethod.GET)
30. public void hello(HttpServletRequest request, HttpServletResponse response)
31. throws Exception {
32.
33. String username = ServletRequestUtils.getRequiredStringParameter(
34. request, "username");
35. String password = ServletRequestUtils.getRequiredStringParameter(
36. request, "password");
37. System.out.println(accountService.verify(username, password));
38. }
39.}
/**
* 2010-1-23
*/
package org.zlex.spring.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.zlex.spring.service.AccountService;
/**
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
@Controller
@RequestMapping("/account.do")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping(method = RequestMethod.GET)
public void hello(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String username = ServletRequestUtils.getRequiredStringParameter(
request, "username");
String password = ServletRequestUtils.getRequiredStringParameter(
request, "password");
System.out.println(accountService.verify(username, password));
}
}
分段详述:
Java代码
1.@Controller
2.@RequestMapping("/account.do")
Java代码
1.@Controller
2.@RequestMapping("/account.do")
@Controller
@RequestMapping("/account.do")
这两行注解,@Controller是告诉Spring容器,这是一个控制器类,@RequestMapping("/account.do")是来定义该控制器对应的请求路径(/account.do)
Java代码
我们将用到如下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.<web-app
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns="http://java.sun.com/xml/ns/javaee"
5. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
6. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
7. id="WebApp_ID"
8. version="2.5">
9. <display-name>spring</display-name>
10. <!-- 应用路径 -->
11. <context-param>
12. <param-name>webAppRootKey</param-name>
13. <param-value>spring.webapp.root</param-value>
14. </context-param>
15. <!-- Log4J 配置 -->
16. <context-param>
17. <param-name>log4jConfigLocation</param-name>
18. <param-value>classpath:log4j.xml</param-value>
19. </context-param>
20. <context-param>
21. <param-name>log4fRefreshInterval</param-name>
22. <param-value>60000</param-value>
23. </context-param>
24. <!--Spring上下文 配置 -->
25. <context-param>
26. <param-name>contextConfigLocation</param-name>
27. <param-value>/WEB-INF/applicationContext.xml</param-value>
28. </context-param>
29. <!-- 字符集 过滤器 -->
30. <filter>
31. <filter-name>CharacterEncodingFilter</filter-name>
32. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
33. <init-param>
34. <param-name>encoding</param-name>
35. <param-value>UTF-8</param-value>
36. </init-param>
37. <init-param>
38. <param-name>forceEncoding</param-name>
39. <param-value>true</param-value>
40. </init-param>
41. </filter>
42. <filter-mapping>
43. <filter-name>CharacterEncodingFilter</filter-name>
44. <url-pattern>/*</url-pattern>
45. </filter-mapping>
46. <!-- Spring 监听器 -->
47. <listener>
48. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
49. </listener>
50. <listener>
51. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
52. </listener>
53. <!-- Spring 分发器 -->
54. <servlet>
55. <servlet-name>spring</servlet-name>
56. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
57. <init-param>
58. <param-name>contextConfigLocation</param-name>
59. <param-value>/WEB-INF/servlet.xml</param-value>
60. </init-param>
61. </servlet>
62. <servlet-mapping>
63. <servlet-name>spring</servlet-name>
64. <url-pattern>*.do</url-pattern>
65. </servlet-mapping>
66. <welcome-file-list>
67. <welcome-file>index.html</welcome-file>
68. <welcome-file>index.htm</welcome-file>
69. <welcome-file>index.jsp</welcome-file>
70. <welcome-file>default.html</welcome-file>
71. <welcome-file>default.htm</welcome-file>
72. <welcome-file>default.jsp</welcome-file>
73. </welcome-file-list>
74.</web-app>
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<web-app
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xmlns="http://java.sun.com/xml/ns/javaee"
5. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
6. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
7. id="WebApp_ID"
8. version="2.5">
9. <display-name>spring</display-name>
10. <!-- 应用路径 -->
11. <context-param>
12. <param-name>webAppRootKey</param-name>
13. <param-value>spring.webapp.root</param-value>
14. </context-param>
15. <!-- Log4J 配置 -->
16. <context-param>
17. <param-name>log4jConfigLocation</param-name>
18. <param-value>classpath:log4j.xml</param-value>
19. </context-param>
20. <context-param>
21. <param-name>log4fRefreshInterval</param-name>
22. <param-value>60000</param-value>
23. </context-param>
24. <!--Spring上下文 配置 -->
25. <context-param>
26. <param-name>contextConfigLocation</param-name>
27. <param-value>/WEB-INF/applicationContext.xml</param-value>
28. </context-param>
29. <!-- 字符集 过滤器 -->
30. <filter>
31. <filter-name>CharacterEncodingFilter</filter-name>
32. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
33. <init-param>
34. <param-name>encoding</param-name>
35. <param-value>UTF-8</param-value>
36. </init-param>
37. <init-param>
38. <param-name>forceEncoding</param-name>
39. <param-value>true</param-value>
40. </init-param>
41. </filter>
42. <filter-mapping>
43. <filter-name>CharacterEncodingFilter</filter-name>
44. <url-pattern>/*</url-pattern>
45. </filter-mapping>
46. <!-- Spring 监听器 -->
47. <listener>
48. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
49. </listener>
50. <listener>
51. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
52. </listener>
53. <!-- Spring 分发器 -->
54. <servlet>
55. <servlet-name>spring</servlet-name>
56. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
57. <init-param>
58. <param-name>contextConfigLocation</param-name>
59. <param-value>/WEB-INF/servlet.xml</param-value>
60. </init-param>
61. </servlet>
62. <servlet-mapping>
63. <servlet-name>spring</servlet-name>
64. <url-pattern>*.do</url-pattern>
65. </servlet-mapping>
66. <welcome-file-list>
67. <welcome-file>index.html</welcome-file>
68. <welcome-file>index.htm</welcome-file>
69. <welcome-file>index.jsp</welcome-file>
70. <welcome-file>default.html</welcome-file>
71. <welcome-file>default.htm</welcome-file>
72. <welcome-file>default.jsp</welcome-file>
73. </welcome-file-list>
74.</web-app>
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID"
version="2.5">
<display-name>spring</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>classpath:log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4fRefreshInterval</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>*.do</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>
有不少人问我,这段代码是什么:
Xml代码
1.<!-- 应用路径 -->
2.<context-param>
3. <param-name>webAppRootKey</param-name>
4. <param-value>spring.webapp.root</param-value>
5.</context-param>
Xml代码
1.<!-- 应用路径 -->
2.<context-param>
3. <param-name>webAppRootKey</param-name>
4. <param-value>spring.webapp.root</param-value>
5.</context-param>
<!-- 应用路径 -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>spring.webapp.root</param-value>
</context-param>
这是当前应用的路径变量,也就是说你可以在其他代码中使用${spring.webapp.root}指代当前应用路径。我经常用它来设置log的输出目录。
为什么要设置参数log4jConfigLocation?
Xml代码
1.<!-- Log4J 配置 -->
2. <context-param>
3. <param-name>log4jConfigLocation</param-name>
4. <param-value>classpath:log4j.xml</param-value>
5. </context-param>
6. <context-param>
7. <param-name>log4fRefreshInterval</param-name>
8. <param-value>60000</param-value>
9. </context-param>
Xml代码
1.<!-- Log4J 配置 -->
2. <context-param>
3. <param-name>log4jConfigLocation</param-name>
4. <param-value>classpath:log4j.xml</param-value>
5. </context-param>
6. <context-param>
7. <param-name>log4fRefreshInterval</param-name>
8. <param-value>60000</param-value>
9. </context-param>
<!-- Log4J 配置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4fRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
这是一种基本配置,spring中很多代码使用了不同的日志接口,既有log4j也有commons-logging,这里只是强制转换为log4j!并且,log4j的配置文件只能放在classpath根路径。同时,需要通过commons-logging配置将日志控制权转交给log4j。同时commons-logging.properties必须放置在classpath根路径。
commons-logging内容:
Properties代码
1.org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
Properties代码
1.org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
最后,记得配置log4j的监听器:
Xml代码
1.<listener>
2. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
3.</listener>
Xml代码
1.<listener>
2. <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
3.</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
接下来,我们需要配置两套配置文件,applicationContext.xml和servlet.xml。
applicationContext.xml用于对应用层面做整体控制。按照分层思想,统领service层和dao层。servlet.xml则单纯控制controller层。
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <import
9. resource="service.xml" />
10. <import
11. resource="dao.xml" />
12.</beans>
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <import
9. resource="service.xml" />
10. <import
11. resource="dao.xml" />
12.</beans>
<?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分别用于业务、数据处理。
service.xml
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <context:component-scan
9. base-package="org.zlex.spring.service" />
10.</beans>
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <context:component-scan
9. base-package="org.zlex.spring.service" />
10.</beans>
<?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">
<context:component-scan
base-package="org.zlex.spring.service" />
</beans>
注意,这里通过<context:component-scan />标签指定了业务层的基础包路径——“org.zlex.spring.service”。也就是说,业务层相关实现均在这一层。这是有必要的分层之一。
dao.xml
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <context:component-scan
13. base-package="org.zlex.spring.dao" />
14.</beans>
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <context:component-scan
13. base-package="org.zlex.spring.dao" />
14.</beans>
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan
base-package="org.zlex.spring.dao" />
</beans>
dao层如法炮制,包路径是"org.zlex.spring.dao"。从这个角度看,注解还是很方便的!
最后,我们看看servlet.xml
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <context:component-scan
9. base-package="org.zlex.spring.controller" />
10. <bean
11. id="urlMapping"
12. class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
13. <bean
14. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
15.</beans>
Xml代码
1.<?xml version="1.0" encoding="UTF-8"?>
2.<beans
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. <context:component-scan
9. base-package="org.zlex.spring.controller" />
10. <bean
11. id="urlMapping"
12. class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
13. <bean
14. class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
15.</beans>
<?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">
<context:component-scan
base-package="org.zlex.spring.controller" />
<bean
id="urlMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>
包路径配置就不细说了,都是一个概念。最重要的时候后面两个配置,这将使得注解生效!
“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 <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
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. }
53.
54. /**
55. * @param password the password to set
56. */
57. public void setPassword(String password) {
58. this.password = password;
59. }
60.
61.
62.}
Java代码
1./**
2. * 2010-1-23
3. */
4.package org.zlex.spring.domain;
5.
6.import java.io.Serializable;
7.
8./**
9. *
10. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
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. }
53.
54. /**
55. * @param password the password to set
56. */
57. public void setPassword(String password) {
58. this.password = password;
59. }
60.
61.
62.}
/**
* 2010-1-23
*/
package org.zlex.spring.domain;
import java.io.Serializable;
/**
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
public class Account implements Serializable {
/**
*
*/
private static final long serialVersionUID = -533698031946372178L;
private String username;
private String password;
/**
* @param username
* @param password
*/
public Account(String username, String password) {
this.username = username;
this.password = password;
}
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
}
通常,在构建域对象时,需要考虑该对象可能需要进行网络传输,本地缓存,因此建议实现序列化接口Serializable
我们再来看看控制器,这就稍微复杂了一点代码如下所示:
AccountController .java
Java代码
1./**
2. * 2010-1-23
3. */
4.package org.zlex.spring.controller;
5.
6.import javax.servlet.http.HttpServletRequest;
7.import javax.servlet.http.HttpServletResponse;
8.
9.import org.springframework.beans.factory.annotation.Autowired;
10.import org.springframework.stereotype.Controller;
11.import org.springframework.web.bind.ServletRequestUtils;
12.import org.springframework.web.bind.annotation.RequestMapping;
13.import org.springframework.web.bind.annotation.RequestMethod;
14.import org.zlex.spring.service.AccountService;
15.
16./**
17. *
18. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
19. * @version 1.0
20. * @since 1.0
21. */
22.@Controller
23.@RequestMapping("/account.do")
24.public class AccountController {
25.
26. @Autowired
27. private AccountService accountService;
28.
29. @RequestMapping(method = RequestMethod.GET)
30. public void hello(HttpServletRequest request, HttpServletResponse response)
31. throws Exception {
32.
33. String username = ServletRequestUtils.getRequiredStringParameter(
34. request, "username");
35. String password = ServletRequestUtils.getRequiredStringParameter(
36. request, "password");
37. System.out.println(accountService.verify(username, password));
38. }
39.}
Java代码
1./**
2. * 2010-1-23
3. */
4.package org.zlex.spring.controller;
5.
6.import javax.servlet.http.HttpServletRequest;
7.import javax.servlet.http.HttpServletResponse;
8.
9.import org.springframework.beans.factory.annotation.Autowired;
10.import org.springframework.stereotype.Controller;
11.import org.springframework.web.bind.ServletRequestUtils;
12.import org.springframework.web.bind.annotation.RequestMapping;
13.import org.springframework.web.bind.annotation.RequestMethod;
14.import org.zlex.spring.service.AccountService;
15.
16./**
17. *
18. * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
19. * @version 1.0
20. * @since 1.0
21. */
22.@Controller
23.@RequestMapping("/account.do")
24.public class AccountController {
25.
26. @Autowired
27. private AccountService accountService;
28.
29. @RequestMapping(method = RequestMethod.GET)
30. public void hello(HttpServletRequest request, HttpServletResponse response)
31. throws Exception {
32.
33. String username = ServletRequestUtils.getRequiredStringParameter(
34. request, "username");
35. String password = ServletRequestUtils.getRequiredStringParameter(
36. request, "password");
37. System.out.println(accountService.verify(username, password));
38. }
39.}
/**
* 2010-1-23
*/
package org.zlex.spring.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.zlex.spring.service.AccountService;
/**
*
* @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>
* @version 1.0
* @since 1.0
*/
@Controller
@RequestMapping("/account.do")
public class AccountController {
@Autowired
private AccountService accountService;
@RequestMapping(method = RequestMethod.GET)
public void hello(HttpServletRequest request, HttpServletResponse response)
throws Exception {
String username = ServletRequestUtils.getRequiredStringParameter(
request, "username");
String password = ServletRequestUtils.getRequiredStringParameter(
request, "password");
System.out.println(accountService.verify(username, password));
}
}
分段详述:
Java代码
1.@Controller
2.@RequestMapping("/account.do")
Java代码
1.@Controller
2.@RequestMapping("/account.do")
@Controller
@RequestMapping("/account.do")
这两行注解,@Controller是告诉Spring容器,这是一个控制器类,@RequestMapping("/account.do")是来定义该控制器对应的请求路径(/account.do)
Java代码
- spring注解1.rar (2.2 MB)
- 下载次数: 95
相关推荐
Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于注解实例Spring MVC 基于...
本实例将深入探讨Spring中的注解使用,特别是如何创建一个最简单的Spring注解实例。 首先,我们需要了解Spring的核心组件——Spring容器,也称为ApplicationContext。这个容器负责管理应用程序中的bean,包括它们的...
Spring框架是Java开发中广泛应用的一个轻量级容器,它的核心特性之一就是依赖注入(Dependency Injection,简称DI),而注解注入则是实现DI的一种便捷方式。本文将深入探讨Spring注解注入的相关知识点,以及如何通过...
IT学习者Spring MVC注解实例.pdf
1. `@Controller` 注解:这个注解用于标记一个类作为Spring MVC的控制器。当Spring容器启动时,会扫描带有@Controller的类,并将这些类实例化,用于处理HTTP请求。例如: ```java @Controller public class ...
总的来说,这个入门实例旨在帮助初学者理解如何在没有使用注解的情况下,通过XML配置文件集成SpringMVC、Spring和Hibernate,完成一个简单的Web应用。虽然现在的最佳实践倾向于使用注解和Spring Boot,但理解非注解...
总的来说,这个"spring mvc 注解实例"项目为你提供了一个学习Spring MVC基本功能的起点,包括注解驱动的控制器、拦截器的使用以及数据库操作。通过运行提供的代码,你可以亲自体验和理解这些概念,从而更好地掌握...
Spring 3.0 MVC 是一个强大的Java框架,用于构建企业级Web应用程序。它以其模块化、松耦合和高度可配置性而闻名。本自学教程集合了多种资源,旨在帮助初学者逐步掌握Spring 3.0 MVC的核心概念,并通过实际项目实例...
3. `<mvc:annotation-driven>`:这是 Spring MVC 的一个重要注解,它启用了 Spring MVC 对注解的支持,例如 `@RequestMapping`, `@RequestParam`, `@PathVariable` 等,使得我们可以使用注解来处理请求映射和参数...
对spring mvc注解的实例
7. **学习资源**: 这个实例对于初学者来说是一个很好的起点,它展示了一个完整的Spring MVC应用的基本构造,并且使用了现代的注解驱动方式。通过这个例子,初学者可以了解Spring MVC的基本流程,以及如何将注解应用...
在Spring框架中,注解是实现依赖注入和配置的核心机制之一。本文将深入探讨Spring注解的基本原理,包括它们如何被解析、处理以及如何影响应用程序的生命周期。 首先,我们需要了解注解在Java语言中的本质。注解是一...
spring3整合EhCache注解实例
本例子就是一个使用 Spring AOP 注解实现的项目,旨在展示如何在简单的环境中应用这一特性。 1. **AOP 基础概念** - **切面(Aspect)**: 包含一组相关功能的模块,这些功能在多个对象中都可能被用到,比如日志、...
在Spring框架中,自动检测注解(Autowired)是核心特性之一,它极大地简化了依赖注入的过程,使得开发者能够更加专注于业务逻辑,而不是繁琐的配置。本文将深入探讨Spring中的自动检测注解及其工作原理。 首先,`@...
# Spring 3 注解详解 在 Spring 3 中,注解成为了主要的配置方式,极大地简化了应用程序的配置。本文将详细介绍几个关键的注解,包括 @Autowired、@Qualifier、@Resource 和 @PostConstruct,以及它们在实际开发中...
本教程将围绕"Spring3注解初级实例"进行详细讲解,帮助初学者了解如何在Spring3中使用注解进行开发。 首先,让我们了解一下什么是注解(Annotation)。注解是Java编程语言的一种元数据,它可以提供有关代码的额外...
SSH是Java Web开发中的一个经典框架组合,包括Struts、Hibernate和Spring三个核心组件。这个框架结合了MVC设计模式、对象关系映射以及依赖注入等技术,为开发者提供了高效且灵活的开发环境。 **Struts** 是一个开源...
在Spring框架中,面向切面编程(AOP)是一种强大的工具,它允许程序员定义横切关注点,如日志、事务管理、权限控制等,这些关注点可以被模块化并独立于业务逻辑进行处理。本篇文章将深入探讨如何通过Spring的注解...
在这个实例中,我们将重点讨论如何使用Spring MVC的注解进行数据的增删改操作,并结合Hibernate作为ORM(对象关系映射)工具。 首先,`@Controller`注解标记了一个类作为Spring MVC的控制器,负责处理HTTP请求。在...