You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 3ffc3aa
Browse filesBrowse the repository at this point in the historyBrowse files
<para>If you're new here, <linkxl: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>
43
+
<para><linkxl:href=""/></para>
44
+
<!--
45
+
Start Shortcoder content
46
+
-->
47
+
<!--
48
+
End Shortcoder content
49
+
-->
50
+
<section>
51
+
<title><anchorxml:id="50Table_of_Contents"/><emphasisrole="bold">Table of Contents</emphasis></title>
<para><linklinkend="cglib"><emphasisrole="bold">Â Â Â 2.1. </emphasis>Justification of the <emphasis>cglib</emphasis> dependency</link></para>
61
+
</listitem>
62
+
<listitem>
63
+
<para><linklinkend="cglib_gone"><emphasisrole="bold">Â Â Â 2.2. </emphasis>The <emphasis>cglib</emphasis> dependency in Spring 3.2 and beyond</link></para>
64
+
</listitem>
65
+
<listitem>
66
+
<para><linklinkend="java_config"><emphasisrole="bold">3. </emphasis>The Java based web configuration</link></para>
67
+
</listitem>
68
+
<listitem>
69
+
<para><linklinkend="web_xml"><emphasisrole="bold">Â Â 3.1. </emphasis>The web.xml</link></para>
<para>The tutorial illustrates how to <emphasisrole="bold">Bootstrap a Web Application with Spring</emphasis> and also discusses how to make the jump <emphasisrole="bold">from XML to Java</emphasis> without having to completely migrate the entire XML configuration.</para>
78
+
</section>
79
+
<sectionxml:id="78maven.1">
80
+
<title><anchorxml:id="79dbdoclet.2_The_Maven_pomxml"/><emphasisrole="bold">2. The Maven pom.xml</emphasis></title>
<title><emphasisrole="bold">2.1. The cglib dependency before Spring 3.2</emphasis></title>
132
+
<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>
133
+
<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>
134
+
<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>
135
+
<para>Also, because of this, there are a few restrictions for <emphasis>Configuration</emphasis> annotated classes:</para>
136
+
<itemizedlist>
137
+
<listitem>
138
+
<para> Configuration classes <emphasisrole="bold">should not be final</emphasis></para>
139
+
</listitem>
140
+
<listitem>
141
+
<para> They should have a constructor with no arguments</para>
142
+
</listitem>
143
+
</itemizedlist>
144
+
</section>
145
+
<sectionxml:id="144cglib_gone.1">
146
+
<title><emphasisrole="bold">2.2. The cglib dependency in Spring 3.2 and beyond</emphasis></title>
147
+
<para>Starting with Spring 3.2, it is <emphasisrole="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>
148
+
<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>
149
+
<para>Also, the new cglib 3.0 is now used, upgraded from the older 2.2 dependency (see this <linkxl:href="https://jira.springsource.org/browse/SPR-9669">JIRA issue</link> for more details).</para>
150
+
<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 <linkxl:href="https://jira.springsource.org/browse/SPR-9639">this Spring Jira</link> to keep track of the Spring support, and <linkxl:href="http://www.baeldung.com/java8">the Java 8 Resources page</link> to keep tabs on the that.</para>
151
+
</section>
152
+
</section>
153
+
<sectionxml:id="152java_config.1">
154
+
<title><anchorxml:id="153dbdoclet.3_The_Java_based_Web_Configuration"/><emphasisrole="bold">3. The Java based Web Configuration</emphasis></title>
  public static PropertySourcesPlaceholderConfigurer properties() {
163
+
  return new PropertySourcesPlaceholderConfigurer();
164
+
  }
165
+
}</screen>
166
+
<para>First, the <emphasisrole="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 <emphasisrole="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 <linkxl:href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-java">official docs</link>.</para>
167
+
<para>Then, <emphasisrole="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>Moving on to <emphasisrole="bold">@ComponentScan</emphasis> – this configures the component scanning directive, effectively replacing the XML:</para>
<para>As of Spring 3.1, the <emphasis>@Configuration</emphasis> are excluded from classpath scanning by default – see <linkxl: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>
<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>
174
+
<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>
175
+
<para>And finally, using the<emphasisrole="bold"> @Bean</emphasis> annotation to configure the <emphasisrole="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>
<para>For a more in depth discussion on why it was necessary to manually register the <emphasis>PropertySourcesPlaceholderConfigurer</emphasis> bean, see the <linkxl:href="http://www.baeldung.com/2012/02/06/properties-with-spring/">Properties with Spring Tutorial</link>.</para>
180
+
<sectionxml:id="179web_xml.1">
181
+
<title><emphasisrole="bold">3.1. The web.xml</emphasis></title>
<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>
234
+
<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>
235
+
<para>Other than this, the <emphasis>web.xml</emphasis> doesn’t really change from a XML to a Java based configuration.</para>
<para>The presented approach allows for a smooth <emphasisrole="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>
241
+
<para>This way, in a migration, the XML beans can be ported in small increments.</para>
242
+
<para>In <linkxl: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>
243
+
<para><linkxl: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>
244
+
<para>This is an Eclipse based project, so it should be easy to import and run as it is.</para>
0 commit comments