java - Converting spring XML file to spring @Configuration class -


following question understanding spring @autowired usage wanted create complete knowledge base other option of spring wiring, @configuration class.

let's assume have spring xml file looks this:

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"   xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    <import resource="another-application-context.xml"/>    <bean id="somebean" class="stack.overflow.spring.configuration.someclassimpl">     <constructor-arg value="${some.interesting.property}" />   </bean>    <bean id="anotherbean" class="stack.overflow.spring.configuration.anotherclassimpl">     <constructor-arg ref="somebean"/>     <constructor-arg ref="beanfromsomewhereelse"/>   </bean> </beans> 

how can use @configuration instead? have affect on code itself?

(disclaimer - answer based on blog post)

migrating xml @configuration

it possible migrate xml @configuration in few steps:

  1. create @configuration annotated class:

    @configuration public class myapplicationcontext {  } 
  2. for each <bean> tag create method annotated @bean:

    @configuration public class myapplicationcontext {    @bean(name = "somebean")   public someclass getsomeclass() {     return new someclassimpl(someinterestingproperty); // still need inject someinterestingproperty   }    @bean(name = "anotherbean")   public anotherclass getanotherclass() {     return new anotherclassimpl(getsomeclass(), beanfromsomewhereelse); // still need inject beanfromsomewhereelse   } } 
  3. in order import beanfromsomewhereelse need import it's definition. can defined in xml , we'll use @importresource:

    @importresource("another-application-context.xml") @configuration public class myapplicationcontext {   ...   } 

    if bean defined in @configuration class can use @import annotation:

    @import(otherconfiguration.class) @configuration public class myapplicationcontext {   ... } 
  4. after imported other xmls or @configuration classes, can use beans declare in our context declaring private member @configuration class follows:

    @autowired @qualifier(value = "beanfromsomewhereelse") private final strangebean beanfromsomewhereelse; 

    or use directly parameter in method defines bean depends on beanfromsomewhereelse using @qualifier follows:

    @bean(name = "anotherbean") public anotherclass getanotherclass(@qualifier (value = "beanfromsomewhereelse") final strangebean beanfromsomewhereelse) {   return new anotherclassimpl(getsomeclass(), beanfromsomewhereelse); } 
  5. importing properties similar importing bean xml or @configuration class. instead of using @qualifier we'll use @value properties follows:

    @autowired @value("${some.interesting.property}") private final string someinterestingproperty; 

    this can used spel expressions well.

  6. in order allow spring treat such classes beans containers need mark in our main xml putting tag in context:

    <context:annotation-config/> 

    you can import @configuration classes same create simple bean:

    <bean class="some.package.myapplicationcontext"/> 

    there ways avoid spring xmls altogether not in scope of answer. can find out 1 of these options in blog post on i'm basing answer.


the advantages , disadvantages of using method

basically find method of declaring beans more comfortable using xmls due few advantages see:

  1. typos - @configuration classes compiled , typos won't allow compilations
  2. fail fast (compile time) - if forget inject bean you'll fail on compile time , not on run-time xmls
  3. easier navigate in ide - between constructors of beans understand dependency tree.
  4. possible debug configuration startup

the disadvantages not many see them there few think of:

  1. abuse - code easier abuse xmls
  2. with xmls can defined dependencies based on classes not available during compile time provided during run-time. @configuration classes must have classes available @ compile time. that's not issue, there cases may be.

bottom line: fine combine xmls, @configuration , annotations in application context. spring doesn't care method bean declared with.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -