javascript
使用Spring 3引导Web应用程序
1.概述
這是關(guān)于使用Spring 3.1和基于Java的配置來建立RESTfull Web應(yīng)用程序的系列教程的第一篇。 本文將重點(diǎn)介紹如何引導(dǎo)Web應(yīng)用程序 ,討論如何從XML過渡到Java,而不必完全遷移整個XML配置。
2. Maven
2.1。 cglib依賴關(guān)系的證明
您可能想知道為什么cglib是一個依賴項(xiàng)-事實(shí)證明有理由將其包含在其中-沒有它,整個配置將無法運(yùn)行。 如果刪除,Spring將拋出:
原因:java.lang.IllegalStateException:處理@Configuration類需要CGLIB。 將CGLIB添加到類路徑或刪除以下@Configuration bean定義
Spring處理@Configuration類的方式解釋了發(fā)生這種情況的原因。 這些類實(shí)際上是bean,因此,它們需要了解Context,并尊重范圍和其他bean語義。 這是通過針對每個@Configuration類動態(tài)創(chuàng)建具有此意識的cglib代理來實(shí)現(xiàn)的,因此可以實(shí)現(xiàn)cglib依賴性。
此外,因此,對配置注釋類有一些限制:
- 配置類不應(yīng)是最終的
- 他們應(yīng)該有一個沒有參數(shù)的構(gòu)造函數(shù)
2.2。 Spring 3.2中的cglib依賴項(xiàng)
從Spring 3.2開始, 不再需要將cglib添加為顯式依賴項(xiàng) 。 這是因?yàn)镾pring現(xiàn)在正在內(nèi)聯(lián)cglib –這將確保所有基于類的代理功能都可以在Spring 3.2中立即使用。
新的cglib代碼位于Spring包下: org.springframework.cglib (替換原始的net.sf.cglib )。 更改軟件包的原因是為了避免與類路徑上已經(jīng)存在的任何cglib版本沖突。
另外,現(xiàn)在使用新的cglib 3.0,它是從較早的2.2依賴項(xiàng)升級的(有關(guān)更多詳細(xì)信息,請參見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進(jìn)行元注釋,這使注釋的類成為標(biāo)準(zhǔn)bean ,因此也成為組件掃描的候選對象。 @Configuration類的主要目的是成為Spring IoC容器的bean定義的來源。 有關(guān)更詳細(xì)的描述,請參見官方文檔 。
然后, @ ImportResource用于導(dǎo)入基于XML的現(xiàn)有Spring配置。 這可能是仍在從XML遷移到Java的配置,或者只是您希望保留的傳統(tǒng)配置。 無論哪種方式,將其導(dǎo)入到容器對于成功遷移都是必不可少的,它允許很小的步驟而沒有太大的風(fēng)險(xiǎn)。 替換的等效XML注釋是:
<import resource =” classpath *:/ rest_config.xml” />
繼續(xù)@ComponentScan –這將配置組件掃描指令,有效地替換XML:
<context:component-scan base-package="org.rest" />從Spring 3.1開始,默認(rèn)情況下, @ Configuration不包括在類路徑掃描中,請參見JIRA問題 。 在Spring 3.1之前,這些類應(yīng)明確排除在外:
excludeFilters = { @ComponentScan.Filter( Configuration.class ) }@Configuration類不應(yīng)被自動發(fā)現(xiàn),因?yàn)樗鼈円延蒀ontainer指定并使用-允許重新發(fā)現(xiàn)它們并將其引入Spring上下文將導(dǎo)致以下錯誤:
由以下原因引起:org.springframework.context.annotation.ConflictingBeanDefinitionException:豆類[org.rest.spring.AppConfig]的由注釋指定的豆名稱'webConfig'與同名和類[org.net]的現(xiàn)有,不兼容的豆定義沖突。 rest.spring.AppConfig]
最后,使用@Bean批注配置屬性支持 – PropertySourcesPlaceholderConfigurer在@Bean批注的方法中初始化,指示它將產(chǎn)生由Container管理的Spring bean。 此新配置已替換以下XML:
<context:property-placeholder location="classpath:persistence.properties, classpath:web.properties" ignore-unresolvable="true"/>有關(guān)為什么需要手動注冊PropertySourcesPlaceholderConfigurer bean的詳細(xì)討論,請參見帶有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而不是默認(rèn)的XmlWebApplicationContext 。 較新的AnnotationConfigWebApplicationContext接受帶注解的@Configuration的類作為Container配置的輸入,這是設(shè)置基于Java的上下文所必需的。
與XmlWebApplicationContext不同,它不假定默認(rèn)配置類位置,因此必須設(shè)置Servlet的“ contextConfigLocation” init-param 。 這將指向@Configuration類所在的java包。 還支持類的完全限定名稱。
接下來,將DispatcherServlet配置為使用相同類型的上下文,唯一的區(qū)別是它從不同的包中加載配置類。
除此之外, web.xml并沒有真正從XML更改為基于Java的配置。
4。結(jié)論
提出的方法允許將Spring配置從XML平滑遷移到Java,同時將新舊混合在一起。 這對于較舊的項(xiàng)目很重要,因?yàn)檩^舊的項(xiàng)目可能具有許多基于XML的配置,無法一次全部遷移。 這樣,應(yīng)用程序的web.xml和引導(dǎo)程序是遷移的第一步,之后可以以較小的增量移植其余的XML bean。
在關(guān)于REST with Spring的下一篇文章中 ,我將介紹如何在項(xiàng)目中設(shè)置MVC,HTTP狀態(tài)代碼的配置,有效負(fù)載編組和內(nèi)容協(xié)商。 同時,您可以簽出github項(xiàng)目 。
翻譯自: https://www.javacodegeeks.com/2011/11/bootstrapping-web-application-with.html
總結(jié)
以上是生活随笔為你收集整理的使用Spring 3引导Web应用程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白贝粥的做法 怎么做白贝粥
- 下一篇: Oracle WebLogic Java