Discard the heavy flavors of the XML configuration--spring4 using groovy to configure the Bean (GO)

Source: Internet
Author: User

Before Spring4, bean configuration can be mainly divided into two ways, one is the use of XML-based, the personal very annoying this way, because obviously a very simple thing, put in XML will be a lot more complex information. Another way, starting with spring3.0, Spring provides a Java-based configuration that looks a little bit better than the way XML is configured. And in the spring4.0 of release a few days ago, we could use groovy as a spring configuration file! Using groovy is more flexible and less disruptive than the earliest XML-based configuration. Groovy configuration is more refined than Java-based configuration!

This blog reviews the existing Xml,java configuration, and then simply shows how groovy is configured by Spring4. The code for this blog is on GitHub: Https://github.com/kiwiwin/spring4-bean-demo

example, we use Gradle to introduce dependencies:

    Compile ("Org.springframework:spring-core:4.0.0.release",            "Org.springframework:spring-context : 4.0.0.RELEASE ")

Here is a very simple example of a football game with Hometeam home and Awayteam away, which are two beans passed through a constructor function. There are also homescore and awayscore that indicate the scores that the home team and the visiting team get separately, passing through the property.

Here's how to compare XML, Java, groovy three configurations

1. Using XML for Bean configuration

Football-match-config.xml

<?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.xsd "><bean id= "Footballmatch"class= "Org.kiwi.spring.groovy.FootballMatch" > <constructor-arg ref= "hometeam"/> <constructor-arg ref=    "Awayteam"/> <property name= "Homescore" value= "3"/> <property name= "Awayscore" value= "1"/> </bean> <bean id= "Hometeam"class= "Org.kiwi.spring.groovy.FootballTeam" > <constructor-arg value= "Manchester"/> </bean> & Lt;bean id= "Awayteam"class= "Org.kiwi.spring.groovy.FootballTeam" > <constructor-arg value= "AC Milan"/> </bean></beans>

Although our IDE is smart enough to help us generate various tags for XML, it does seem to make people feel uncomfortable. But if you really like this, then I can only say that you are happy.

2. Using Java for bean configuration

First define the bean:

Footballmatchconfig.java

 PackageOrg.kiwi.spring.groovy;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration; @Configuration Public classFootballmatchconfig {@Bean PublicFootballmatch Footballmatch () {FinalFootballmatch Footballmatch =NewFootballmatch (Hometeam (), Awayteam ()); Footballmatch.sethomescore (3); Footballmatch.setawayscore (1); returnFootballmatch; } @Bean PublicFootballteam Hometeam () {return NewFootballteam ("Manchester"); } @Bean PublicFootballteam Awayteam () {return NewFootballteam ("AC Milan"); }}

Then define the location of the Java configuration file from the context:

Football-match-java-config.xml

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsd "><context:component-Scan Base- Package= "Org.kiwi.spring.groovy"/></beans>

Java-based bean configuration is no longer limited to XML itself, compared to a purely XML-based bean configuration. The configuration itself is clearer.

3. Using Groovy for bean configuration

Footballmatchconfig.groovy

Import Org.kiwi.spring.groovy.FootballMatch Import Org.kiwi.spring.groovy.FootballTeambeans {    "Manchester")    "AC Milan")    Footballmatch (Footballmatch, Hometeam, Awayteam) {        = 3        = 1    }}

It's simple! Who sees this simple configuration method not to be tempted?

Unlike the previous applcationcontext, in order to support groovy-based bean configuration. The SPRING4 provides a new pair of groovy

Applicationcontext:genericgroovyapplicationcontext. Public class  Footballapp {    publicstaticvoid   Main (string[] args) {        finalnew Genericgroovyapplicationcontext (" Footballmatchconfig.groovy ");         Final Footballmatch Footballmatch = (footballmatch) context.getbean ("Footballmatch");        System.out.println (Footballmatch.display ());}    }

A heavy-tasting programmer might think it's a full-on-the-finish bean configuration with groovy. But think of Maven and Gradle,maven based on Xml,gradle based on groovy. The same configuration is very refined in gradle, and because Gradle uses a language such as groovy, rather than XML, to make it more flexible and more convenient to define tasks.

In this blog, although there is not much to write on the more powerful aspects of groovy configuration, simplicity is obvious. Simplicity is beauty.

We can be very conservative and think of all the places where we can use XML, which will eventually be replaced by other concise languages.

Warm tips:

There was a problem with the configuration file not found when trying to use groovy for the bean configuration for the first time in IntelliJ. This is because groovy's configuration file suffix is. Groovy. The IntelliJ compiler will treat it as a source file, not a configuration file. So you need to remove groovy from the resource patterns column in the compiler setup!? *.groovy can

Discard the heavy flavors of the XML configuration--spring4 using groovy to configure the Bean (GO)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.