4. Spring Overview
Main functions of Spring
• Bean container: ApplicationContext and BeanFactory
• Dependency Injection (DI) and Inversion of Control (IoC)
• Proxies
• Transactions
• Security
• Caching
• Event publishing and listening
• Exception conversion
CONFIDENTIAL 4
10. Alternate approach to BeanDefinition modification
def doWithSpring = {
def mybeanDef = delegate.getBeanDefinition('mybean')
mybeanDef.beanClass = NewClass
mybeanDef.propertyValues.add("order",
application.config.plugin?.rendering?.order ?: 42)
}
● Use loadAfter = ['plugin1', 'plugin2'] to
ensure the bean is loaded
● Only valid in a plugin, not the app's resources.groovy
CONFIDENTIAL 10
12. Aliases
As of Grails 2.1 aliases work fully
• You can create aliases pre2.1 but only if defined in the same
resources.groovy or plugin (doWithSpring)
beans = {
springConfig.addAlias 'alias', 'realBean'
}
CONFIDENTIAL 12
15. Spring MVC
New in Grails 1.2
Annotate src/java or src/groovy classes with @Controller
Add all packages to the grails.spring.bean.packages list in
Config.groovy
• e.g.grails.spring.bean.packages = ['gr8conf.testapp.foo']
CONFIDENTIAL 15
16. Spring MVC
Annotate methods with
@o.s.w.bind.annotation.RequestMapping
@RequestMapping("/mvc/hello.dispatch")
public ModelMap handleRequest() {
return new ModelMap()
.addAttribute("text", "some text")
.addAttribute("cost", 42)
.addAttribute("config",
grailsApplication.getConfig().flatten()));
}
CONFIDENTIAL 16
18. Spring MVC
Use @Autowired for dependency injection (on fields in Groovy
classes, on setters or constructors in Java)
private GrailsApplication grailsApplication;
@Autowired
public void setGrailsApplication(GrailsApplication app) {
grailsApplication = app;
}
CONFIDENTIAL 18
26. A Simple Proxied Bean – The interface
package gr8conf.eu.spring;
public interface ThingManager {
void method1();
int methodTwo(boolean foo);
}
CONFIDENTIAL 26
27. A Simple Proxied Bean – The implementation
package gr8conf.eu.spring;
public class ThingManagerImpl
implements ThingManager {
public void method1() {
System.out.println("You called method1");
}
public int methodTwo(boolean foo) {
System.out.println("You called methodTwo");
return 42;
}
}
CONFIDENTIAL 27
28. A Simple Proxied Bean – The FactoryBean
package gr8conf.eu.spring;
public class ThingManagerProxyFactoryBean
implements FactoryBean<ThingManager>,
InitializingBean {
private ThingManager managerProxy;
private ThingManager target;
public ThingManager getObject() {
return managerProxy;
}
public Class<ThingManager> getObjectType() {
return ThingManager.class;
}
public boolean isSingleton() {
return true;
}
CONFIDENTIAL 28
29. A Simple Proxied Bean – The FactoryBean
public void setTarget(ThingManager manager) {
target = manager;
}
public void afterPropertiesSet() {
Assert.notNull(target,
"The proxied manager must be set");
Class<?>[] interfaces = { ThingManager.class };
InvocationHandler invocationHandler =
new InvocationHandler() {
public Object invoke(Object proxy,
Method m, Object[] args)
throws Throwable {
CONFIDENTIAL 29