基于spring的introduce实现

本文介绍了一个使用Spring AOP实现的示例项目,通过定义代理工厂、目标类和引入类,展示了如何为业务类添加横切关注点。项目中包含了配置文件、测试用例和依赖管理,演示了AOP在实际应用中的工作流程。
siye@r480:~/svlution/workspace/springaop4322$ tree src/
src/
├── main
│   ├── java
│   │   ├── log4j.properties
│   │   └── ocn
│   │       └── site
│   │           └── springaop
│   │               ├── introduce
│   │               │   ├── DelegatingIntroductionInterceptorImpl.java
│   │               │   └── Person.java
│   │               ├── setup
│   │               │   └── Appconfig.java
│   │               └── target
│   │                   └── User.java
│   └── resources
└── test
    ├── java
    │   └── ocn
    │       └── site
    │           └── springaop
    │               └── target
    │                   └── Runtest.java
    └── resources
        └── config
            └── application.xml

17 directories, 7 files
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.22.RELEASE</version>
    <scope>test</scope>
</dependency>
package ocn.site.springaop.introduce;

import org.apache.log4j.Logger;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;
import org.springframework.stereotype.Component;

// should be read doc 4 superclass Advice
@Component
public class DelegatingIntroductionInterceptorImpl extends DelegatingIntroductionInterceptor implements Person {

	private static final long serialVersionUID = 1L;
	private final Logger logger = Logger.getLogger(this.getClass());

	@Override
	public void eat() {
		logger.info("补充能量");
	}

}
package ocn.site.springaop.introduce;

public interface Person {

	public void eat();

}
package ocn.site.springaop.setup;

import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan({ "ocn.site.springaop.introduce", "ocn.site.springaop.target" })
public class Appconfig {

	@Bean
	public ProxyFactory getProxyFactory() {
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setProxyTargetClass(true);
		return proxyFactory;
	}

}
package ocn.site.springaop.target;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class User {

	private final Logger logger = Logger.getLogger(this.getClass());

	public void work() {
		logger.info("工作任务");
	}

}
<?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.xsd">

	<bean class="org.springframework.aop.framework.ProxyFactory">
		<property name="proxyTargetClass" value="true"></property>
	</bean>

	<bean class="ocn.site.springaop.target.User"></bean>

	<bean class="ocn.site.springaop.introduce.DelegatingIntroductionInterceptorImpl"></bean>

</beans>
package ocn.site.springaop.target;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import ocn.site.springaop.introduce.DelegatingIntroductionInterceptorImpl;
import ocn.site.springaop.introduce.Person;

@RunWith(SpringRunner.class)
//@ContextConfiguration(classes = Appconfig.class)// used java-config
@ContextConfiguration("classpath:config/application.xml")
public class Runtest {

	private @Autowired ProxyFactory proxyFactory;
	private @Autowired User target;
	private @Autowired DelegatingIntroductionInterceptorImpl introductionInterceptorImpl;

	@Test
	public void run() throws Exception {
		proxyFactory.setTarget(target);
		proxyFactory.addAdvice(introductionInterceptorImpl);
		User user = (User) proxyFactory.getProxy();
		user.work();
		Person person = (Person) user;
		person.eat();
	}

}
19-09-05 16:41:13 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-05 16:41:13 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@6615435c, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4909b8da, org.springframework.test.context.support.DirtiesContextTestExecutionListener@3a03464]
19-09-05 16:41:13 org.springframework.context.support.GenericApplicationContext  =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@7823a2f9: startup date [Thu Sep 05 16:41:13 CST 2019]; root of context hierarchy
19-09-05 16:41:14 ocn.site.springaop.target.User  =====>>> 工作任务
19-09-05 16:41:14 ocn.site.springaop.introduce.DelegatingIntroductionInterceptorImpl  =====>>> 补充能量
19-09-05 16:41:14 org.springframework.context.support.GenericApplicationContext  =====>>> Closing org.springframework.context.support.GenericApplicationContext@7823a2f9: startup date [Thu Sep 05 16:41:13 CST 2019]; root of context hierarchy
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值