Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Configuration

Tags:

spring

xml

I've been reading up on Spring and it keeps talking about the spring configuration data you need, but where do you put this xml file? and what do you save it as? I can't seem to find this information anywhere.

like image 553
auwall Avatar asked Jun 08 '11 18:06

auwall


People also ask

What is @configuration and @bean in spring?

Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. The @Bean annotation tells Spring that a method annotated with @Bean will return an object that should be registered as a bean in the Spring application context.

What does @configuration do spring boot?

@Configuration : Tags the class as a source of bean definitions for the application context. @EnableAutoConfiguration : Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.

Do we need @configuration in spring boot?

@Configuration is: not required, if you already pass the annotated class in the sources parameter when calling the SpringApplication. run() method; required, when you don't pass the annotated class explicitly, but it's in the package that's specified in the @ComponentScan annotation of your main configuration class.


2 Answers

More importantly than where, the question should be for you: what exactly is this 'configuration data'?

From the docs:

the Spring IoC container consumes a form of configuration metadata; this configuration metadata represents how you as an application developer tell the Spring container to instantiate, configure, and assemble the objects in your application.

Configuration metadata is traditionally supplied in a simple and intuitive XML format, which is what most of this chapter uses to convey key concepts and features of the Spring IoC container.

However, you can also use annotations or Java-based configuration to provide the configuration metadata for your POJOs.

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

As Tnem already mentioned, here you can find how to instantiate a container in different scenarios.

And what is this IoC container?

IoC (inversion of control) and DI (dependency injection) are terms coined by Martin Fowler, regarding Spring see the first section of the docs.

I encourage you to read the whole reference if you want to get into development with Spring.

like image 155
abalogh Avatar answered Sep 28 '22 10:09

abalogh


More interesting than the pure name is how do you split the files (and give each part a name).

If you have a Standalone or Webapplication with out tests, then you can put all configuration in one file. - But having no test should not be a opinion.

Lets assume you have a web application with tests.

Then you should split the configuration in two files, one for the pure java (without the web suff) configuration and one that contains all the other stuff for the WEB application.

I personaly perfer to name it applicationContext.xml and webmvc-config.xml. The default name for the web configuration file (if no specifed for the Dispatcher Servlet) would be /WEB-INF/<servletname>-servlet.xml)

I locate the applicationContext.xml in classpath:/META-INF/spring directory and the webmvc-config.xml in WEB-INF/spring. That location is the style of Spring Roo. It works, but every other folder would work too. Because I use maven the exact location of the files are:

  • /src/main/resources/META-INF/spring/applicationContext.xml
  • /src/main/webapp/WEB-INF/spring/webmvc-config.xml

The core applicationContext.xml is loaded with the org.springframework.web.context.ContextLoaderListener, and the webmvc-config.xml by the Dispatacher Servlet. web.xml:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>CFMA-SpringProject</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Now you start to write your tests, for the business logic of your application, without loading all the web stuff. But in most cases that is not enougth. For example you want to run some fast tests with an Inmemory Database while you run the normal Application with an persistent Database like MySql (please don't blame me for that sentence), or you want to use a jndi configured db in production and a "normal" configured one for tests. So what you need is two different configuration. To write not every thing twice, the easiest way is to split the applicationContext.xml in two files:

  • applicationContext.xml for the core stuff without the db stuff that differs to the tests
  • applicationContext-db.xml for the productive db configuration (for example jndi-lookup for db connection and LocalContainerEntityManagerFactoryBean for MySql)

(Now you understand the pattern of contextConfigLocation in the web.xml)

For the Tests you need now two files (you can write it in one file, but I prefer two). * testContext-h2DbConfig.xml The file that is the testing sibling of applicationContext-db.xml, but with the test database and without jndi. * textContext.xml This file in the one referenced by @ContextConfiguration in you test cases. This file contains only the imports of the configuration you need for the tests. In this case it is:

<import resource="classpath:/META-INF/spring/applicationContext.xml" /> 
<import resource="classpath:/META-INF/spring/testContext-h2DbConfig.xml" />

Because I use spring, both files are located in /src/test/resources/META-INF/spring/testContext.xml

If you have other aspects of your spring configuration, where the test and the productive configuration differs (for example schedulers), then you can split it in the same way.

I hope you understand how splitting, naming convention and location work together.

like image 22
Ralph Avatar answered Sep 28 '22 09:09

Ralph