分享

Spring 注解学习手札

 用勿龍潛 2012-03-23
近来工作发生了一些变化,有必要学习一下Spring注解了!
网上找了一些个例子,总的说来比较土,大多数是转载摘抄,按照提示弄下来根本都运行不了,索性自己趟一遍这浑水,在这里留下些个印记。
这次,先来构建一个极为简单的web应用,从controller到dao。不考虑具体实现,只是先对整体架构有一个清晰的了解。日后在分层细述每一层的细节。

相关参考:
Spring 注解学习手札(一) 构建简单Web应用
Spring 注解学习手札(二) 控制层梳理
Spring 注解学习手札(三) 表单页面处理
Spring 注解学习手札(四) 持久层浅析
Spring 注解学习手札(五) 业务层事务处理
Spring 注解学习手札(六) 测试


我们将用到如下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./2001/XMLSchema-instance"  
  4.     xmlns="http://java./xml/ns/javaee"  
  5.     xmlns:web="http://java./xml/ns/javaee/web-app_2_5.xsd"  
  6.     xsi:schemaLocation="http://java./xml/ns/javaee http://java./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>log4jRefreshInterval</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. <!-- 应用路径 -->  
  2. <context-param>  
  3.     <param-name>webAppRootKey</param-name>  
  4.     <param-value>spring.webapp.root</param-value>  
  5. </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>log4jRefreshInterval</param-name>  
  8.         <param-value>60000</param-value>  
  9.     </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  

最后,记得配置log4j的监听器:
Xml代码 复制代码 收藏代码
  1. <listener>  
  2.     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  3. </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./schema/beans"  
  4.     xmlns:xsi="http://www./2001/XMLSchema-instance"  
  5.     xmlns:context="http://www./schema/context"  
  6.     xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans.xsd   
  7.         http://www./schema/context http://www./schema/context/spring-context.xsd">  
  8.     <import  
  9.         resource="service.xml" />  
  10.     <import  
  11.         resource="dao.xml" />  
  12. </beans>  

applicationContext.xml什么都不干,它只管涉及到整体需要的配置,并且集中管理。
这里引入了两个配置文件service.xml和dao.xml分别用于业务、数据处理。
service.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www./schema/beans"  
  4.     xmlns:xsi="http://www./2001/XMLSchema-instance"  
  5.     xmlns:context="http://www./schema/context"  
  6.     xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans.xsd   
  7.         http://www./schema/context http://www./schema/context/spring-context.xsd">  
  8.     <context:component-scan  
  9.         base-package="org.zlex.spring.service" />  
  10. </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./schema/beans"  
  4.     xmlns:xsi="http://www./2001/XMLSchema-instance"  
  5.     xmlns:aop="http://www./schema/aop"  
  6.     xmlns:context="http://www./schema/context"  
  7.     xmlns:tx="http://www./schema/tx"  
  8.     xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans.xsd   
  9.         http://www./schema/aop http://www./schema/aop/spring-aop.xsd   
  10.         http://www./schema/context http://www./schema/context/spring-context.xsd   
  11.         http://www./schema/tx http://www./schema/tx/spring-tx.xsd">  
  12.     <context:component-scan  
  13.         base-package="org.zlex.spring.dao" />  
  14. </beans>  

dao层如法炮制,包路径是"org.zlex.spring.dao"。从这个角度看,注解还是很方便的!
最后,我们看看servlet.xml
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www./schema/beans"  
  4.     xmlns:xsi="http://www./2001/XMLSchema-instance"  
  5.     xmlns:context="http://www./schema/context"  
  6.     xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans.xsd   
  7.         http://www./schema/context http://www./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>  

包路径配置就不细说了,都是一个概念。最重要的时候后面两个配置,这将使得注解生效!
“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. }  

通常,在构建域对象时,需要考虑该对象可能需要进行网络传输,本地缓存,因此建议实现序列化接口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. @Controller  
  2. @RequestMapping("/account.do")  

这两行注解,@Controller是告诉Spring容器,这是一个控制器类,@RequestMapping("/account.do")是来定义该控制器对应的请求路径(/account.do)
Java代码 复制代码 收藏代码
  1. @Autowired  
  2. private AccountService accountService;  

这是用来自动织入业务层实现AccountService,有了这个注解,我们就可以不用写setAccountService()方法了!
同时,JSR-250标准注解,推荐使用@Resource来代替Spring专有的@Autowired注解。
引用
Spring 不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。

  @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分别是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

  @Resource装配顺序

  1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常

  2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常

  3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常

  4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;

  1.6. @PostConstruct(JSR-250)

在方法上加上注解@PostConstruct,这个方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的属性(依赖注入))。


这有点像ORM最终被JPA一统天下的意思! 大家知道就可以了,具体使用何种标准由项目说了算!
最后,来看看核心方法:
Java代码 复制代码 收藏代码
  1. @RequestMapping(method = RequestMethod.GET)   
  2. public void hello(HttpServletRequest request, HttpServletResponse response)   
  3.         throws Exception {   
  4.   
  5.     String username = ServletRequestUtils.getRequiredStringParameter(   
  6.             request, "username");   
  7.     String password = ServletRequestUtils.getRequiredStringParameter(   
  8.             request, "password");   
  9.     System.out.println(accountService.verify(username, password));   
  10. }  

注解@RequestMapping(method = RequestMethod.GET)指定了访问方法类型。
注意,如果没有用这个注解标识方法,Spring容器将不知道那个方法可以用于处理get请求!
对于方法名,我们可以随意定!方法中的参数,类似于“HttpServletRequest request, HttpServletResponse response”,只要你需要方法可以是有参也可以是无参!
解析来看Service层,分为接口和实现:
AccountService.java
Java代码 复制代码 收藏代码
  1. /**  
  2.  * 2010-1-23  
  3.  */  
  4. package org.zlex.spring.service;   
  5.   
  6. /**  
  7.  *   
  8.  * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>  
  9.  * @version 1.0  
  10.  * @since 1.0  
  11.  */  
  12. public interface AccountService {   
  13.   
  14.     /**  
  15.      * 验证用户身份  
  16.      *   
  17.      * @param username  
  18.      * @param password  
  19.      * @return  
  20.      */  
  21.     boolean verify(String username, String password);   
  22.   
  23. }  

接口不需要任何Spring注解相关的东西,它就是一个简单的接口!
重要的部分在于实现层,如下所示:
AccountServiceImpl.java
Java代码 复制代码 收藏代码
  1. /**  
  2.  * 2010-1-23  
  3.  */  
  4. package org.zlex.spring.service.impl;   
  5.   
  6. import org.springframework.beans.factory.annotation.Autowired;   
  7. import org.springframework.stereotype.Service;   
  8. import org.springframework.transaction.annotation.Transactional;   
  9. import org.zlex.spring.dao.AccountDao;   
  10. import org.zlex.spring.domain.Account;   
  11. import org.zlex.spring.service.AccountService;   
  12.   
  13. /**  
  14.  *   
  15.  * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>  
  16.  * @version 1.0  
  17.  * @since 1.0  
  18.  */  
  19. @Service  
  20. @Transactional  
  21. public class AccountServiceImpl implements AccountService {   
  22.   
  23.     @Autowired  
  24.     private AccountDao accountDao;   
  25.   
  26.     /*  
  27.      * (non-Javadoc)  
  28.      *   
  29.      * @see org.zlex.spring.service.AccountService#verify(java.lang.String,  
  30.      * java.lang.String)  
  31.      */  
  32.     @Override  
  33.     public boolean verify(String username, String password) {   
  34.   
  35.         Account account = accountDao.read(username);   
  36.   
  37.         if (password.equals(account.getPassword())) {   
  38.             return true;   
  39.         } else {   
  40.             return false;   
  41.         }   
  42.     }   
  43.   
  44. }  

注意以下内容:
Java代码 复制代码 收藏代码
  1. @Service  
  2. @Transactional  

注解@Service用于标识这是一个Service层实现,@Transactional用于控制事务,将事务定位在业务层,这是非常务实的做法!
接下来,我们来看持久层:AccountDao和AccountDaoImpl类
AccountDao.java
Java代码 复制代码 收藏代码
  1. /**  
  2.  * 2010-1-23  
  3.  */  
  4. package org.zlex.spring.dao;   
  5.   
  6. import org.zlex.spring.domain.Account;   
  7.   
  8. /**  
  9.  *   
  10.  * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>  
  11.  * @version 1.0  
  12.  * @since 1.0  
  13.  */  
  14. public interface AccountDao {   
  15.   
  16.     /**  
  17.      * 读取用户信息  
  18.      *   
  19.      * @param username  
  20.      * @return  
  21.      */  
  22.     Account read(String username);   
  23.   
  24. }  

这个接口就是简单的数据提取,无需任何Spring注解有关的东西!
再看其实现类:
AccountDaoImpl.java
Java代码 复制代码 收藏代码
  1. /**  
  2.  * 2010-1-23  
  3.  */  
  4. package org.zlex.spring.dao.impl;   
  5.   
  6. import org.springframework.stereotype.Repository;   
  7. import org.zlex.spring.dao.AccountDao;   
  8. import org.zlex.spring.domain.Account;   
  9.   
  10. /**  
  11.  *   
  12.  * @author <a href="mailto:zlex.dongliang@gmail.com">梁栋</a>  
  13.  * @version 1.0  
  14.  * @since 1.0  
  15.  */  
  16. @Repository  
  17. public class AccountDaoImpl implements AccountDao {   
  18.   
  19.     /* (non-Javadoc)  
  20.      * @see org.zlex.spring.dao.AccountDao#read(java.lang.String)  
  21.      */  
  22.     @Override  
  23.     public Account read(String username) {   
  24.         
  25.         return new Account(username,"wolf");   
  26.     }   
  27.   
  28. }  

这里只需要注意注解:
Java代码 复制代码 收藏代码
  1. @Repository  

意为持久层,Dao实现这层我没有过于细致的介绍通过注解调用ORM或是JDBC来完成实现,这些内容后续细述!
这里我们没有提到注解@Component,共有4种“组件”式注解:
引用
 
@Component:可装载组件 
@Repository:持久层组件 
@Service:服务层组件 
@Controller:控制层组件

这样spring容器就完成了控制层、业务层和持久层的构建。
启动应用,访问http://localhost:8080/spring/account.do?username=snow&password=wolf
观察控制台,如果得到包含“true”的输出,本次构建就成功了!

代码见附件!

顺便说一句:在Spring之前的XML配置中,如果你想在一个类中获得文件可以通过在xml配置这个类的某个属性。在注解的方式(Spring3.0)中,你可以使用@Value来指定这个文件。
例如,我们想要在一个类中获得一个文件,可以这样写:
Java代码 复制代码 收藏代码
  1. @Value("/WEB-INF/database.properties")   
  2. private File databaseConfig;  


如果这个properties文件已经正常在容器中加载,可以直接这样写:

Java代码 复制代码 收藏代码
  1. @Value("${jdbc.url}")    
  2. private String url;  

获得这个url参数! 

容器中加载这个Properties文件:
Xml代码 复制代码 收藏代码
  1. <util:properties id="jdbc" location="/WEB-INF/database.properties"/>  


这样,我们就能通过注解@Value获得/WEB-INF/database.properties这个文件!
如果我们想要获得注入在xml中的某个类,例如dataSource(<bean id ="dataSource">)可以在注解的类中这么写:
Java代码 复制代码 收藏代码
  1. @Resource(name = "dataSource")   
  2. private BasicDataSource dataSource;  


如果只有这么一个类使用该配置文件:

Java代码 复制代码 收藏代码
  1. @ImportResource("/WEB-INF/database.properties")   
  2. public class AccountDaoImpl extends AccountDao {  


相关参考:
Spring 注解学习手札(一) 构建简单Web应用
Spring 注解学习手札(二) 控制层梳理
Spring 注解学习手札(三) 表单页面处理
Spring 注解学习手札(四) 持久层浅析
Spring 注解学习手札(五) 业务层事务处理
Spring 注解学习手札(六) 测试
  • 大小: 74 KB

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多