Docker入门系列7:动态映射端口port mapping
2015-06-25 11:11
721 查看
接口类:
实现类:
AOP拦截器
测试类
配置文件
输出:
user:shawn
printUser---!
printUser user:hello!
package com.test.TestSpring3; public interface UserService // 被拦截的接口 { public void printUser(String user); }
实现类:
package com.test.TestSpring3; public class UserServiceImp implements UserService // 实现UserService接口 { public void printUser(String user) ...{ System.out.println("printUser user:" + user);// 显示user } }
AOP拦截器
package com.test.TestSpring3; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class UserInterceptor implements MethodInterceptor // AOP方法拦截器 { public Object invoke(MethodInvocation arg0) throws Throwable ...{ try { if (arg0.getMethod().getName().equals("printUser")) // 拦截方法是否是UserService接口的printUser方法 { Object[] args = arg0.getArguments();// 被拦截的参数 System.out.println("user:" + args[0]); arg0.getArguments()[0] = "hello!";// 修改被拦截的参数 } System.out.println(arg0.getMethod().getName() + "---!"); return arg0.proceed();// 运行UserService接口的printUser方法 } catch (Exception e) { throw e; } } }
测试类
package com.test.TestSpring3; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.web.context.support.WebApplicationContextUtils; public class TestInterceptor { public static void main(String[] args) { ApplicationContext ctx = new FileSystemXmlApplicationContext( "classpath:applicationContext.xml"); // ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService us = (UserService) ctx.getBean("userService"); us.printUser("shawn"); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="userServiceImp" class="com.test.TestSpring3.UserServiceImp" /> <bean id="userInterceptor" class="com.test.TestSpring3.UserInterceptor" /> <bean id="userService" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 代理接口 --> <property name="proxyInterfaces"> <value>com.test.TestSpring3.UserService</value> </property> <!-- 目标实现类 --> <property name="target"> <ref local="userServiceImp" /> </property> <!-- 拦截器 --> <property name="interceptorNames"> <list> <value>userInterceptor</value> </list> </property> </bean> </beans>
输出:
user:shawn
printUser---!
printUser user:hello!
相关文章推荐
- Docker常用命令
- DOCKER 给运行中的容器添加映射端口
- docker中 启动所有的容器命令
- 部署维护docker环境
- docker nsenter
- 使用docker构建jenkins镜像并运行容器
- 关于Docker在测试方面的应用
- Docker,云时代的程序交付方式
- Docker Networking Rules: EXPOSE, -p, -P, –link
- docker 设置tomcat自启动方法
- 如何在云服务提供商的平台上使用Docker Machine
- Docker折腾笔记
- 基于Docker、阿里云、JFinal的java saas平台搭建
- 创建支持ssh的docker镜像
- 除了OCP,DockerCon的主题演讲还说了些什么?
- Docker Centos安装Mysql5.6
- docker常用管理命令(上)
- docker使用总结
- Docker 1.7.0 深度解析
- Docker 创建安装了tomcat的镜像