Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

DSL Syntax Chapter 18 ■ Spring and Groovy Of course, you need to learn part of the syntax of the Spring DSL, right? This section will help you to understand the syntax. First, you need to start with a beans keyword. Between curly braces, you’ll place the bean definitions. // Some imports beans { } //Beans here or even some logic Every bean must have an Id and a class between the parentheses. beanId ( class ) Example: beans { } helloWorldService ( com.mycompany.service.MyService) //Equivalent to: Every bean property must be enclosed between curly braces, then you add the name of the property = value. beanId (class) { variable = value variable = ref('value') //Reference to a another bean } Example: beans { } helloWorldService ( com.mycompany.service.MyService) { message = "Hello World" greeting = ref ("myGreeting") } myGreeting ( com.mycompany.service.CustomGreeting) //Equivalent to: 259

Chapter 18 ■ Spring and Groovy If the property is a map or an array, use square brackets and separate the elements by a comma. beanId (class) { name = [ value1, value2, ...] name = [ key1:value, key2:value] } //Example: beans { helloWorldService ( com.mycompany.service.MyService) { title = ["Sr.", "Jr.", "Ms.", "Mrs."] gender = [male: "Hi", female: "Hello"] } } //Equivalent to: Sr. Jr. Ms. Mrs. If a bean has a constructor, then they need to be after the class definition. Example: beanId (class, contructor-param1 [, constructor-param-n]) beans { helloWorldService ( com.mycompany.service.MyService, "Hello World") myGreeting ( com.mycompany.service.CustomGreeting, ref("defaultLanguage")) } defaultLanguage( com.mycompany.i18n.Language, "english") 260

DSL Syntax<br />

Chapter 18 ■ <strong>Spring</strong> and Groovy<br />

Of course, you need to learn part of the syntax of the <strong>Spring</strong> DSL, right? This section will help you to understand<br />

the syntax.<br />

First, you need to start with a beans keyword. Between curly braces, you’ll place the bean definitions.<br />

// Some imports<br />

beans {<br />

}<br />

//Beans here or even some logic<br />

Every bean must have an Id and a class between the parentheses.<br />

beanId ( class )<br />

Example:<br />

beans {<br />

}<br />

helloWorldService ( com.mycompany.service.MyService)<br />

//Equivalent to:<br />

<br />

Every bean property must be enclosed between curly braces, then you add the name of the property = value.<br />

beanId (class) {<br />

variable = value<br />

variable = ref('value') //Reference to a another bean<br />

}<br />

Example:<br />

beans {<br />

}<br />

helloWorldService ( com.mycompany.service.MyService) {<br />

message = "Hello World"<br />

greeting = ref ("myGreeting")<br />

}<br />

myGreeting ( com.mycompany.service.CustomGreeting)<br />

//Equivalent to:<br />

<br />

<br />

<br />

<br />

<br />

259

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!