commit
c325d48063
|
@ -1,7 +1,7 @@
|
|||
transformation html2docbook;
|
||||
|
||||
section HTML {
|
||||
encoding = "UTF-8";
|
||||
encoding = "windows-1252";
|
||||
exclude = ["//h3[contains(.,'I usually')]", "//*[@id='disqus_thread']","//head","//*[@class='custom-design-100']","//*[@class='custom-design-114']", "//form","//*[@src]","//*[@id='inner-wrapper']/*[position()<7]" , "//*[@class='post-meta']" , "//*[@class='entry-title']","//*[@id='respond']" ,"//*[@id='comments']","//*[@class='post-entries']","//*[@class='social']/../../h3[last()]" ,"//*[@class='social']/.."];
|
||||
section-numbering-pattern = "(((\d\.)+)?\d?\.?\p{Z}*).*";
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1,313 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<article version="5.0" xml:lang="en-US" xmlns="http://docbook.org/ns/docbook" xmlns:xl="http://www.w3.org/1999/xlink">
|
||||
<section>
|
||||
<title>Bootstrap a Web Application with Spring 4</title>
|
||||
</section>
|
||||
<anchor xml:id="5wrapper"/>
|
||||
<anchor xml:id="6inner-wrapper"/>
|
||||
<!--
|
||||
/#side-nav
|
||||
-->
|
||||
<!--
|
||||
/.menus
|
||||
-->
|
||||
<para><link linkend="top">Return to Content</link></para>
|
||||
<!--
|
||||
#content Starts
|
||||
-->
|
||||
<anchor xml:id="17content"/>
|
||||
<anchor xml:id="18main-sidebar-container"/>
|
||||
<!--
|
||||
#main Starts
|
||||
-->
|
||||
<anchor xml:id="22toc_container"/>
|
||||
<para>Contents</para>
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<para><link linkend="Table_of_Contents">Table of Contents</link></para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><link linkend="dbdoclet.1_Overview">1. Overview</link></para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><link linkend="dbdoclet.2_The_Maven_pomxml">2. The Maven pom.xml</link></para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><link linkend="dbdoclet.3_The_Java_based_Web_Configuration">3. The Java based Web Configuration</link></para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><link linkend="dbdoclet.4_Conclusion">4. Conclusion</link></para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
<para>If you're new here, <link xl:href="https://my.leadpages.net/leadbox/146382273f72a2%3A13a71ac76b46dc/5735865741475840/">you may want to get my "REST APIs with Spring" eBook</link>. Thanks for visiting!</para>
|
||||
<para><link xl:href=""/></para>
|
||||
<!--
|
||||
Start Shortcoder content
|
||||
-->
|
||||
<!--
|
||||
End Shortcoder content
|
||||
-->
|
||||
<section>
|
||||
<title><anchor xml:id="50Table_of_Contents"/><emphasis role="bold">Table of Contents</emphasis></title>
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<para><link linkend="overview"><emphasis role="bold">1. </emphasis>Overview</link></para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><link linkend="maven"><emphasis role="bold">2. </emphasis>The Maven pom.xml</link></para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><link linkend="cglib"><emphasis role="bold">Â Â Â 2.1. </emphasis>Justification of the <emphasis>cglib</emphasis> dependency</link></para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><link linkend="cglib_gone"><emphasis role="bold">Â Â Â 2.2. </emphasis>The <emphasis>cglib</emphasis> dependency in Spring 3.2 and beyond</link></para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><link linkend="java_config"><emphasis role="bold">3. </emphasis>The Java based web configuration</link></para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><link linkend="web_xml"><emphasis role="bold">Â Â 3.1. </emphasis>The web.xml</link></para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para><link linkend="conclusion"><emphasis role="bold">4. </emphasis>Conclusion</link></para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
<section xml:id="74overview.1">
|
||||
<title><anchor xml:id="75dbdoclet.1_Overview"/><emphasis role="bold">1. Overview</emphasis></title>
|
||||
<para>The tutorial illustrates how to <emphasis role="bold">Bootstrap a Web Application with Spring</emphasis> and also discusses how to make the jump <emphasis role="bold">from XML to Java</emphasis> without having to completely migrate the entire XML configuration.</para>
|
||||
</section>
|
||||
<section xml:id="78maven.1">
|
||||
<title><anchor xml:id="79dbdoclet.2_The_Maven_pomxml"/><emphasis role="bold">2. The Maven pom.xml</emphasis></title>
|
||||
<screen><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>
|
||||
|
||||
</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>4.0.5.RELEASE</spring.version>
|
||||
</properties>
|
||||
|
||||
</project></screen>
|
||||
<section xml:id="129cglib.1">
|
||||
<title><emphasis role="bold">2.1. The cglib dependency before Spring 3.2</emphasis></title>
|
||||
<para>You may wonder why <emphasis>cglib</emphasis> is a dependency – it turns out there is a valid reason to include it – the entire configuration cannot function without it. If removed, Spring will throw:</para>
|
||||
<para><emphasis>Caused by: java.lang.IllegalStateException: CGLIB is required to process @Configuration classes. Either add CGLIB to the classpath or remove the following @Configuration bean definitions</emphasis></para>
|
||||
<para>The reason this happens is explained by the way Spring deals with <emphasis>@Configuration</emphasis> classes. These classes are effectively beans, and because of this they need to be aware of the Context, and respect scope and other bean semantics. This is achieved by dynamically creating a cglib proxy with this awareness for each <emphasis>@Configuration</emphasis> class, hence the cglib dependency.</para>
|
||||
<para>Also, because of this, there are a few restrictions for <emphasis>Configuration</emphasis> annotated classes:</para>
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<para> Configuration classes <emphasis role="bold">should not be final</emphasis></para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para> They should have a constructor with no arguments</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
</section>
|
||||
<section xml:id="144cglib_gone.1">
|
||||
<title><emphasis role="bold">2.2. The cglib dependency in Spring 3.2 and beyond</emphasis></title>
|
||||
<para>Starting with Spring 3.2, it is <emphasis role="bold">no longer necessary to add cglib as an explicit dependency</emphasis>. This is because Spring is in now inlining <emphasis>cglib</emphasis> – which will ensure that all class based proxying functionality will work out of the box with Spring 3.2.</para>
|
||||
<para>The new cglib code is placed under the Spring package: <emphasis>org.springframework.cglib</emphasis> (replacing the original <emphasis>net.sf.cglib</emphasis>). The reason for the package change is to avoid conflicts with any <emphasis>cglib</emphasis> versions already existing on the classpath.</para>
|
||||
<para>Also, the new cglib 3.0 is now used, upgraded from the older 2.2 dependency (see this <link xl:href="https://jira.springsource.org/browse/SPR-9669">JIRA issue</link> for more details).</para>
|
||||
<para>Finally, now that Spring 4.0 is out in the wild, changes like this one (removing the cglib dependency) are to be expected with Java 8 just around the corner – you can watch <link xl:href="https://jira.springsource.org/browse/SPR-9639">this Spring Jira</link> to keep track of the Spring support, and <link xl:href="http://www.baeldung.com/java8">the Java 8 Resources page</link> to keep tabs on the that.</para>
|
||||
</section>
|
||||
</section>
|
||||
<section xml:id="152java_config.1">
|
||||
<title><anchor xml:id="153dbdoclet.3_The_Java_based_Web_Configuration"/><emphasis role="bold">3. The Java based Web Configuration</emphasis></title>
|
||||
<screen>@Configuration
|
||||
@ImportResource( { "classpath*:/rest_config.xml" } )
|
||||
@ComponentScan( basePackages = "org.rest" )
|
||||
@PropertySource({ "classpath:rest.properties", "classpath:web.properties" })
|
||||
public class AppConfig{
|
||||
|
||||
@Bean
|
||||
  public static PropertySourcesPlaceholderConfigurer properties() {
|
||||
  return new PropertySourcesPlaceholderConfigurer();
|
||||
  }
|
||||
}</screen>
|
||||
<para>First, the <emphasis role="bold">@Configuration</emphasis> annotation – this is the main artifact used by the Java based Spring configuration; it is itself meta-annotated with <emphasis>@Component</emphasis>, which makes the annotated classes <emphasis role="bold">standard beans</emphasis> and as such, also candidates for component scanning. The main purpose of <emphasis>@Configuration</emphasis> classes is to be sources of bean definitions for the Spring IoC Container. For a more detailed description, see the <link xl:href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-java">official docs</link>.</para>
|
||||
<para>Then, <emphasis role="bold">@ImportResource</emphasis> is used to import the existing XML based Spring configuration. This may be configuration which is still being migrated from XML to Java, or simply legacy configuration that you wish to keep. Either way, importing it into the Container is essential for a successful migration, allowing small steps without to much risk. The equivalent XML annotation that is replaced is:</para>
|
||||
<para><emphasis><import resource=”classpath*:/rest_config.xml” /></emphasis></para>
|
||||
<para>Moving on to <emphasis role="bold">@ComponentScan</emphasis> – this configures the component scanning directive, effectively replacing the XML:</para>
|
||||
<screen><context:component-scan base-package="org.rest" /></screen>
|
||||
<para>As of Spring 3.1, the <emphasis>@Configuration</emphasis> are excluded from classpath scanning by default – see <link xl:href="https://jira.springsource.org/browse/SPR-8808">this JIRA issue</link>. Before Spring 3.1 though, these classes should have been excluded explicitly:</para>
|
||||
<screen>excludeFilters = { @ComponentScan.Filter( Configuration.class ) }</screen>
|
||||
<para>The <emphasis>@Configuration</emphasis> classes should not be autodiscovered because they are already specified and used by the Container – allowing them to be rediscovered and introduced into the Spring context will result in the following error:</para>
|
||||
<para><emphasis>Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name ‘webConfig’ for bean class [org.rest.spring.AppConfig] conflicts with existing, non-compatible bean definition of same name and class [org.rest.spring.AppConfig]</emphasis></para>
|
||||
<para>And finally, using the<emphasis role="bold"> @Bean</emphasis> annotation to configure the <emphasis role="bold">properties support</emphasis> –Â <emphasis>PropertySourcesPlaceholderConfigurer</emphasis> is initialized in a <emphasis>@Bean</emphasis> annotated method, indicating it will produce a Spring bean managed by the Container. This new configuration has replaced the following XML:</para>
|
||||
<screen><context:property-placeholder
|
||||
location="classpath:persistence.properties, classpath:web.properties"
|
||||
ignore-unresolvable="true"/></screen>
|
||||
<para>For a more in depth discussion on why it was necessary to manually register the <emphasis>PropertySourcesPlaceholderConfigurer</emphasis> bean, see the <link xl:href="http://www.baeldung.com/2012/02/06/properties-with-spring/">Properties with Spring Tutorial</link>.</para>
|
||||
<section xml:id="179web_xml.1">
|
||||
<title><emphasis role="bold">3.1. The web.xml</emphasis></title>
|
||||
<screen><?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></screen>
|
||||
<para> First, the root context is defined and configured to use <emphasis>AnnotationConfigWebApplicationContext</emphasis> instead of the default <emphasis>XmlWebApplicationContext</emphasis>. The newer <emphasis>AnnotationConfigWebApplicationContext</emphasis> accepts <emphasis>@Configuration</emphasis> annotated classes as input for the Container configuration and is needed in order to set up the Java based context. Unlike <emphasis>XmlWebApplicationContext</emphasis>, it assumes no default configuration class locations, so the <emphasis>“contextConfigLocation”</emphasis><emphasis>init-param</emphasis> for the Servlet must be set. This will point to the java package where the <emphasis>@Configuration</emphasis> classes are located; the fully qualified name(s) of the classes are also supported.</para>
|
||||
<para>Next, the <emphasis>DispatcherServlet</emphasis> is configured to use the same kind of context, with the only difference that it’s loading configuration classes out of a different package.</para>
|
||||
<para>Other than this, the <emphasis>web.xml</emphasis> doesn’t really change from a XML to a Java based configuration.</para>
|
||||
</section>
|
||||
</section>
|
||||
<section xml:id="237conclusion.1">
|
||||
<title><anchor xml:id="238dbdoclet.4_Conclusion"/><emphasis role="bold">4. Conclusion</emphasis></title>
|
||||
<para>The presented approach allows for a smooth <emphasis role="bold">migration of the Spring configuration</emphasis> from XML to Java, mixing the old and the new. This is important for older projects, which may have a lot of XML based configuration that cannot be migrated all at once.</para>
|
||||
<para>This way, in a migration, the XML beans can be ported in small increments.</para>
|
||||
<para>In <link xl:href="http://www.baeldung.com/2011/10/25/building-a-restful-web-service-with-spring-3-1-and-java-based-configuration-part-2/">the next article on REST with Spring</link>, I cover setting up MVC in the project, configuration of the HTTP status codes, payload marshalling and content negotiation.</para>
|
||||
<para><link xl:href="https://my.leadpages.net/leadbox/147e9e473f72a2%3A13a71ac76b46dc/5745710343389184/">The implementation of this <emphasis>Bootstrap a Spring Web App Tutorial</emphasis> can be downloaded as a working sample project.</link></para>
|
||||
<para>This is an Eclipse based project, so it should be easy to import and run as it is.</para>
|
||||
<!--
|
||||
Start Shortcoder content
|
||||
-->
|
||||
<para>Â </para>
|
||||
<!--
|
||||
End Shortcoder content
|
||||
-->
|
||||
<para><link xl:href=""/></para>
|
||||
<anchor xml:id="252dd_ajax_float"/>
|
||||
<para><link xl:href="http://twitter.com/share"/></para>
|
||||
<!--
|
||||
OptinSkin
|
||||
-->
|
||||
<anchor xml:id="257ois_12"/>
|
||||
<!--
|
||||
End OptinSkin
|
||||
-->
|
||||
<!--
|
||||
/.entry
|
||||
-->
|
||||
<para><emphasis role="italic"/><link xl:href="http://www.baeldung.com/tag/java-2/">java</link>, <link xl:href="http://www.baeldung.com/tag/spring/">Spring</link></para>
|
||||
<!--
|
||||
/.post
|
||||
-->
|
||||
<!--
|
||||
/#main
|
||||
-->
|
||||
<anchor xml:id="271optinskin-widget-3"/>
|
||||
<!--
|
||||
OptinSkin
|
||||
-->
|
||||
<anchor xml:id="275ois_5"/>
|
||||
<!--
|
||||
End OptinSkin
|
||||
-->
|
||||
<!--
|
||||
/#sidebar
|
||||
-->
|
||||
<!--
|
||||
/#main-sidebar-container
|
||||
-->
|
||||
<!--
|
||||
/#content
|
||||
-->
|
||||
<anchor xml:id="288copyright"/>
|
||||
<para>© 2014 Baeldung. All Rights Reserved. </para>
|
||||
<anchor xml:id="290credit"/>
|
||||
<!--
|
||||
/#inner-wrapper
|
||||
-->
|
||||
<!--
|
||||
/#wrapper
|
||||
-->
|
||||
<!--
|
||||
/.fix
|
||||
-->
|
||||
<!--
|
||||
ngg_resource_manager_marker
|
||||
-->
|
||||
<!--
|
||||
WP SyntaxHighlighter Ver.1.7.3 Begin
|
||||
-->
|
||||
<!--
|
||||
WP SyntaxHighlighter Ver.1.7.3 End
|
||||
-->
|
||||
</section>
|
||||
</section>
|
||||
|
||||
</article>
|
Loading…
Reference in New Issue