idea 实现Spring讲解(Ioc-控制反转)/Aop(面向切面的编程)

                                                 IOC

一.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)+增强

总结:符合切点表达式的业务方法就是切面

 

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值