当前位置: 移动技术网 > IT编程>开发语言>Java > IOC和AOP使用拓展

IOC和AOP使用拓展

2018年11月12日  | 移动技术网IT编程  | 我要评论

多种方式实现依赖注入

构造注入

编写测试类

public class userserviceimpl implements userservice {

    // 声明接口类型的引用,和具体实现类解耦合
    private userdao dao;

    // 无参构造
    public userserviceimpl() {
    }

    // 用于为dao属性赋值的构造方法
    public userserviceimpl(userdao dao) {
        this.dao = dao;
    }

    public void addnewuser(user user) {
        // 调用用户dao的方法保存用户信息
        dao.save(user);
    }
}

在使用设值注入时,spring通过javabean无参构造方法实例化对象,当我们编写带参构造方法后,java虚拟机不会再提供默认的无参构造方法,为了保证使用的灵活性,建议自行添加一个无参构造方法
配置文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="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 ">
    <!-- 定义userdaoimpl对象,并指定id为userdao -->
    <bean id="userdao" class="dao.impl.userdaoimpl" />
    <!-- 定义userserviceimpl对象,并指定id为userservice -->
    <bean id="userservice" class="service.impl.userserviceimpl">
        <!-- 通过定义的单参构造为userservice的dao属性赋 值 -->
        <constructor-arg>
            <!-- 引用id为userdao的对象为userservice的dao属性赋值 -->
            <ref bean="userdao" />
        </constructor-arg>
    </bean>
</beans>

1 一个 constructor-arg元素表示构造方法的一个参数,且使用时不区分顺序。
2 通过constructor-arg元素的index 属性可以指定该参数的位置索引,位置从0 开始。
3 constructor-arg元素还提供了type 属性用来指定参数的类型,避免字符串和基本数据类型的混淆。

constructor-arg节点下的四个属性

  • index是索引,指定注入的属性,从0开始,如:0代表persondao,1代表str属性;
  • type是指该属性所对应的类型,如persondao对应的是com.aptech.dao.persondao;
  • ref 是指引用的依赖对象;
  • value 当注入的不是依赖对象,而是基本数据类型时,就用value;

比如:

<bean id="rod" class="cn.springdemo.greeting">
    <constructor-arg index="1">
    <value>rod</value>
    </constructor-arg>
    <constructor-arg index="0">
    <value>世界上有10种人</value>
    </constructor-arg>
</bean>

使用p命名空间实现属性注入

p命名空间的特点:使用属性而不是子元素的形式配置bean的属性,从而简化了配置代码
语法:
对于直接量(基本数据类型、字符串)属性:p:属性名="属性值"
对于引用bean的属性:p:属性名-ref="bean的id"
使用前先要在spring配置文件中引入p命名空间

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

示例:

<?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:p="http://www.springframework.org/schema/p" 
    xsi:schemalocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 使用p命名空间注入属性值 -->
    <bean id="user" class="entity.user" p:username="皮皮" p:age="21"
        p:email="pipi@anxin.com" />
    <bean id="userdao" class="dao.impl.userdaoimpl" />
    <bean id="userservice" class="service.impl.userserviceimpl" p:dao-ref="userdao" />
</beans>

注入不同数据类型

注入直接量

  使用

<bean id="user" class="entity.user">
    <property name="username">
        <value>张三</value>
    </property>
    <property name="age">
        <value>23</value>
    </property>
</bean>

如果属性值中包含了xml文件的特殊字符(& < > " '),则注入需要进行处理,通常可以采用两种办法,使用<[cdata[]]>标记或把特殊字符替换为实体引用.

<bean id="product" class="entity.product">
         <!-- 使用<![cdata[]]>标记处理xml特 殊字符 -->
        <property name="specialcharacter1">
            <value><![cdata[p&g]]></value>
        </property>
        <!-- 把xml特殊字符替换为实体引用 -->
        <property name="specialcharacter2">
            <value>p&amp;g</value>
        </property>
<bean>
符号 实体引用
< & lt;
> & gt;
& & amp;
' & apos;
" & quot;

注意:在xml文件中字符"<"和“&”是非法的,其他3个符号是合法的,但是将它们替换为实体引用是个好习惯

引用其他bean组件

spring中定义的bean可以互相引用,从而建立依赖关系,除了使用ref属性,还可以通过

<bean id="userdao" class="dao.impl.userdaoimpl"/>
<bean id="userservice"  class="service.impl.userserviceimpl">
    <property name="dao">
        <ref bean="userdao"/>
    </property>
</bean>

使用内部bean

<!-- 定义内部bean -->
<bean id="userservice" class="service.impl.userserviceimpl">
    <property name="dao">
        <bean class="dao.impl.userdaoimpl"/>
    </property>
</bean>

这样这个userdaoimpl类型的bean就只能被useruservice使用,其他的bean则无法使用

注入集合类型的属性

对于list或数组类型的属性,可以使用

<!-- 注入list类型 -->
<property name="list">
    <list>
        <!-- 定义list中的元素 -->
        <value>足球</value>
        <value>篮球</value>
    </list>
</property>

<!-- 注入map类型 -->
<property name="map">
    <map>
        <!-- 定义map中的键值对 -->
        <entry>
            <key>
                <value>football</value>
            </key>
            <value>足球</value>
        </entry>
        <entry>
            <key>
                <value>basketball</value>
            </key>
            <value>篮球</value>
        </entry>
    </map>
</property>

注入null和空字符串值
可以使用注入空字符串,使用

<!-- 注入空字符串值 -->
<property name="emptyvalue">
    <value></value>
</property>
    <!-- 注入null值 -->
<property name="nullvalue">
    <null/>
</property> 

其他增强类型

spring支持多种增强类型,除了我们上一篇文章说的前置增强和后置增强,在这里我们在补充几种常用的增强类型

异常抛出增强

异常抛出增强的特点是在目标方法抛出异常时织入增强处理。使用异常抛出增强,可以为各功能模块提供统一的,可拨插的异常处理方案

/**
 * 定义包含增强方法的javabean
 */
public class errorlogger {
    private static final logger log = logger.getlogger(errorlogger.class);

    public void afterthrowing(joinpoint jp, runtimeexception e) {
        log.error(jp.getsignature().getname() + " 方法发生异常:" + e);
    }
}

spring配置文件

<!-- 声明增强方法所在的bean -->
<bean id="thelogger" class="aop.errorlogger"></bean>
    <!-- 配置切面 -->
    <aop:config>
        <!-- 定义切入点 -->
        <aop:pointcut id="pointcut" expression="execution(* service.userservice.*(..))" />
        <!-- 引用包含增强方法的bean -->
        <aop:aspect ref="thelogger">
            <!-- 将afterthrowing()方法定义为异常抛出增强并引用pointcut切入点 -->
            <!-- 通过throwing属性指定为名为e的参数注入异常实例 -->
            <aop:after-throwing method="afterthrowing"
                pointcut-ref="pointcut" throwing="e" />
        </aop:aspect>
    </aop:config>
</beans>

expression指示符我们上一篇文章已经说话大家可以先看一下上一篇文章
使用

最终增强

最终增强的特点是无论抛出异常还是正常退出,该增强都会得到执行,类似于异常处理机制中finally块的作用,一般用于释放资源,使用最终增强,就可以为各功能模块提供统一的,可拨插的处理方案.

/**
 * 定义包含增强方法的javabean
 */
public class afterlogger {
    private static final logger log = logger.getlogger(afterlogger.class);

    public void afterlogger(joinpoint jp) {
        log.info(jp.getsignature().getname() + " 方法结束执行。");
    }
}

spring配置文件

<!-- 声明增强方法所在的bean -->
    <bean id="thelogger" class="aop.afterlogger"></bean>
    <!-- 配置切面 -->
    <aop:config>
        <!-- 定义切入点 -->
        <aop:pointcut id="pointcut" expression="execution(* service.userservice.*(..))" />
        <!-- 引用包含增强方法的bean -->
        <aop:aspect ref="thelogger">
            <!-- 将afterlogger()方法定义为最终增强并引用pointcut切入点 -->
            <aop:after method="afterlogger" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

使用

环绕增强

环绕增强在目标方法的前后都可以织入增强处理.环绕增强是功能最强大的增强处理,spring把目标方法的控制权全部交给它,在环绕增强处理中,可以获取或修改目标方法的参数,返回值可以对它进行异常处理,甚至可以决定目标方法是否被执行.

/**
 * 定义包含增强方法的javabean
 */
public class aroundlogger {
    private static final logger log = logger.getlogger(aroundlogger.class);

    public object aroundlogger(proceedingjoinpoint jp) throws throwable {
        log.info("调用 " + jp.gettarget() + " 的 " + jp.getsignature().getname()
                + " 方法。方法入参:" + arrays.tostring(jp.getargs()));
        try {
            object result = jp.proceed();
            log.info("调用 " + jp.gettarget() + " 的 "
                    + jp.getsignature().getname() + " 方法。方法返回值:" + result);
            return result;
        } catch (throwable e) {
            log.error(jp.getsignature().getname() + " 方法发生异常:" + e);
            throw e;
        } finally {
            log.info(jp.getsignature().getname() + " 方法结束执行。");
        }
    }
}

spring配置文件

<!-- 声明增强方法所在的bean -->
<bean id="thelogger" class="aop.aroundlogger"></bean>
    <!-- 配置切面 -->
    <aop:config>
        <!-- 定义切入点 -->
        <aop:pointcut id="pointcut" expression="execution(* service.userservice.*(..))" />
        <!-- 引用包含增强方法的bean -->
        <aop:aspect ref="thelogger">
            <!-- 将aroundlogger()方法定义为环绕增强并引用pointcut切入点 -->
            <aop:around method="aroundlogger" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

使用

增强处理类型 特 点
before 前置增强处理,在目标方法前织入增强处理
afterreturning 后置增强处理,在目标方法正常执行(不出现异常)后织入增强处理
afterthrowing 异常增强处理,在目标方法抛出异常后织入增强处理
after 最终增强处理,不论方法是否抛出异常,都会在目标方法最后织入增强处理
around 环绕增强处理,在目标方法的前后都可以织入增强处理

spring aop配置元素

aop配置元素 描 述
aop:config aop配置的顶层元素,大多数的<aop:*>元素必须包含在
aop:pointcut 定义切点
aop:aspect 定义切点
aop:after 定义最终增强(不管被通知的方法是否执行成功)
aop:after-returning 定义after-returning增强
aop:after-throwing 定义after-throwing增强
aop:around 定义环绕增强
aop:before 定义前置增强
aop:aspectj-autoproxy 启动@aspectj注解驱动的切面

使用注解实现ioc的配置

前面我们说过用xml的形式配置ioc,那种方式是比较麻烦的,在spring2.0以后的版本我们就可以使用注解来配置,进一步减少了配置文件的代码
使用注解定义bean

/**
 * 用户业务类,实现对user功能的业务管理
 */
@service("userservice")
public class userserviceimpl implements userservice {

    @autowired  // 默认按类型匹配
    @qualifier("userdao") // 按指定名称匹配
    private userdao dao;

    // 使用@autowired直接为属性注入,可以省略setter方法
    /*public void setdao(userdao dao) {
        this.dao = dao;
    }*/

    public void addnewuser(user user) {
        // 调用用户dao的方法保存用户信息
        dao.save(user);
    }
}

上面代码我们通过注解定义了一个名为userdao的bean@autowired的作用和在xml文件中编写

<!--扫描包中注解标注的类-->
<context:component-scan base-package="service,dao"/>
<!--多个包之前用逗号隔开-->

spring还提供了其他的注解
@componet:实现bean组件的定义
@repository:用于标注dao类
@service:用于标注业务类
@controller:用于标注控制器类
@autowired:实现bean的自动装配
@qualifier:指定bean的名称
@resource:实现bean的组件装配
大家可以查看spring的 进一步了解他们的用法

使用注解定义切面

aspectj
面向切面的框架,它扩展了java语言,定义了aop 语法,能够在编译期提供代码的织入
@aspectj
aspectj 5新增的功能,使用jdk 5.0 注解技术和正规的aspectj切点表达式语言描述切面
spring通过集成aspectj实现了以注解的方式定义增强类,大大减少了配置文件中的工作量
利用轻量级的字节码处理框架asm处理@aspectj中所描述的方法参数名
使用注解定义切面实现日志功能

/**
 * 使用注解定义切面
 */
@aspect
public class userservicelogger {
    private static final logger log = logger.getlogger(userservicelogger.class);
    
    @pointcut("execution(* service.userservice.*(..))")
    public void pointcut() {}

    @before("pointcut()")
    public void before(joinpoint jp) {
        log.info("调用 " + jp.gettarget() + " 的 " + jp.getsignature().getname()
                + " 方法。方法入参:" + arrays.tostring(jp.getargs()));
    }
    @afterreturning(pointcut = "pointcut()", returning = "returnvalue")
    public void afterreturning(joinpoint jp, object returnvalue) {
        log.info("调用 " + jp.gettarget() + " 的 " + jp.getsignature().getname()
                + " 方法。方法返回值:" + returnvalue);
    }
}

切入点表达式使用@pointcut注解来表示,而切入点签名则需要一个普通的方法定义来提供,
如上面代码中的pointcut()方法,作为切入点签名的方法必须返回void类型,切入点定义好后,就可以使用pointcut()签名进行引用
定义完切面后,还需要在spring配置文件中完成织入工作

 <context:component-scan base-package="service,dao" />
    <bean class="aop.userservicelogger"></bean>
    <aop:aspectj-autoproxy />

配置文件中首先要导入aop命名空间,只需要在配置文件中添加

使用注解定义其他类型增强

异常抛出增强

/**
 * 通过注解实现异常抛出增强
 */
@aspect
public class errorlogger {
    private static final logger log = logger.getlogger(errorlogger.class);

    @afterthrowing(pointcut = "execution(* service.userservice.*(..))", throwing = "e")
    public void afterthrowing(joinpoint jp, runtimeexception e) {
        log.error(jp.getsignature().getname() + " 方法发生异常:" + e);
    }

}

使用afterthrowing注解可以定义异常抛出增强,如果需要获取抛出的异常,可以为增强方法声明相关类型的参数,并通过@afterthrowing注解的throwing属性指定该参数名称,spring会为其注入从目标方法抛出的异常实例
其他的方法都是大同小异,大家可以自己动手试一试
@aspect(定义一个切面 )
@before(前置增强)
@afterreturning(后置增强)
@around (环绕增强)
@afterthrowing(异常抛出增强)
@after(最终增强)

在配置文件中添加

写的不好还有不懂的地方,大家可以留言一下 我会尽量解决
by安心

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网