近來工作發(fā)生了一些變化,有必要學(xué)習(xí)一下Spring注解了!
網(wǎng)上找了一些個(gè)例子,總的說來比較土,大多數(shù)是轉(zhuǎn)載摘抄,按照提示弄下來根本都運(yùn)行不了,索性自己趟一遍這渾水,在這里留下些個(gè)印記。
這次,先來構(gòu)建一個(gè)極為簡(jiǎn)單的web應(yīng)用,從controller到dao。不考慮具體實(shí)現(xiàn),只是先對(duì)整體架構(gòu)有一個(gè)清晰的了解。日后在分層細(xì)述每一層的細(xì)節(jié)。
相關(guān)參考:
Spring 注解學(xué)習(xí)手札(一) 構(gòu)建簡(jiǎn)單Web應(yīng)用 Spring 注解學(xué)習(xí)手札(二) 控制層梳理 Spring 注解學(xué)習(xí)手札(三) 表單頁面處理 Spring 注解學(xué)習(xí)手札(四) 持久層淺析 Spring 注解學(xué)習(xí)手札(五) 業(yè)務(wù)層事務(wù)處理 Spring 注解學(xué)習(xí)手札(六) 測(cè)試 我們將用到如下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 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>
-
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:log4j.xml</param-value>
- </context-param>
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
-
- <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>
-
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
-
- <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>
有不少人問我,這段代碼是什么:
-
- <context-param>
- <param-name>webAppRootKey</param-name>
- <param-value>spring.webapp.root</param-value>
- </context-param>
這是當(dāng)前應(yīng)用的路徑變量,也就是說你可以在其他代碼中使用${spring.webapp.root}指代當(dāng)前應(yīng)用路徑。我經(jīng)常用它來設(shè)置log的輸出目錄。
為什么要設(shè)置參數(shù)log4jConfigLocation?
-
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:log4j.xml</param-value>
- </context-param>
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
這是一種基本配置,spring中很多代碼使用了不同的日志接口,既有l(wèi)og4j也有commons-logging,這里只是強(qiáng)制轉(zhuǎn)換為log4j!并且,log4j的配置文件只能放在classpath根路徑。同時(shí),需要通過commons-logging配置將日志控制權(quán)轉(zhuǎn)交給log4j。同時(shí)commons-logging.properties必須放置在classpath根路徑。
commons-logging內(nèi)容:
- org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
最后,記得配置log4j的監(jiān)聽器:
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
接下來,我們需要配置兩套配置文件,applicationContext.xml和servlet.xml。
applicationContext.xml用于對(duì)應(yīng)用層面做整體控制。按照分層思想,統(tǒng)領(lǐng)service層和dao層。servlet.xml則單純控制controller層。
- <?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什么都不干,它只管涉及到整體需要的配置,并且集中管理。
這里引入了兩個(gè)配置文件service.xml和dao.xml分別用于業(yè)務(wù)、數(shù)據(jù)處理。
service.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">
- <context:component-scan
- base-package="org.zlex.spring.service" />
- </beans>
注意,這里通過<context:component-scan />標(biāo)簽指定了業(yè)務(wù)層的基礎(chǔ)包路徑——“org.zlex.spring.service”。也就是說,業(yè)務(wù)層相關(guān)實(shí)現(xiàn)均在這一層。這是有必要的分層之一。
dao.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: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"。從這個(gè)角度看,注解還是很方便的!
最后,我們看看servlet.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">
- <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>
包路徑配置就不細(xì)說了,都是一個(gè)概念。最重要的時(shí)候后面兩個(gè)配置,這將使得注解生效!
“org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping”是默認(rèn)實(shí)現(xiàn),可以不寫,Spring容器默認(rèn)會(huì)默認(rèn)使用該類。
“org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter”直接關(guān)系到多動(dòng)作控制器配置是否可用! 簡(jiǎn)單看一下代碼結(jié)構(gòu),如圖:
Account類是來存儲(chǔ)賬戶信息,屬于域?qū)ο?,極為簡(jiǎn)單,代碼如下所示:
Account.java
-
-
-
- package org.zlex.spring.domain;
-
- import java.io.Serializable;
-
-
-
-
-
-
-
- public class Account implements Serializable {
-
-
-
-
- private static final long serialVersionUID = -533698031946372178L;
-
- private String username;
- private String password;
-
-
-
-
-
- public Account(String username, String password) {
- this.username = username;
- this.password = password;
- }
-
-
-
-
- public String getUsername() {
- return username;
- }
-
-
-
-
- public void setUsername(String username) {
- this.username = username;
- }
-
-
-
-
- public String getPassword() {
- return password;
- }
-
-
-
-
- public void setPassword(String password) {
- this.password = password;
- }
-
-
- }
通常,在構(gòu)建域?qū)ο髸r(shí),需要考慮該對(duì)象可能需要進(jìn)行網(wǎng)絡(luò)傳輸,本地緩存,因此建議實(shí)現(xiàn)序列化接口Serializable
我們?cè)賮砜纯纯刂破?,這就稍微復(fù)雜了一點(diǎn)代碼如下所示:
AccountController .java
-
-
-
- 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;
-
-
-
-
-
-
-
- @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));
- }
- }
分段詳述:
- @Controller
- @RequestMapping("/account.do")
這兩行注解,
@Controller是告訴Spring容器,這是一個(gè)控制器類,
@RequestMapping("/account.do")是來定義該控制器對(duì)應(yīng)的請(qǐng)求路徑(/account.do)
- @Autowired
- private AccountService accountService;
這是用來自動(dòng)織入業(yè)務(wù)層實(shí)現(xiàn)AccountService,有了這個(gè)注解,我們就可以不用寫setAccountService()方法了!
同時(shí),JSR-250標(biāo)準(zhǔn)注解,推薦使用
@Resource來代替Spring專有的@Autowired注解。
引用
Spring 不但支持自己定義的@Autowired注解,還支持幾個(gè)由JSR-250規(guī)范定義的注解,它們分別是@Resource、@PostConstruct以及@PreDestroy。
@Resource的作用相當(dāng)于@Autowired,只不過@Autowired按byType自動(dòng)注入,而@Resource默認(rèn)按 byName自動(dòng)注入罷了。@Resource有兩個(gè)屬性是比較重要的,分別是name和type,Spring將@Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。所以如果使用name屬性,則使用byName的自動(dòng)注入策略,而使用type屬性時(shí)則使用byType自動(dòng)注入策略。如果既不指定name也不指定type屬性,這時(shí)將通過反射機(jī)制使用byName自動(dòng)注入策略。
@Resource裝配順序
1. 如果同時(shí)指定了name和type,則從Spring上下文中找到唯一匹配的bean進(jìn)行裝配,找不到則拋出異常
2. 如果指定了name,則從上下文中查找名稱(id)匹配的bean進(jìn)行裝配,找不到則拋出異常
3. 如果指定了type,則從上下文中找到類型匹配的唯一bean進(jìn)行裝配,找不到或者找到多個(gè),都會(huì)拋出異常
4. 如果既沒有指定name,又沒有指定type,則自動(dòng)按照byName方式進(jìn)行裝配(見2);如果沒有匹配,則回退為一個(gè)原始類型(UserDao)進(jìn)行匹配,如果匹配則自動(dòng)裝配;
1.6. @PostConstruct(JSR-250)
在方法上加上注解@PostConstruct,這個(gè)方法就會(huì)在Bean初始化之后被Spring容器執(zhí)行(注:Bean初始化包括,實(shí)例化Bean,并裝配Bean的屬性(依賴注入))。
這有點(diǎn)像ORM最終被JPA一統(tǒng)天下的意思!
大家知道就可以了,具體使用何種標(biāo)準(zhǔn)由項(xiàng)目說了算!
最后,來看看核心方法:
- @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));
- }
注解@RequestMapping(method = RequestMethod.GET)指定了訪問方法類型。
注意,如果沒有用這個(gè)注解標(biāo)識(shí)方法,Spring容器將不知道那個(gè)方法可以用于處理get請(qǐng)求!
對(duì)于方法名,我們可以隨意定!方法中的參數(shù),類似于“HttpServletRequest request, HttpServletResponse response”,只要你需要方法可以是有參也可以是無參!
解析來看Service層,分為接口和實(shí)現(xiàn):
AccountService.java
-
-
-
- package org.zlex.spring.service;
-
-
-
-
-
-
-
- public interface AccountService {
-
-
-
-
-
-
-
-
- boolean verify(String username, String password);
-
- }
接口不需要任何Spring注解相關(guān)的東西,它就是一個(gè)簡(jiǎn)單的接口!
重要的部分在于實(shí)現(xiàn)層,如下所示:
AccountServiceImpl.java
-
-
-
- package org.zlex.spring.service.impl;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.zlex.spring.dao.AccountDao;
- import org.zlex.spring.domain.Account;
- import org.zlex.spring.service.AccountService;
-
-
-
-
-
-
-
- @Service
- @Transactional
- public class AccountServiceImpl implements AccountService {
-
- @Autowired
- private AccountDao accountDao;
-
-
-
-
-
-
-
- @Override
- public boolean verify(String username, String password) {
-
- Account account = accountDao.read(username);
-
- if (password.equals(account.getPassword())) {
- return true;
- } else {
- return false;
- }
- }
-
- }
注意以下內(nèi)容:
注解@Service用于標(biāo)識(shí)這是一個(gè)Service層實(shí)現(xiàn),@Transactional用于控制事務(wù),將事務(wù)定位在業(yè)務(wù)層,這是非常務(wù)實(shí)的做法!
接下來,我們來看持久層:AccountDao和AccountDaoImpl類
AccountDao.java
-
-
-
- package org.zlex.spring.dao;
-
- import org.zlex.spring.domain.Account;
-
-
-
-
-
-
-
- public interface AccountDao {
-
-
-
-
-
-
-
- Account read(String username);
-
- }
這個(gè)接口就是簡(jiǎn)單的數(shù)據(jù)提取,無需任何Spring注解有關(guān)的東西!
再看其實(shí)現(xiàn)類:
AccountDaoImpl.java
-
-
-
- package org.zlex.spring.dao.impl;
-
- import org.springframework.stereotype.Repository;
- import org.zlex.spring.dao.AccountDao;
- import org.zlex.spring.domain.Account;
-
-
-
-
-
-
-
- @Repository
- public class AccountDaoImpl implements AccountDao {
-
-
-
-
- @Override
- public Account read(String username) {
-
- return new Account(username,"wolf");
- }
-
- }
這里只需要注意注解:
意為持久層,Dao實(shí)現(xiàn)這層我沒有過于細(xì)致的介紹通過注解調(diào)用ORM或是JDBC來完成實(shí)現(xiàn),這些內(nèi)容后續(xù)細(xì)述!
這里我們沒有提到注解
@Component,共有4種“組件”式注解:
引用
@Component:可裝載組件
@Repository:持久層組件
@Service:服務(wù)層組件
@Controller:控制層組件
這樣spring容器就完成了控制層、業(yè)務(wù)層和持久層的構(gòu)建。
啟動(dòng)應(yīng)用,訪問
http://localhost:8080/spring/account.do?username=snow&password=wolf 觀察控制臺(tái),如果得到包含“true”的輸出,本次構(gòu)建就成功了!
代碼見附件!
順便說一句:在Spring之前的XML配置中,如果你想在一個(gè)類中獲得文件可以通過在xml配置這個(gè)類的某個(gè)屬性。在注解的方式(Spring3.0)中,你可以使用
@Value來指定這個(gè)文件。
例如,我們想要在一個(gè)類中獲得一個(gè)文件,可以這樣寫:
- @Value("/WEB-INF/database.properties")
- private File databaseConfig;
如果這個(gè)properties文件已經(jīng)正常在容器中加載,可以直接這樣寫:
- @Value("${jdbc.url}")
- private String url;
獲得這個(gè)url參數(shù)!
容器中加載這個(gè)Properties文件:
- <util:properties id="jdbc" location="/WEB-INF/database.properties"/>
這樣,我們就能通過注解
@Value獲得
/WEB-INF/database.properties這個(gè)文件!
如果我們想要獲得注入在xml中的某個(gè)類,例如dataSource(<bean id ="dataSource">)可以在注解的類中這么寫:
- @Resource(name = "dataSource")
- private BasicDataSource dataSource;
如果只有這么一個(gè)類使用該配置文件:
- @ImportResource("/WEB-INF/database.properties")
- public class AccountDaoImpl extends AccountDao {
相關(guān)參考:
Spring 注解學(xué)習(xí)手札(一) 構(gòu)建簡(jiǎn)單Web應(yīng)用 Spring 注解學(xué)習(xí)手札(二) 控制層梳理 Spring 注解學(xué)習(xí)手札(三) 表單頁面處理 Spring 注解學(xué)習(xí)手札(四) 持久層淺析 Spring 注解學(xué)習(xí)手札(五) 業(yè)務(wù)層事務(wù)處理 Spring 注解學(xué)習(xí)手札(六) 測(cè)試