关于面试别问及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 ...
随机推荐
- Magento的基本架构解析
Magento的基本架构解析 magento 是在Zend框架基础上建立起来的,这点保证了代码的安全性及稳定性.选择Zend的原因有很多,但是最基本的是因为 zend框架提供了面向对象的代码库并且有很 ...
- .NET Core Analysis
.NET Core 1.0.1 Module Component .NET Core MongoDB MongoDB.Driver There has a nuget package availabl ...
- centos 安装pptp
1. 安装依赖 ppp yum -y install ppp 2. 编译安装pptpd wget http://jaist.dl.sourceforge.net/project/poptop/pptp ...
- xshell 语句
使用命令 cd 切换到tomcat的bin目录,如:cd /root/Test_APP_Project_CRM/bin 使用命令 [ ./startup.sh ]启动tomcat服务../star ...
- async?
Here, I want to record one thing, as to async and await methods, I've seen many misuses. Since these ...
- My Env
Font -- YaHei Consolas Hybrid YaHei ConsolasAsume that we put the font file in /usr/share/fonts/myfo ...
- 性能调优利器之strace
最近需要对一个自己开发的socket server的性能进行分析,刚开始还想了好长时间怎么来分析.后来才意识到其实使用strace就足够了. 观察到的现象是server单进程CPU使用率97,但磁盘i ...
- 盒子模型(W3C盒子模型、IE盒子模型)
盒子模型:一个物体在页面中所占据的位置 盒子模型包含以下几种元素: padding:margin:content:border 这是大家都知道的,也是书本上定义说明的,但是在ie的情况下是有点区别的; ...
- js通过隐藏iframe修改session值
js:function selects(id, ss) { window.frames["UpSession"].window.location.href = "../U ...
- 霍尼韦尔FC400A与FC400B的区别
给霍尼韦尔官方打电话咨询了下,发现两者区别不大,唯一的区别是400B可以和主机联动,也就是主机关的时候,400B也可以自动关闭,不需要手动去关闭电源,这样非常方便. 本来官方是只有400A的时候,但是 ...