zoukankan      html  css  js  c++  java
  • Spring中IOC&AOP

    Spring 核心是一个容器,核心机制为IOC和AOP

    一、IOC 控制转移,也可称为解耦合,若没有spring容器,则action与dao直接相连,如若此action需要跟另一个dao相连或dao层发生变动,都需要大改程序,这种高度耦合的程序对于编程是不利的,spring的出现改变了这一现象,以往dao的控制权在于调用它的action,现在,把控制权交给spring容器,在配置文件中指定引用

    配置文件applicationContext中可以使用setter方式注入也可以用构造方法注入

    <!--setter方式-->

    <bean id="ID名称" class="类全名"></bean>

    如果类中含有其他类引用,在该action类中,dao类以属性方式存在,private 类型名称,补全setter方法

    在配置文件中<property name="引用dao" ref="引用dao的ID"></property>

    类中list,set,map等属性,在配置文件中分别为

    <property><list><value>11</value><value>12</value></list></property>

    <property><set><value>11</value><value>12</value></set></property>

    <property><map><entry key="123" value="dadas"></entry><entry key="124" value="dadas"></entry></map></property>

    <!--构造方式-->

    <bean id="address" class="java.net.InetSocketAddress">
    <constructor-arg index="0" value="127.0.0.1" />
    <constructor-arg index="1" value="4449" />
    </bean>

    使用构造方法注入配置文件,,在类中用构造方法参数使用

    自动扫描技术,避免配置文件中出现大量bean

    在<beans></beans>中引入标签context,设置默认扫描包

    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
    >


    <!-- -自动扫描功能在容器中有标识类扫描 -->
     <context:component-scan base-package="com.ss" /> 

    在类名上加入标签

    @Controller (一般action层)@Service(一般业务层) @Repository(一般dao层) @Component(其他层)  组件标识可以混用,不一定要在特点层特点,若后没有(“id自定义名称”)如@Repository("jdbcdao") 指定该层名称,则调用时一般默认为类名(首字母小写)(get bean("")时填入)。若类中存在引用其他类属性,则在该属性上部加入标签,否则报空指针异常。@Resource(name="bean对象的id名")

    二、AOP 面向切面编程,若程序中需要使用某个程序块多次处理,比如打印日志监控,为避免麻烦,可以把程序块提取出来,在某些方法或类调用前后执行,类似过滤器概念

    在配置文件beans中加入aop标签

    xmlns:aop="http://www.springframework.org/schema/aop"

    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" 

    使用标签

    <aop:config>
    <!-- 切入点,指定目标对象或者方法 -->  
    <aop:pointcut id="daocut" expression="within(使用切面的目标类或方法例com.ss.ss.*)"/>


    <!-- 切面,指定类,共同处理的组件 -->
    <aop:aspect id="loggeraspect" ref="切块bean的id名">
    <!-- 在方法前后执行-->
    <aop:before method="需要执行的方法" pointcut-ref="切入点ID名"/>
    </aop:aspect>

    </aop:config>

     AOP注解方式****

    在主配置文件中引入<aop:aspectj-autoproxy/>

    在切面类上定义@Aspect,先要确保此类被自动扫描

    在类下定义一个空的方法作为切入点 @Pointcut("within()"),此定义必须为类或方法上,所以需自单独创建

      例如@Pointcut("within(com.ss.*.*)")

             public void daocut(){

    }

    在切面方法上定义通知@Around("daocut()")(环绕通知,此参数为切入点所在的空方法)

    若通知方法为around,则所在方法中默认为带参数方法,参数为(ProceedingJoinPoint  pjp) 

    此参数可以得到调用切面的类中的方法名

    //获取调用类名
    String classname=pjp.getTarget().getClass().getName();
    //获取调用方法名
    String methodname=pjp.getSignature().getName();

    若通知方法为aop:after-throwing,则所在方法中的参数为(Exception ex)

    在配置文件中<aop:after-throwing method="方法名" throwing="ex" pointcut-ref="切入点id"/>(追加throwing配置,值为参数名,保持一致)

    ex中可以得到哪些类哪些方法出的那种类型的异常,例如

    StackTraceElement[] message=ex.getStackTrace();
    System.out.println(ex+"*****"+message[0]);

    可以得到包名类名下方法报的类型错误

    可以结合log4j将错误输出整理

  • 相关阅读:
    App案例分析——XBMC
    四则运算题目生成程序(基于控制台)
    第一次结对编程
    第二次作业--摩拜单车
    第0次作业
    团队编程作业1-团队展示与选题
    结对编程1-模块化
    APP案例分析
    第1次作业
    第0道作业
  • 原文地址:https://www.cnblogs.com/dss1025/p/8205596.html
Copyright © 2011-2022 走看看