日期:2014-05-18 浏览次数:20948 次
public interface WorkInterface {
public void dowork();
}
@Component("master")
public class Master implements WorkInterface{
@Override
public void dowork() {
// TODO Auto-generated method stub
System.out.println("Working after meal...");
}
}
public class DynamicProxyFactory{
@Resource
private Servant servant;
// @Before("execution(* com.spring.WorkInterface.*(..))")
@Before("execution(* com.spring.Master.*(..))")
public void before(){
servant.dowork();
}
}
public class Test {
public static void main(String[] args) {
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
WorkInterface master=(WorkInterface)factory.getBean("master");
master.dowork();
}
}
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 启用组件扫描 -->
<context:component-scan base-package="com.spring"/>
<!-- 启用注解注入 -->
<context:annotation-config/>
<!-- 启用AspectJ的-->
<aop:aspectj-autoproxy />
</beans>