...
| Code Block |
|---|
class Calculator {
private total = 0
def add(a, b) { total++; a + b }
def getTotalCalulationsgetTotalCalculations() { 'Total Calculations: ' + total }
String toString() { 'Calc: ' + hashCode()}
}
class Client {
def calc = new Calculator()
def executeCalc(a, b) { calc.add(a, b) }
String toString() { 'Client: ' + hashCode()}
}
|
...
| Code Block |
|---|
def client = new Client() assert 3 == client.executeCalc(1, 2) println "$client, $client.calc, $client.calc.totalCalulationstotalCalculations" client = new Client() assert 4 == client.executeCalc(2, 2) println "$client, $client.calc, $client.calc.totalCalulationstotalCalculations" |
Here is the result of running this script (your hashcode values may vary):
...
| Code Block |
|---|
// require(groupId:'aopalliance', artifactId:'aopalliance', version:'1.0')
// require(groupId:'com.google.code.guice', artifactId:'guice', version:'1.0')
import com.google.inject.*
interface Calculator {
def add(a, b)
}
class CalculatorImpl implements Calculator {
private total = 0
def add(a, b) { total++; a + b }
def getTotalCalulationsgetTotalCalculations() { 'Total Calculations: ' + total }
String toString() { 'Calc: ' + hashCode()}
}
class Client {
@Inject Calculator calc
def executeCalc(a, b) { calc.add(a, b) }
String toString() { 'Client: ' + hashCode()}
}
def injector = Guice.createInjector (
[configure: { binding ->
binding.bind(Calculator)
.to(CalculatorImpl)
.asEagerSingleton() } ] as Module
)
client = injector.getInstance(Client)
assert 3 == client.executeCalc(1, 2)
println "$client, $client.calc, $client.calc.totalCalulationstotalCalculations"
client = injector.getInstance(Client)
assert 4 == client.executeCalc(2, 2)
println "$client, $client.calc, $client.calc.totalCalulationstotalCalculations"
|
Note the @Inject annotation in the Client class. We can always tell right in the source code which fields will be injected.
...
| Code Block |
|---|
import com.google.inject.*
@ImplementedBy(CalculatorImpl)
interface Calculator {
// as before ...
}
@Singleton
class CalculatorImpl implements Calculator {
// as before ...
}
class Client {
// as before ...
}
def injector = Guice.createInjector({} as Module[])
// ...
|
Note the @Singleton annotation on the CalculatorImpl class and the @ImplementedBy annotation in the Calculator interface.
...
| Code Block |
|---|
// require(groupId:'org.springframework', artifactId:'spring-core', version:'2.1m1')
// require(groupId:'org.springframework', artifactId:'spring-beans', version:'2.1m1')
import org.springframework.beans.factory.support.*
interface Calculator {
def add(a, b)
}
class CalculatorImpl implements Calculator {
private total = 0
def add(a, b) { total++; a + b }
def getTotalCalulationsgetTotalCalculations() { 'Total Calculations: ' + total }
String toString() { 'Calc: ' + hashCode()}
}
class Client {
Client(Calculator calc) { this.calc = calc }
def calc
def executeCalc(a, b) { calc.add(a, b) }
String toString() { 'Client: ' + hashCode()}
}
// Here we 'wire' up our dependencies through the API. Alternatively,
// we could use XML-based configuration or the Grails Bean Builder DSL.
def factory = new DefaultListableBeanFactory()
factory.registerBeanDefinition('calc', new RootBeanDefinition(CalculatorImpl))
def beanDef = new RootBeanDefinition(Client, false)
beanDef.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_AUTODETECT)
factory.registerBeanDefinition('client', beanDef)
client = factory.getBean('client')
assert 3 == client.executeCalc(1, 2)
println "$client, $client.calc, $client.calc.totalCalulationstotalCalculations"
client = factory.getBean('client')
assert 4 == client.executeCalc(2, 2)
println "$client, $client.calc, $client.calc.totalCalulationstotalCalculations"
|
And here is the result (your hashcode values will vary):
...