使用Groovy对字符串进行弹簧注入

时间:2015-03-23 23:36:06

标签: spring groovy

这个例子很有效,我认为唯一改变的是我安装的java和groovy的版本。

@Grab('org.springframework:spring-context:3.2.1.RELEASE')
@Grab('org.apache.commons:commons-lang3:3.1')
@GrabConfig(systemClassLoader = true)
import org.springframework.context.support.ClassPathXmlApplicationContext

def ctx = new ClassPathXmlApplicationContext("classpath:context.xml")
def su = ctx.getBean('su')
def result = su.swapCase('Fee Fi Fo Fum') 
assert result == 'fEE fI fO fUM'
println "result = $result"

这是context.xml文件:

<?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-3.0.xsd">
   <bean id="su" class="org.apache.commons.lang3.StringUtils"/>
   <bean id="data" class="java.lang.String"><constructor-arg   value="Fee Fi Fo Fum"/></bean>
</beans>

如果我注释掉它运行的上下文文件的最后一行的第二行。

这曾经起作用,现在不起作用。我现在在做什么?

我正在跑步

  • Groovy 2.3.0-beta-2

  • Java 1.8.0_05。

这是日志/跟踪:

Mar 23, 2015 4:28:26 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@73d983ea: startup date [Mon Mar 23 16:28:26 PDT 2015]; root of context hierarchy
Mar 23, 2015 4:28:26 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [context.xml]
Mar 23, 2015 4:28:27 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@53dbe163: defining beans [su,data]; root of factory hierarchy
Caught: java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.core.LocalVariableTableParameterNameDiscoverer.inspectClass(LocalVariableTableParameterNameDiscoverer.java:110)
    at org.springframework.core.LocalVariableTableParameterNameDiscoverer.getParameterNames(LocalVariableTableParameterNameDiscoverer.java:85)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:193)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1049)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at works.run(works.groovy:14)

Process compilation exited abnormally with code 1

1 个答案:

答案 0 :(得分:0)

至少需要Spring 4才能使用Java 8进行编译。

在此宣布:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/new-in-4.0.html

您使用的是Spring 3.2,您必须使用Java 7。