Sillycat Framework(I)Enhancement of Easygroovyplugin

Sillycat Framework(I)Enhancement of Easygroovyplugin

1. enhance the plugin, add 'source-key' to the configuration

Codes in GroovyScanBeanDefinitionParser:
String key = element.getAttribute("source-key");
if (null == key || "".equalsIgnoreCase(key.trim())
|| "null".equals(key.trim())) {
key = "groovy.file.path";
}
String filepath = SystemConfiguration.getString(key);

configuration in xsd, groovy.xsd:
<xsd:complexType>
<xsd:attribute name="source-pattern" type="xsd:string"
use="required" />
<xsd:attribute name="source-key" type="xsd:string" />
</xsd:complexType>

configuration in easygroovy.properties:
easymarket.groovy.file.path=file:/home/luohua/work/restserver

configuration in spring:
<groovy:scan source-pattern="/groovy/com/sillycat/easymarket/**/*.groovy"
                              source-key="easymarket.groovy.file.path"/>

2. give the bean a id in detail

codes in GroovyScanBeanDefinitionParser.java:

String beanId = parseBeanId(resource.getFile().getAbsolutePath());
//bd.setBeanClassName(beanId);
logger.info("register beanId=" + beanId);
parserContext.getRegistry().registerBeanDefinition(beanId, bd);

private String parseBeanId(String fullName) {
String beanId = "";
int start = fullName.lastIndexOf("/") + 1;
int end = fullName.indexOf(".groovy");
beanId = fullName.substring(start, end);
//beanId.replace("Impl", "");
beanId = beanId.substring(0,1).toLowerCase() + beanId.substring(1);
end = beanId.lastIndexOf("Impl");
if(end > 0){
beanId = beanId.substring(0,end);
}
return beanId;
}

The JUNIT testcase can be run then:

public void setUp() throws Exception {
super.setUp();
personService = (PersonService) ctx.getBean("personService");
}
public void tearDown() throws Exception {
super.tearDown();
}
public void testGet(){
Person p = personService.get(Integer.valueOf(1));
assertNotNull(p);
System.out.println(p);
}

你可能感兴趣的:(spring,bean,JUnit,groovy)