Permalink
Browse files

Merged changes from dcardon :)

  • Loading branch information...
1 parent f9ace55 commit 098070a919b4d0e351945dd90524bac328b2cf23 @pepite committed Mar 7, 2010
Showing with 78 additions and 19 deletions.
  1. +2 −1 .gitignore
  2. +59 −0 README.textile
  3. +1 −0 src/play/modules/spring/SpringEnhancer.java
  4. +16 −18 src/play/modules/spring/SpringPlugin.java
View
@@ -2,4 +2,5 @@
build.properties
/dist
lib/play-spring.jar
-
+*.ipr
+*.iws
View
@@ -0,0 +1,59 @@
+h1. Spring module
+
+The spring support module help you to integrate Spring managed beans with a play application.
+
+h2. <a>Enable the Spring module for the application</a>
+
+In the **/conf/application.conf** file, enable the Spring module by adding this line:
+
+bc. # The spring module
+module.spring=${play.path}/modules/spring
+
+h2. <a>Define an application-context.xml registry</a>
+
+In the **conf/** directory of the application you can then create a **application-context.xml** file and define some beans.
+
+For example:
+
+bc. <?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springsource.org/dtd/spring-beans-2.0.dtd">
+<beans>
+
+ <bean id="test" class="utils.Test" />
+
+</beans>
+
+h2. <a>Retrieve beans from application code</a>
+
+You can obtain bean instances from the application code, using the **play.modules.spring.Spring** helper.
+
+bc. public Application extends Controller {
+
+ public static void index() {
+ Test test = Spring.getBeanOfType(Test.class);
+ ...
+ }
+
+}
+
+h2. <a>@javax.inject.Inject support</a>
+
+Not implemented yet.
+
+h2. <a>Automatic Component Scanning</a>
+
+Spring 2.5 and later supports auto-wiring and component identification using annotations, obviating the need to define beans explicitly in XML.
+
+To enable component scanning, add this line to the **/conf/application.conf** file:
+
+bc. play.spring.component-scan=true
+
+Note that enabling this scans for @org.springframework.stereotype.Component, @org.springframework.stereotype.Repository, @org.springframework.stereotype.Service to identify Spring beans. Additionally, running the component scan enables support for annotation based configuration (i.e., @org.springframework.beans.factory.annotation.Autowired, etc).
+
+h3. Limited Scanning
+
+You can limit the classes scanned to specific packages or canonical class names with this line in the **/conf/application.conf** file:
+
+bc. play.spring.component-scan.base-packages=controllers.beans,services
+
+In this example, only classes whose canonical name start with either 'controllers.beans' or 'services' will be scanned and identified as beans.
@@ -14,6 +14,7 @@ public void enhanceThisClass(ApplicationClass applicationClass) throws Exception
return;
}
for (CtField ctField : ctClass.getDeclaredFields()) {
+ // TODO: FIXME
if(hasAnnotation(ctField, "javax.inject.Inject")) {
//ctClass.removeField(ctField);
//CtField newCtField = new CtField(ctField.getType(), ctField.getName(), ctClass);
@@ -4,6 +4,7 @@
import java.io.InputStream;
import java.net.URL;
import java.util.Map;
+
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
@@ -21,15 +22,13 @@
import play.vfs.VirtualFile;
public class SpringPlugin extends PlayPlugin implements BeanSource {
- /**
- * Component scanning constants.
- */
- private static final String PLAY_SPRING_COMPONENT_SCAN_FLAG = "play.spring.component-scan";
+ /**
+ * Component scanning constants.
+ */
+ private static final String PLAY_SPRING_COMPONENT_SCAN_FLAG = "play.spring.component-scan";
private static final String PLAY_SPRING_COMPONENT_SCAN_BASE_PACKAGES = "play.spring.component-scan.base-packages";
- private static final String TRUE_STR = "true";
- private static final String FALSE_STR = "false";
-
- public static GenericApplicationContext applicationContext;
+
+ public static GenericApplicationContext applicationContext;
private long startDate = 0;
@Override
@@ -58,8 +57,8 @@ public void enhance(ApplicationClass applicationClass) throws Exception {
@Override
public void onApplicationStart() {
- URL url = this.getClass().getClassLoader().getResource(Play.id+".application-context.xml");
- if(url == null) {
+ URL url = this.getClass().getClassLoader().getResource(Play.id + ".application-context.xml");
+ if (url == null) {
url = this.getClass().getClassLoader().getResource("application-context.xml");
}
if (url != null) {
@@ -76,12 +75,11 @@ public void onApplicationStart() {
//
// Check for component scan
//
- boolean doComponentScan = Play.configuration.getProperty(PLAY_SPRING_COMPONENT_SCAN_FLAG,FALSE_STR).equals(TRUE_STR);
+ boolean doComponentScan = Play.configuration.getProperty(PLAY_SPRING_COMPONENT_SCAN_FLAG, "false").equals("true");
Logger.debug("Spring configuration do component scan: " + doComponentScan);
- if (doComponentScan)
- {
+ if (doComponentScan) {
ClassPathBeanDefinitionScanner scanner = new PlayClassPathBeanDefinitionScanner(applicationContext);
- String scanBasePackage = Play.configuration.getProperty(PLAY_SPRING_COMPONENT_SCAN_BASE_PACKAGES,"");
+ String scanBasePackage = Play.configuration.getProperty(PLAY_SPRING_COMPONENT_SCAN_BASE_PACKAGES, "");
Logger.debug("Base package for scan: " + scanBasePackage);
Logger.debug("Scanning...");
scanner.scan(scanBasePackage.split(","));
@@ -121,12 +119,12 @@ public void onApplicationStart() {
}
public <T> T getBeanOfType(Class<T> clazz) {
- Map<String,T> beans = applicationContext.getBeansOfType(clazz);
- if(beans.size() == 0) {
+ Map<String, T> beans = applicationContext.getBeansOfType(clazz);
+ if (beans.size() == 0) {
return null;
}
return beans.values().iterator().next();
}
-
-
+
+
}

0 comments on commit 098070a

Please sign in to comment.