您的位置:首页 > 编程语言 > Java开发

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

2017-07-24 09:45 946 查看
                                                 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)+增强

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

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐