用命令行启动spring
A first example
Let us assume that you are an application developer who has been tasked with diagnosing the cause of
some performance problems in a system. Rather than break out a profiling tool, what we are going to do
Spring Framework
3.1 Reference Documentation 238
is switch on a simple profiling aspect that will enable us to very quickly get some performance metrics, so
that we can then apply a finer-grained profiling tool to that specific area immediately afterwards.
Here is the profiling aspect. Nothing too fancy, just a quick-and-dirty time-based profiler, using the
@AspectJ-style of aspect declaration.
package foo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;
import org.springframework.core.annotation.Order;
@Aspect
public class ProfilingAspect {
@Around("methodsToBeProfiled()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
StopWatch sw = new StopWatch(getClass().getSimpleName());
try {
sw.start(pjp.getSignature().getName());
return pjp.proceed();
} finally {
sw.stop();
System.out.println(sw.prettyPrint());
}
}
@Pointcut("execution(public * foo..*.*(..))")
public void methodsToBeProfiled(){}
}
We will also need to create an 'META-INF/aop.xml' file, to inform the AspectJ weaver that we want to
weave our ProfilingAspect into our classes. This file convention, namely the presence of a file (or
files) on the Java classpath called ' META-INF/aop.xml' is standard AspectJ.
<!DOCTYPE aspectj PUBLIC
"-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<!-- only weave classes in our application-specific packages -->
<include within="foo.*"/>
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="foo.ProfilingAspect"/>
</aspects>
</aspectj>
Now to the Spring-specific portion of the configuration. We need to configure a LoadTimeWeaver (all
explained later, just take it on trust for now). This load-time weaver is the essential component
Spring Framework
3.1 Reference Documentation 239
responsible for weaving the aspect configuration in one or more 'META-INF/aop.xml' files into the
classes in your application. The good thing is that it does not require a lot of configuration, as can be seen
below (there are some more options that you can specify, but these are detailed later).
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- a service object; we will be profiling its methods -->
<bean id="entitlementCalculationService"
class="foo.StubEntitlementCalculationService"/>
<!-- this switches on the load-time weaving -->
<context:load-time-weaver/>
</beans>
Now that all the required artifacts are in place - the aspect, the 'META-INF/aop.xml' file, and the
Spring configuration -, let us create a simple driver class with a main(..) method to demonstrate the
LTW in action.
package foo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml", Main.class);
EntitlementCalculationService entitlementCalculationService
= (EntitlementCalculationService) ctx.getBean("entitlementCalculationService");
// the profiling aspect is 'woven' around this method execution
entitlementCalculationService.calculateEntitlement();
}
}
There is one last thing to do. The introduction to this section did say that one could switch on LTW
selectively on a per-ClassLoader basis with Spring, and this is true. However, just for this example,
we are going to use a Java agent (supplied with Spring) to switch on the LTW. This is the command line
we will use to run the above Main class:
java -javaagent:C:/projects/foo/lib/global/spring-instrument.jar foo.Main
The '-javaagent' is a Java 5+ flag for specifying and enabling agents to instrument programs running
on the JVM. The Spring Framework ships with such an agent, the InstrumentationSavingAgent,
which is packaged in the spring-instrument.jar that was supplied as the value of the
-javaagent argument in the above example.