关于面试别问及Spring如何回答思路总结!
首先要知道 Spring两大核心IOC和AOP(Java轻量级业务层框架Spring两大核心IOC和AOP原理)
IOC:
1.从Java最基本的创建对象开始
如Interface Driven Design接口驱动,接口驱动有很多好处,可以提供不同灵活的子类实现,增加代码稳定和健壮性等等,但是接口一定是需要实现的,也就是如下语句迟早要执行:AInterface a = new AInterfaceImp(); 这样一来,耦合关系就产生了,如:
classA
{
AInterface a; A(){} AMethod()//一个方法
{
a = new AInterfaceImp();
}
}
InterfaceImplFactory
{
AInterface create(Object condition)
{
if(condition == condA)
{
return new AInterfaceImpA();
}
else if(condition == condB)
{
return new AInterfaceImpB();
}
else
{
return new AInterfaceImp();
}
}
}
Filter的实现、interceputor的实现以及struts2的拦截器的实现都是AOP思想的体现
下面一个例子来说明问题
引用于:http://supben.iteye.com/blog/1520126
package com.supben.advice; import java.lang.reflect.Method; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice; /**
* 实现spring advice 接口
*
* @author shencl
*
*/
public class TestAdvice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice { private static final Logger log = LoggerFactory.getLogger(TestAdvice.class); /**
* before 通知
*/
public void before(Method method, Object[] args, Object target) throws Throwable {
log.info(target.getClass().getSimpleName() + "类的" + method.getName() + "方法,执行TestAdvice的before通知");
// 通知要做的业务
if (method.getName().startsWith("get")) {
log.info("只有方法名是以get开始的方法,才会执行到这句话....");
}
} /**
* after 通知
*/
public void afterReturning(Object arg0, Method method, Object[] arg2, Object target) throws Throwable {
log.info(target.getClass().getSimpleName() + "类的" + method.getName() + "方法,执行TestAdvice的after通知");
} /**
* 异常通知
*/
public void afterThrowing(Method method, Object[] args, Object target, Exception ex) throws Throwable {
log.info(target.getClass().getSimpleName() + "类的" + method.getName() + "方法,执行TestAdvice的throwing通知");
} }
配置文件
<?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"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config />
<!-- 扫描com.supben 下所有的包-->
<context:component-scan base-package="com.supben" /> <bean id="testAdvice" class="com.supben.advice.TestAdvice" />
<aop:config>
<aop:advisor pointcut="execution(* *..service.*Service.*(..))"
advice-ref="testAdvice" />
</aop:config> </beans>
service接口
package com.supben.service;
public interface FirstService {
public void get();
public void exception();
}
service实现类
package com.supben.service.impl; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import com.supben.service.FirstService; @Service("firstService")
public class FirstServiceImpl implements FirstService {
private static final Logger log = LoggerFactory.getLogger(FirstServiceImpl.class); public void get() {
log.info("方法执行ing.....");
} public void exception() {
throw new RuntimeException("测试异常");
}
}
测试类
package com.supben.test;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.supben.service.FirstService;
import com.supben.spring.SpringContextUtil;
public class ServiceTest extends TestCase {
/**
* 装载spring 配置文件
*/
static {
new ClassPathXmlApplicationContext("application.xml");
}
@Test
public void testGet() {
FirstService service = SpringContextUtil.getBean("firstService");
service.get();
}
@Test
public void testGet2() {
FirstService service = SpringContextUtil.getBean("firstService");
service.exception();
}
}
测试结果:
2012-05-09 15:10:10,028 INFO [com.supben.advice.TestAdvice] - FirstServiceImpl类的get方法,执行TestAdvice的before通知
2012-05-09 15:10:10,030 INFO [com.supben.advice.TestAdvice] - 只有方法名是以get开始的方法,才会执行到这句话....
2012-05-09 15:10:10,032 INFO [com.supben.service.impl.FirstServiceImpl] - 方法执行ing.....
2012-05-09 15:10:10,032 INFO [com.supben.advice.TestAdvice] - FirstServiceImpl类的get方法,执行TestAdvice的after通知
2012-05-09 15:10:10,035 INFO [com.supben.advice.TestAdvice] - FirstServiceImpl类的exception方法,执行TestAdvice的before通知
2012-05-09 15:10:10,035 INFO [com.supben.advice.TestAdvice] - FirstServiceImpl类的exception方法,执行TestAdvice的throwing通知
结果分析:
get方法满足执行之前会 执行 before通知,执行完成后会执行after通知。
exception方法执行之前会执行before 通知,因为方法名不是以get开头,所以不会执行before通知的业务逻辑。然后出现异常了会执行throwing通知,因为出异常了,方法没有执行完毕,所以不会触发after通知。
概念:
切面(aspect):
翻译成外貌更合适:整个程序相当于一个密封的圆柱体,即一个外貌,现在要面向这个东西编程,在不改变原来类(FirstServiceImpl)的情况下,改变里边的代码。通知(advice):TestAdvice里边的before,after,throwing方法都是通知。
常见的有前置通知,后置通知,异常通知。
切点(cut-point):定义通知应该应用在哪些地方,本例是FirstServiceImpl中的get方法和exception方法,一般用正则表达式定义。
切点表达式:配置文件中的execution(* *..service.*Service.*(..))
是一个切点表达式,表示的是一个一个的方法.比如本例中的表达式,意思是 包目录的最后一级是service,类/接口名 后缀为Service的
class文件里的,方法名为任意名称,参数个数不限的 方法。 * 表示任意,(..)表示方法参数个数不限。
目标对象(traget):FirstService就是目标对象。
此外还有两个重要的概念
引入(Introduction):允许为已存在类添加新方法和属性。
代理(Proxy):将通知应用到目标对象后创建的对象。
关于面试别问及Spring如何回答思路总结!的更多相关文章
- BAT面试官告诉你如何回答你的职业规划
前言(Why) 在面试中不论是在一面二面三面这种技术面,还是在最后的hr面,经常会被人问及,"谈谈你的职业规划"这种问题,我们回答的很可能会给我们的面试表现加分,如果回答地不好,对 ...
- 面试常问Spring IOC,不得不会。
广义的 IOC IoC(Inversion of Control) 控制反转,即“不用打电话过来,我们会打给你”. 两种实现: 依赖查找(DL)和依赖注入(DI). IOC 和 DI .DL 的关系( ...
- java web方面的面试问题,Spring MVC方面的面试问题,摘自java web轻量级开发面试教程
本文摘自java web轻量级开发面试教程: https://baike.baidu.com/item/Java%20Web%E8%BD%BB%E9%87%8F%E7%BA%A7%E5%BC%80%E ...
- 面试官:“谈谈Spring中都用到了那些设计模式?”。
我自己总结的Java学习的系统知识点以及面试问题,已经开源,目前已经 41k+ Star.会一直完善下去,欢迎建议和指导,同时也欢迎Star: https://github.com/Snailclim ...
- 《PHP程序员面试笔试宝典》——如何回答算法设计问题?
如何巧妙地回答面试官的问题? 本文摘自<PHP程序员面试笔试宝典> 程序员面试中的很多算法设计问题,都是历年来各家企业的"炒现饭",不管求职者以前对算法知识掌握得是否扎 ...
- 《PHP程序员面试笔试宝典》——如何回答快速估算类问题?
如何巧妙地回答面试官的问题? 本文摘自<PHP程序员面试笔试宝典> 有些大企业的面试官,总喜欢出一些快速估算类问题,对他们而言,这些问题只是手段,不是目的,能够得到一个满意的结果固然是他们 ...
- 《PHP程序员面试笔试宝典》——如何回答非技术性问题?
如何巧妙地回答面试官的问题? 本文摘自<PHP程序员面试笔试宝典> 评价一个人的能力,除了专业能力,还有一些非专业能力,如智力.沟通能力和反应能力等,所以在IT企业招聘过程的笔试.面试环节 ...
- 《PHP程序员面试笔试宝典》——如何回答技术性的问题?
如何巧妙地回答面试官的问题? 本文摘自<PHP程序员面试笔试宝典> 程序员面试中,面试官会经常询问一些技术性的问题,有的问题可能比较简单,都是历年的面试.笔试真题,求职者在平时的复习中会经 ...
- 基于spring的placeholder思路处理配置信息敏感信息加密解密的实践
基于Spring的placeholder处理思路,实现系统配置信息敏感信息的加密解密处理. 我们的处理方案,是基于类org.springframework.beans.factory.config.P ...
随机推荐
- Date.prototype.format
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...
- python知识点总结
此知识要点,是根据学习廖雪峰phthon3.0教程总结的,所以结构基本和这个教程的结构相同. 背景知识 python是什么?(1)python是一门编程语言,意味着可以用python编写程序,完成一定 ...
- JS中的prototype
JS中的phototype是JS中比较难理解的一个部分 本文基于下面几个知识点: 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个 ...
- 最简单的推送--uexGetui
个推插件使用指南 配置方法这里不再复述,详情请参见插件接入指引 怎样创建一个最简单的推送? //只需要两个方法 uexGetui.initialize(data); uexGetui.onInitia ...
- 关于IE11版本下JS中时间判断的问题
最近在做代码的优化及浏览器的兼容问题.遇到了谷歌.火狐.360兼容模式.IE(8以上)版本对时间判断大小的问题 . 在谷歌.火狐.360.IE11以下IE8以上版本下 var d1="201 ...
- Windows访问Ubuntu14.04远程桌面全攻略
最近接到一个任务,在Ubuntu系统下开发一个串口读写程序.开发要在台式机上进行,安装UbuntuKylin 14.04,并且在串口连接了设备.个人使用的是笔记本电脑,系统是Windows8.1.自然 ...
- qt qml 刮刮卡效果
用canvas+mouseArea实现的刮刮卡效果. 表层是一层色彩,用手指划开,可看到下面的文字Lisence: MIT, 请保留本文档说明Author: surfsky.cnblogs.com 2 ...
- catalina
用catalina启动tomcat容器,将项目放到tomcat中,通过cmd:启动:catalina,相当于本地的测试环境.
- apache2.4配置Django1.7运行环境
系统环境Centos 6.5 这篇文章不适用6以下的系统,因为会碰到这个错误 [Mon Sep 22 18:13:02 2014] [error] [client 10.209.75.90] Trun ...
- HTTP/TCP
转:http://blog.csdn.net/sundacheng1989/article/details/28239711 http://blog.csdn.net/sundacheng1989/a ...