idea 实现Spring讲解(Ioc-控制反转)/Aop(面向切面的编程)
2017-07-24 09:45
946 查看
IOC
B.Ioc控制反转 说的是创建对象实例的控制权剥离Ioc'容器(Spring容器)控制,实际就是xml控制。
<!--Spring-Context架包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<!--Spring-Bean架包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
B.建配置文件(applictionContext.xml)
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<beanid="happyservice"class="cn.service.HappyService">
<propertyname="info"value="happy"></property>
<propertyname="age"value="12"></property>
</bean>
<beanid="happyService"class="cn.service.HappyService"></bean>
<!--*******************第一个实例***********************-->
<beanid="color"
class="cn.print.ColorPinkimp"></bean>
<beanid="paperA4"class="cn.print.PaperA4"></bean>
<beanid="print"class="cn.print.ColorAndPaper">
<propertyname="color"ref="color"/>
<propertyname="paper"ref="paperA4"/>
</bean>
</beans>
C.测试
<dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.8.7</version >
</dependency>
第二步:
第三步:
第四步:
第五步:
第六步(日志):后置增强接口
前置增强接口:
第七步:'qiapplictionContext.xml
<!--*************************Aop的搭建********************************-->
<?xml version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean
id="userdao"
class="cn.aop.UserDaoimp"/>
<bean
id="userbiz"
class="cn.service.UserBizz">
<property
name="ao"
ref="userdao"/>
</bean>
<bean
id="LoggerAfter"
class="cn.aop.LoggerAfter"/>
<bean
id="LoggerBefpre"
class="cn.aop.LoggerBefor"/>
<aop:config>
<aop:pointcut
id="point"
expression="execution(* *..service.*.*(..))"></aop:pointcut>
<aop:advisor
advice-ref="LoggerAfter"
pointcut-ref="point"/>
</aop:config>
</beans>
第八步:单测
总结:符合切点表达式的业务方法就是切面
一.IOC(控制反转)
1.Spring框架的作用:管理我们项目业务中的各项Bean(service,dao,Action)实例化类。
2.Spring体系结构图
3.控制反转的理解
A.控制反转是一个重要的面向对象的编程法则来削减计算机程序的耦合性问题,也是轻量级的Spring框架核心,beans。B.Ioc控制反转 说的是创建对象实例的控制权剥离Ioc'容器(Spring容器)控制,实际就是xml控制。
4.Spring的搭建
A.导入架包<!--Spring-Context架包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<!--Spring-Bean架包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
B.建配置文件(applictionContext.xml)
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<beanid="happyservice"class="cn.service.HappyService">
<propertyname="info"value="happy"></property>
<propertyname="age"value="12"></property>
</bean>
<beanid="happyService"class="cn.service.HappyService"></bean>
<!--*******************第一个实例***********************-->
<beanid="color"
class="cn.print.ColorPinkimp"></bean>
<beanid="paperA4"class="cn.print.PaperA4"></bean>
<beanid="print"class="cn.print.ColorAndPaper">
<propertyname="color"ref="color"/>
<propertyname="paper"ref="paperA4"/>
</bean>
</beans>
C.测试
二.Aop(面向切面编程)
1.Aop的目标(作用):让我们可以“专心做事”日志记录,事务处理,异常捕获,缓存操作。
2.Aop原理:将复杂的需求解析出来。将散布在系统中的公共功能集中解决。
3.aop架包
<!--Aop架包--><dependency>
<groupId> org.aspectj</groupId >
<artifactId> aspectjweaver</artifactId >
<version> 1.8.7</version >
</dependency>
4.applictionContext.xml
aop的约束文件:5.实现日志(分割线)打印处理前和处理后
第一步:第二步:
第三步:
第四步:
第五步:
第六步(日志):后置增强接口
前置增强接口:
第七步:'qiapplictionContext.xml
<!--*************************Aop的搭建********************************-->
<?xml version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean
id="userdao"
class="cn.aop.UserDaoimp"/>
<bean
id="userbiz"
class="cn.service.UserBizz">
<property
name="ao"
ref="userdao"/>
</bean>
<bean
id="LoggerAfter"
class="cn.aop.LoggerAfter"/>
<bean
id="LoggerBefpre"
class="cn.aop.LoggerBefor"/>
<aop:config>
<aop:pointcut
id="point"
expression="execution(* *..service.*.*(..))"></aop:pointcut>
<aop:advisor
advice-ref="LoggerAfter"
pointcut-ref="point"/>
</aop:config>
</beans>
第八步:单测
6.Aop总结
切面=切入点(pointcut)+增强总结:符合切点表达式的业务方法就是切面
相关文章推荐
- 笔记——spring基本使用(控制反转IOC、面向切面编程AOP、声明式事务)
- spring的依赖注入(DI)、控制反转(IOC)和面向切面(AOP)
- 自行打造实现控制反转容器(IOC)与面向方面编程(AOP)的轻量级Framework(1)
- Java+spring切面编程(aop)spring控制反转(ioc)+hibenrate对象关系映射(ORM
- Spring 之 控制反转(IoC), 依赖注入(DI)和面向切面(AOP)
- 自行打造实现控制反转容器(IOC)与面向方面编程(AOP)的轻量级Framework(3)
- 自行打造实现控制反转容器(IOC)与面向方面编程(AOP)的轻量级Framework(2)
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- 自行打造实现控制反转容器(IOC)与面向方面编程(AOP)的轻量级Framework(4)
- java+spring切面编程(aop)spring控制反转(ioc)+hibenrate对象关系映射(ORM) 学习网址
- 为什么说Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架?
- Spring学习,依赖注入(DI)、控制反转(IoC)和面向切面(AOP)
- SPRING--Spring中IOC(反转控制) 和 AOP(面向方面编程)
- Spring AOP的基本原理及面向切面编程的实现
- spring_由XML实现AOP面向切面编程_实现动态代理
- CGLIB实现AOP代理的测试类,与Spring分离,加深对面向切面编程的理解
- JavaEE框架——Spring入门基础(控制反转Ioc和切面技术Aop)
- Spring Ioc的实现----------用idea实现控制反转
- Spring基础、IOC(控制反转)、AOP(面向切面编程)、Log4j、注解配置
- Spring Ioc的实现----------用idea实现控制反转