一、IOC和DI
1.IOC和DI的区别
IOC:控制反转:将对象的创建权,由SPRing管理.
DI:依赖注入:在Spring创建对象的过程中,把对象依赖的属性注入到类中.
2.Spring 框架配置文件 BeanFactory与applicationContext 的区别
①ApplicationContext类继承了BeanFactory,并对BeanFactory提供了扩展,如国际化处理、事件传递、Bean自动装配。早期开发使用BeanFactory。
②BeanFactory是通过getBean()方法的时候才会加载这个类;ApplicationContext类在加载配置文件的时候,就创建所有的类。
3.IOC装配Bean
①Spring框架Bean实例化的方式:构造方法实例化:(默认无参数)
<bean id="bean1" class="cn.it.spring.demo.Bean1"></bean>
其中:id遵守xml约束,值唯一,必须以字母开头。还有scope属性:主要有singleton单例(默认的值) prototype多例。
初始化和销毁的方法init-method、destroy-method。
②Bean的生命周期
1.bean对象实例化
2.封装对象属性
3.如果Bean实现BeanNameAware 执行setBeanName
4.如果Bean实现BeanFactoryAware 或者ApplicationContextAware,设置工厂setBeanFactory 或者上下文对象setApplicationContext
5.如果存在类实现BeanPostProcessor(后处理Bean),执行postProcessBeforeInitialization
6.如果Bean实现InitializingBean 执行afterPropertiesSet
7.调用<bean init-method="init">指定初始化方法 init
8.如果存在类实现BeanPostProcessor(处理Bean),执行postProcessAfterInitialization
9.执行业务处理
10.如果Bean实现 DisposableBean 执行destroy
11.调用<beandestroy-method="customerDestroy"> 指定销毁方法 customerDestroy
③Bean中属性注入之setter方法注入
<bean id="car2" class="cn.it.spring.demo.Car">
<!-- <property>标签中name就是属性名称,value是普通属性的值,ref:引用其他的对象 -->
<property name="name"value="保时捷"/>
<property name="price" value="5000000"/>
</bean>
集合属性的注入
<property name="list">
<list>
<value>杭州</value>
<value>旧金山</value>
</list>
</property>
<property name="set">
<set>
<value>杭州</value>
<value>武汉</value>
</set>
</property>
<property name="map">
<map>
<entry key="aa" value="111"/>
<entry key="bb" value="333"/>
</map>
</property>
<propertyname="properties">
<props>
<prop key="username">root</prop>
<prop key="passWord">123</prop>
</props>
</property>
④测试 加载配置文件ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");CustomerService customerService = (CustomerService) applicationContext.getBean("customerService");
⑤注解装配Bean<context:annotation-config />
@Component 描述Spring框架中Bean
通常 XML:bean管理 注解;注入属性的时候比较方便.<bean id="customer" class="com.itpp.Customer"></bean> <bean id="order" class="com.itpp.Order"></bean> <bean id="customerSerice" class="com.itpp.CustomerService"> <property name="customer" ref="customer"></property> </bean>
public class CustomerService { private Customer customer; @Autowired @Qualifier("order") private Order order; public void setCustomer(Customer customer) { this.customer = customer; }
⑥Spring集成JUnit测试@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class SpringTest{
@Test
public void demo1(){
userService.sayHello();
}
}
二、AOP aspect Oriented Programing 面向切面编程
1.关于AOP
AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码。纯java实现,不需要专门的编译过程和类加载器,在运行期通过代理方式向目标类织入增强代码。
底层原理:JDK动态代理:对实现了接口的类生成代理 CGLib代理机制:对类生成代理
2.Spring传统AOP前置通知MethodBeforeAdvice
后置通知AfterReturningAdvice
环绕通知MethodInterceptor
异常抛出通知ThrowsAdvice
第一步:导入相应jar包.
第二步:编写被代理对象:CustomerDao接口 CustoemrDaoImpl实现类
第三步:编写增强的代码
public class MyBeforeAdvice implements MethodBeforeAdvice{
public void before(Methodmethod, Object[] args, Object target) throws Throwable {
System.out.println("前置增强...");
}
}
第四步:生成代理(配置生成代理)
Spring基于ProxyFactoryBean类.底层自动选择使用JDK的动态代理还是CGLIB的代理<bean id="customerDao" class="..CustomerDaoImpl"></bean>
<bean id="beforeAdvice"class="c...MyBeforeAdvice"></bean>
<!-- Spring支持配置生成代理: -->
<bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<propertyname="target" ref="customerDao"/>
<propertyname="proxyInterfaces" value="cn.itcast.spring3.demo3.CustomerDao"/>
<propertyname="interceptorNames" value="beforeAdvice"/>
</bean>
<bean id="orderDao" class="..OrderDao"></bean> <bean id="aroundAdvice" class="..MyAroundAdvice"></bean> <!-- 定义切点切面: --> <bean id="myPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <!-- 定义表达式,规定哪些方法执行拦截 --> <property name="patterns" value=".*add.*,.*find.*"></property> <!-- 应用增强 --> <property name="advice" ref="aroundAdvice"/> </bean> <!-- 定义生成代理对象 --> <bean id="orderDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 配置目标 --> <property name="target" ref="orderDao"></property> <!-- 针对类的代理 --> <property name="proxyTargetClass" value="true"></property> <!-- 在目标上应用增强 --> <property name="interceptorNames" value="myPointcutAdvisor"></property> </bean>3.自动代理<bean id="customerDao"class="com.it.CustoemrDaoImpl"></bean>
<bean id="orderDao"class="com.it2.OrderDao"></bean>
<bean id="beforeAdvice"class="com.it.MyBeforeAdvice"></bean>
<bean id="aroundAdvice"class="com.it2.MyAroundAdvice"></bean>
<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<propertyname="beanNames" value="*Dao"></property>
<propertyname="interceptorNames"value="beforeAdvice"></property>
</bean>
<bean id="myPointcutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="pattern" value=".*add.*"></property> <property name="advice" ref="aroundAdvice"></property> </bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean> |
AspectJ是一个基于Java语言的AOP框架
AspectJ表达式: 语法:execution(表达式)
AspectJ增强:
@Before 前置通知,相当于BeforeAdvice
@AfterReturning 后置通知,相当于AfterReturningAdvice
@Around 环绕通知,相当于MethodInterceptor
@AfterThrowing抛出通知,相当于ThrowAdvice
@After 最终final通知,不管是否异常,该通知都会执行
基于注解
public class UserDao { public void add(){ System.out.println("添加用户"); } public int update(){ System.out.println("修改用户"); return 1; } public void delete(){ System.out.println("删除用户"); } public int find(){ System.out.println("查询用户");// int a=1/0; return 9; }}<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy /> <bean id="userDao" class="com.it.UserDao"></bean> <bean id="myAspect" class="com.it.MyAspect"></bean></beans>@Aspectpublic class MyAspect { @Before("execution(* com.it.UserDao.add(..))") public void before(JoinPoint joinPoint){ System.out.println("前置增强...."+joinPoint); } @AfterReturning(value="execution(* com.it.UserDao.find(..))",returning="resVal") public void afterReturning(Object resVal){ System.out.println("后置增强...."+resVal); } @Around(value="execution(* com.it.UserDao.update(..))") public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ System.out.println("环绕前增强...."); Object obj = proceedingJoinPoint.proceed(); System.out.println("环绕后增强...."); return obj; } @AfterThrowing(value="MyAspect.myPointcut()",throwing="e") public void afterThrowing(Throwable e){ System.out.println("不好了 出异常了!!!"+e.getMessage()); } @After("MyAspect.myPointcut()") public void after(){ System.out.println("最终通知..."); } @Pointcut("execution(* com.it.UserDao.find(..))") public void myPointcut(){}}@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class SpringTest { @Autowired @Qualifier("userDao") private UserDao userDao; @Test public void demo(){ userDao.add(); userDao.delete(); userDao.update(); userDao.find(); }}PS:本文基于某培训机构笔记小结。。:
新闻热点
疑难解答