1.概述
這是關于使用Spring 3.1和基于Java的配置來建立RESTfull Web應用程序的系列教程的第一篇。 本文將重點介紹如何引導Web應用程序 ,討論如何從XML過渡到Java,而不必完全遷移整個XML配置。
2. Maven
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org</groupId><artifactId>rest</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version><exclusions><exclusion><artifactId>commons-logging</artifactId><groupId>commons-logging</groupId></exclusion></exclusions></dependency><dependency><groupId>cglib</groupId><artifactId>cglib-nodep</artifactId><version>${cglib.version}</version><scope>runtime</scope></dependency></dependencies><build><finalName>rest</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.6</source><target>1.6</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build><properties><spring.version>3.2.2.RELEASE</spring.version><cglib.version>2.2.2</cglib.version></properties></project>
2.1。 cglib依賴關系的證明
您可能想知道為什么cglib是一個依賴項-事實證明有理由將其包含在其中-沒有它,整個配置將無法運行。 如果刪除,Spring將拋出:
原因:java.lang.IllegalStateException:處理@Configuration類需要CGLIB。 將CGLIB添加到類路徑或刪除以下@Configuration bean定義
Spring處理@Configuration類的方式解釋了發生這種情況的原因。 這些類實際上是bean,因此,它們需要了解Context,并尊重范圍和其他bean語義。 這是通過針對每個@Configuration類動態創建具有此意識的cglib代理來實現的,因此可以實現cglib依賴性。
此外,因此,對配置注釋類有一些限制:
- 配置類不應是最終的
- 他們應該有一個沒有參數的構造函數
2.2。 Spring 3.2中的cglib依賴項
從Spring 3.2開始, 不再需要將cglib添加為顯式依賴項 。 這是因為Spring現在正在內聯cglib –這將確保所有基于類的代理功能都可以在Spring 3.2中立即使用。
新的cglib代碼位于Spring包下: org.springframework.cglib (替換原始的net.sf.cglib )。 更改軟件包的原因是為了避免與類路徑上已經存在的任何cglib版本沖突。
另外,現在使用新的cglib 3.0,它是從較早的2.2依賴項升級的(有關更多詳細信息,請參見JIRA問題 )。
3.基于Java的Web配置
@Configuration
@ImportResource( { "classpath*:/rest_config.xml" } )
@ComponentScan( basePackages = "org.rest" )
@PropertySource({ "classpath:rest.properties", "classpath:web.properties" })
public class AppConfig{@Beanpublic static PropertySourcesPlaceholderConfigurer properties() {return new PropertySourcesPlaceholderConfigurer();}
}
首先, @Configuration批注–這是基于Java的Spring配置使用的主要工件。 它本身使用@Component進行元注釋,這使注釋的類成為標準bean ,因此也成為組件掃描的候選對象。 @Configuration類的主要目的是成為Spring IoC容器的bean定義的來源。 有關更詳細的描述,請參見官方文檔 。
然后, @ ImportResource用于導入基于XML的現有Spring配置。 這可能是仍在從XML遷移到Java的配置,或者只是您希望保留的傳統配置。 無論哪種方式,將其導入到容器對于成功遷移都是必不可少的,它允許很小的步驟而沒有太大的風險。 替換的等效XML注釋是:
<import resource =” classpath *:/ rest_config.xml” />
繼續@ComponentScan –這將配置組件掃描指令,有效地替換XML:
<context:component-scan base-package="org.rest" />
從Spring 3.1開始,默認情況下, @ Configuration不包括在類路徑掃描中,請參見JIRA問題 。 在Spring 3.1之前,這些類應明確排除在外:
excludeFilters = { @ComponentScan.Filter( Configuration.class ) }
@Configuration類不應被自動發現,因為它們已由Container指定并使用-允許重新發現它們并將其引入Spring上下文將導致以下錯誤:
由以下原因引起:org.springframework.context.annotation.ConflictingBeanDefinitionException:豆類[org.rest.spring.AppConfig]的由注釋指定的豆名稱'webConfig'與同名和類[org.net]的現有,不兼容的豆定義沖突。 rest.spring.AppConfig]
最后,使用@Bean批注配置屬性支持 – PropertySourcesPlaceholderConfigurer在@Bean批注的方法中初始化,指示它將產生由Container管理的Spring bean。 此新配置已替換以下XML:
<context:property-placeholder
location="classpath:persistence.properties, classpath:web.properties"
ignore-unresolvable="true"/>
有關為什么需要手動注冊PropertySourcesPlaceholderConfigurer bean的詳細討論,請參見帶有Spring教程的屬性 。
3.1。 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="rest" version="3.0"><context-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></context-param><context-param><param-name>contextConfigLocation</param-name><param-value>org.rest.spring.root</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>rest</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></init-param><init-param><param-name>contextConfigLocation</param-name><param-value>org.rest.spring.rest</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>rest</servlet-name><url-pattern>/api/*</url-pattern></servlet-mapping><welcome-file-list><welcome-file /></welcome-file-list></web-app>
首先,定義根上下文并將其配置為使用AnnotationConfigWebApplicationContext而不是默認的XmlWebApplicationContext 。 較新的AnnotationConfigWebApplicationContext接受帶注解的@Configuration的類作為Container配置的輸入,這是設置基于Java的上下文所必需的。
與XmlWebApplicationContext不同,它不假定默認配置類位置,因此必須設置Servlet的“ contextConfigLocation” init-param 。 這將指向@Configuration類所在的java包。 還支持類的完全限定名稱。
接下來,將DispatcherServlet配置為使用相同類型的上下文,唯一的區別是它從不同的包中加載配置類。
除此之外, web.xml并沒有真正從XML更改為基于Java的配置。
4。結論
提出的方法允許將Spring配置從XML平滑遷移到Java,同時將新舊混合在一起。 這對于較舊的項目很重要,因為較舊的項目可能具有許多基于XML的配置,無法一次全部遷移。 這樣,應用程序的web.xml和引導程序是遷移的第一步,之后可以以較小的增量移植其余的XML bean。
在關于REST with Spring的下一篇文章中 ,我將介紹如何在項目中設置MVC,HTTP狀態代碼的配置,有效負載編組和內容協商。 同時,您可以簽出github項目 。
翻譯自: https://www.javacodegeeks.com/2011/11/bootstrapping-web-application-with.html