Spring經常使用配置示例 Spring 是一款Java平臺的開源框架,是爲解決企業級應用程序開發的複雜性而建立的,經過良好的分層架構讓開發人員可以專一於業務邏輯的開發。 Spring框架是一個分層架構,由不一樣的模塊組成,構成spring的每一個組件或模塊均可以單獨使用或者多個模塊配合使用,以實現不一樣的功能需求。Spring框架的模塊結構以下圖所示: SpringCore是Spring框架的核心模塊,提供spring框架的基本功能,使用工廠模式BeanFactory經過控制反轉(IoC)、依賴注入(DI)等實現對beans的管理功能,將對象間的耦合關係經過配置文件進行管理,實現了「低耦合」的程序設計原則。 SpringContext經過配置文件的方式向spring提供上下文服務,如JNDI、國際化、校驗等 SpringDAO是spring框架對數據訪問的抽象,封裝了對JDBC的操做,統一了異常結構用於管理不一樣數據庫廠商產品拋出的錯誤信息,簡化了對異常信息的處理。 SpringORM負責spring與ORM框架的集成,如Hibernate、MyBatis等。 SpringWeb是spring的Web模塊,提供WebApplication的上下文信息,實現如文件上傳、數據綁定、與其餘框架(如Struts)的集成等。 SpringWebMVC是一個Web的MVC框架,提供了對Controller、Model、Service等組件的管理,視圖層經過不一樣的視圖解析器支持多種視圖技術,如JSP、Velocity、FreeMarker等 SpringAOP是Spring對面向切面編程的支持,支持JDK和CGLib兩種字節碼操做方式,能夠實現對Spring管理的任意對象的AOP支持。 Spring框架的配置文件是基於xml的,Spring強大的功能依賴於類型繁多的配置項,這些配置項紛繁複雜難以記憶,下面將經常使用的配置項示例記錄下來,以備後續查看使用。 Spring配置------命名空間: 複製代碼 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" <!-- 默認bean命名空間 --> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <!-- 固定格式 --> xmlns:aop="http://www.springframework.org/schema/aop"<!-- AOP命名空間的scheme約束 --> xmlns:context="http://www.springframework.org/schema/context"<!-- context命名空間的scheme約束 --> xsi:schemaLocation=" <!-- 上面各個scheme的location信息 --> http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd "> </beans> 複製代碼 Spring Beans主要配置: 複製代碼 <bean class="bean的徹底限定名" name/id="bean在容器內的惟一名稱" scope="bean的生命週期" lazy-init="是否爲延遲加載" init-method="bean的setter被調用以後調用該方法進行初始化" destroy-method="容器在銷燬該Bean後的調用的方法" abstract="是否爲抽象Bean,spring對於抽象bean不產生實例,主要用於繼承" parent="父Bean的名稱,會繼承父Bean的屬性,與Java的Class無任何關係" factory-method="工廠方法的名字" factory-bean="工廠Bean的名字" depends-on ="依賴Bean的名字,保證初始化順序。」 > <!-- Constructor-arg給屬性賦值寫法一 --> <constructor-arg type="int" value="10"/> <!-- Constructor-arg給屬性賦值寫法二 --> <constructor-arg name="age" value="10"/> <!-- Constructor-arg給屬性賦值寫法三 --> <constructor-arg index="0" value="10"/> <!-- Properties給屬性賦值寫法一 --> <property name="bean1"> <ref bean="另一個bean的id"/> </property> <!-- Properties給屬性賦值寫法二 --> <property name="bean1" ref="另一個bean的id"/> <!-- Properties給屬性賦值寫法三 --> <property name="age" value="10"/> </bean> 複製代碼 Spring 配置------context: 自動掃描包(默認設置) <context:component-scan base-package="com.xxx.test" /> 自動掃描包(含過濾器) 複製代碼 <context:component-scan base-package="cn.xxx.test" > <!-- 根據註解(包含) --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/> <!-- 根據aspectJ語法,通常用於AOP --> <context:include-filter type="aspectj" expression=""/> <!-- 根據正則表達式(排除) --> <context:exclude-filter type="regex" expression=""/> </context:component-scan> 複製代碼 <!-- 註解支持 --> <context:annotation-config/> 激活Spring對class的註解檢測,該配置會向Spring 容器註冊一些BeanPostProcessor用於處理註解, 如:AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor 和 RequiredAnnotationBeanPostProcessor 好比要使用@AutoWired註解,須要向Spring註冊以下的bean : <bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/> 若是要使用@Required的註解,就必須註冊以下bean: <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 而使用context:annotation-config至關於簡化了操做。 另外若是配置了context:component-scan 則一樣具有了註解檢測的功能,此種狀況下能夠移除context:annotation-config Spring 配置------AOP: 一些基本概念: 方面(Aspect):即切面,一個關注點的模塊化,這個關注點實現可能另外橫切多個對象。 鏈接點(Joinpoint):程序執行過程當中明確的點,如方法的調用或特定的異常被拋出。 通知(Advice):在特定的鏈接點,AOP框架執行的動做。各類類型的通知包括「around」、「before」、「after」和「throws」通知。 切入點(Pointcut):指定一個通知將被引起的一系列鏈接點的集合。AOP框架必須容許開發者指定切入點,例如,使用正則表達式。 引入(Introduction):添加方法或字段到被通知的類。Spring容許引入新的接口到任何被通知的對象。 目標(Target):包含鏈接點的對象,也被稱做被通知或被代理對象。 代理(Proxy):AOP框架建立的對象,包含通知。在Spring中,AOP代理能夠是JDK動態代理或CGLIB代理。 編織(Weaving):組裝方面來建立一個被通知對象。這能夠在編譯時完成(例如使用AspectJ編譯器),也能夠在運行時完成。Spring和其餘純Java AOP框架同樣,在運行時完成織入。 AOP的xml配置項: 複製代碼 <bean id="myadvice" class="cn.test.MyAdvice" /> <bean id="targetclass" class="cn.test.aop.TargetClass" /> <aop:config> <aop:pointcut expression="execution(* cn.test.aop.*.*(..))" id="pt" /> <aop:aspect ref="myadvice"> <aop:before method="beforeAdvice" pointcut-ref="pt" /> <aop:after method="afterAdvice" pointcut-ref="pt" /> <aop:around method="aroundAdvice" pointcut-ref="pt"/> </aop:aspect> </aop:config> 複製代碼 使用註解的方式實現AOP(示例): 複製代碼 @Component @Aspect public class MyAdvice2 { //攔截cn.test.spring.aop包下全部類的全部方法 final String exp="execution(* cn.test.spring.aop.*.*(..))"; @Before(exp) public void beforeAdvice(){ System.out.println("before advice2"); } @After(exp) public void afterAdvice(){ System.out.println("after advice2"); } @AfterReturning(exp) public void afterRetAdvice(){ System.out.println("after return advice2"); } @Around(exp) public void aronudAdvice(ProceedingJoinPoint jp){ System.out.println("start arround advice2"); try { jp.proceed(); } catch (Throwable e) { e.printStackTrace(); } System.out.println("after arround advice2"); } } 複製代碼 Spring 配置------MVC: 對web.xml文件的配置項: 複製代碼 <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring Web配置 --> <listener> <listenerclass>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 指定Spring Bean的配置文件所在目錄。默認配置在WEB-INF目錄下 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param> 複製代碼 對spring配置文件的相關配置項: 複製代碼 <mvc:annotation-driven /> <context:component-scan base-package="cn.spring.test" /> <!-- 若是當前請求爲「/」時,則轉發到「index」視圖 --> <mvc:view-controller path="/" view-name="forward:index" /> <!-- 靜態資源映射 --> <mvc:resources mapping="/js/**" location="/WEB-INF/js/" /> <mvc:resources mapping="/css/**" location="/WEB-INF/css/" /> <mvc:resources mapping="/fonts/**" location="/WEB-INF/fonts/" /> <mvc:resources mapping="images/**" location="/WEB-INF/images/" /> <!-- 當上面要訪問的靜態資源不存在與上面的配置中時,則根據此配置來訪問 --> <mvc:default-servlet-handler /> <!-- 支持上傳文件 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize"> <value>xxx</value></property> <property name="defaultEncoding"> <value>UTF-8</value></property> </bean> <!-- jsp視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="/WEB-INF/jsps/" /> <property name="suffix" value=".jsp" /> </bean> 複製代碼 文件上傳示例代碼: 複製代碼 @RequestMapping("/upload") @ResponseBody public String fileUpload(@RequestParam("formFile") MultipartFile formFile) throws IOException { String fileContent=new String(formFile.getBytes()); //write to local file return "code:0"; }