Skip to content

Instantly share code, notes, and snippets.

@deluan
Created October 9, 2010 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deluan/618739 to your computer and use it in GitHub Desktop.
Save deluan/618739 to your computer and use it in GitHub Desktop.
import com.deluan.gaelyk.SpringApplicationContext
def dateFormat = SpringApplicationContext.getBean('dateFormat')
def now = new Date()
println dateFormat.format(now)
def now = new Date()
// Simple context lookup
def dateFormat = getBean('dateFormat')
// Resolve a list of bean names and create bindings for each one of them
autowire this, ['timeFormat', 'dateTimeFormat']
html.html {
body {
p appCtx.displayName // Access the ApplicationContext
p dateFormat.format(now)
p timeFormat.format(now)
p dateTimeFormat.format(now)
}
}
install spring
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<?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="springApplicationContext" class="com.deluan.gaelyk.SpringApplicationContext"/>
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy/MM/dd" />
</bean>
<bean id="timeFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="hh:mm:ss" />
</bean>
<bean id="dateTimeFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy/MM/dd hh:mm:ss" />
</bean>
</beans>
import com.deluan.gaelyk.SpringApplicationContext
binding {
appCtx = SpringApplicationContext.context
getBean = {
SpringApplicationContext.getBean(it)
}
autowire = { self, beanNames ->
beanNames.each {
self.setProperty(it, SpringApplicationContext.getBean(it))
}
}
}
package com.deluan.gaelyk;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* Wrapper to always return a reference to the Spring Application Context from
* within non-Spring enabled beans. Unlike Spring MVC's WebApplicationContextUtils
* we do not need a reference to the Servlet context for this. All we need is
* for this bean to be initialized during application startup.
*
* Original from http://sujitpal.blogspot.com/2007/03/accessing-spring-beans-from-legacy-code.html
*/
public class SpringApplicationContext implements ApplicationContextAware {
private static ApplicationContext CONTEXT;
/**
* This method is called from within the ApplicationContext once it is
* done starting up, it will stick a reference to itself into this bean.
* @param context a reference to the ApplicationContext.
*/
public void setApplicationContext(ApplicationContext context) throws BeansException {
CONTEXT = context;
}
/**
* This is about the same as context.getBean("beanName"), except it has its
* own static handle to the Spring context, so calling this method statically
* will give access to the beans by name in the Spring application context.
* As in the context.getBean("beanName") call, the caller must cast to the
* appropriate target class. If the bean does not exist, then a Runtime error
* will be thrown.
* @param beanName the name of the bean to get.
* @return an Object reference to the named bean.
*/
public static Object getBean(String beanName) {
return CONTEXT.getBean(beanName);
}
public static ApplicationContext getContext() {
return CONTEXT;
}
}
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment