Datasheet
public void setOps(Operation ops) {
this.ops = ops;
}
public void setWriter(ResultWriter writer) {
wtr = writer;
}
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(
“beans.xml”);
BeanFactory factory = (BeanFactory) context;
CalculateSpringAOP calc =
(CalculateSpringAOP) factory.getBean(“opsbean”);
calc.execute(args);
}
public void execute(String [] args) {
long op1 = Long.parseLong(args[0]);
long op2 = Long.parseLong(args[1]);
wtr.showResult(“The result of “ + op1 +
ops.getOpsName() + op2 + “ is “
+ ops.operate(op1, op2) + “!”);
}
}
Note that this main code has not been modified in any way. The aspect is applied via Spring AOP (under-
neath the covers it’s using dynamic proxies) without requiring changes to the target body of code.
Wiring in AOP Proxies
As with other Spring techniques, the AOP magic is wired via the configuration in the Spring context
descriptor. If you take a look at the
beans.xml file used for this application (look in the src/chapter1/
springaop/src/main/resources
directory), you can see how the aspect is specified and applied. The
following highlighted code is responsible for the application of the aspect:
<?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:aop=”http://www.springframework.org/schema/aop”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd”>
23
Chapter 1: Jump Start Spring 2
01612c01.qxd:WroxPro 10/31/07 10:42 AM Page 23