Spring的核心接口
ContextLoaderListener接口
Create a new ContextLoaderListener
that will create a web application context based on the “contextClass” and “contextConfigLocation” servlet context-params. See ContextLoader
superclass documentation for details on default values for each.
This constructor is typically used when declaring ContextLoaderListener
as a <listener>
within web.xml
, where a no-arg constructor is required.
这个接口实现了J2EE的ServletContextListener接口
通过listener 像Servlet容器注册 Web容器启动时 初始化Spring上下文的信息
<!-- 加载Spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-core.xml</param-value> </context-param> <!-- 通过listener 像Servlet容器注册 Web容器启动时 初始化context-param的配置信息。--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
BeanFactory接口
Spring通过BeanFactory接口的getBean来拿到我们所配置Bean的实列,交给Spring管理的Bean全部默认是单例。
以下是批量扫描初始化Bean 交给Spring管理
<context:component-scan base-package="com.sk.service.*"></context:component-scan>
IOC
ioc 是依赖注入,当我们的成员变量是Spring的一个Bean的时候,那这个成员变量可以由Spring帮我们注入(Spring会通过反射调用Set方法)
依赖注入也叫控制反转,以前编程完完全全控制在我自己的手里。用了Spring之后 成员变量的初始化过程控制过程反转到Spring手里。
注解用法:
@Autowired
DemoService demoService;
AOP
从代理的原理我们知道,代理的目的是调用目标方法时可以转而执行InvocationHandler的invoke方法,所以如何在InvocationHandler上做文章就是Spring实现AOP的关键所在。
Spring的AOP实现遵守AOP联盟的约定,同时Spring又扩展了它。增加了 PointCut Advisor接口使得其更加灵活
<!-- 切面逻辑类的对象 -->
<bean id="myInterceptor" class="com.sk.util.MyInterceptor"></bean>
<aop:config>
<!-- 在add方法上加各种各样的我们切入进来的逻辑 -->
<aop:pointcut expression="execution(public * com.sk.service..*.*(..))" id="servicePointcut"/>
<aop:aspect id = "logAspect" ref="myInterceptor">
<!-- aop:pointcut可以加到aspect的里面来 加到里面的话 只能是logAspect 这个aspect使用 -->
<aop:before method="before" pointcut-ref="servicePointcut"/>
<aop:after method="after" pointcut-ref="servicePointcut"/>
</aop:aspect>
</aop:config>
当我们执行的时候 符合我们execution(public * com.sk.service..*.*(..))语法要求的方法的时候,
它会在方法执行之前 执行before方法(logInterceptor的before方法) 方法执行之后 执行after方法。
Junit测试
@Test public void testIoc() { BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext-core.xml"); DemoService demoService = beanFactory.getBean(DemoService.class); demoService.testAop(); }
利用JoinPoint模拟AOP 实现事物管理
JoinPoint:连接点(AOP切面切到我们程序时的连接点,切入的那个点)
上面的配置保持不变,首先给我们的Service 加一个自定义的注解
@Inherited @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) public @interface MyAnnotation { public String transaction() default ""; }
@Service public class DemoServiceImpl implements DemoService{ @Override @MyAnnotation(transaction= "transaction") public void testAop() { System.out.println("excute Service***********"); } }
完善我们的切面逻辑类
当我们的切面发现这个注解的时候 就进行事物的控制
public class MyInterceptor { public void before(JoinPoint jp) throws Exception { MyAnnotation myAnnotation =getHandlerChain(jp); System.out.println("方法开始通过AOP拿到方法上的注解-开始事物"+myAnnotation.transaction()); } public void after(JoinPoint jp)throws Exception { MyAnnotation myAnnotation =getHandlerChain(jp); System.out.println("放过结束通过AOP拿到方法上的注解-结束事物"+myAnnotation.transaction()); } // 取方法或者类上的HandlerChain注解,方法上的优先 private MyAnnotation getHandlerChain(JoinPoint jp) throws Exception { MethodSignature methodSignature = (MethodSignature) jp.getSignature(); Method realMethod = jp.getTarget().getClass().getDeclaredMethod(methodSignature.getName(), methodSignature.getParameterTypes()); MyAnnotation myAnnotation = realMethod.getAnnotation(MyAnnotation.class); if(myAnnotation==null) { Class<? extends Object> cls = jp.getTarget().getClass(); myAnnotation = (MyAnnotation) cls.getAnnotation(MyAnnotation.class); } return myAnnotation; } }
打印结果
方法开始通过AOP拿到方法上的注解-开始事物transaction
excute Service***********
放过结束通过AOP拿到方法上的注解-结束事物transaction
这里只是打印模拟,项目中需要和JDBC(或者Mybatis Hibernate)结合,进而控制数据库transaction的开启和关闭