(13)Spring AOP为目标对象引入新接口

正常用ioc获取一个bean

接口

package shuai.spring.study.service;

public interface IHelloService {
    public void sayHello();
}

实现类

package shuai.spring.study.service.impl;

import shuai.spring.study.service.IHelloService;

public class HelloServiceImpl implements IHelloService {
    @Override
    public void sayHello() {
        System.out.println("============Hello World!");
    }
}

配置文件

<?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:aop="http://www.springframework.org/schema/aop" 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="iHelloService" class="shuai.spring.study.service.impl.HelloServiceImpl"/>
        

</beans>

测试类

package shuai.spring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import shuai.spring.study.service.IHelloService;

public class HelloTest {

    @Test
    public void testHelloWorld() {

        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorld.xml");

        IHelloService iHelloService = context.getBean("iHelloService", IHelloService.class);
        iHelloService.sayHello();

    }
}

测试结果:

============Hello World!

我们可以利用aop让指定的类实现其它的接口,就比如这个shuai.spring.study.service.impl.HelloServiceImpl类,让它实现INiHao接口

定义接口

package shuai.spring.study.service;

public interface INiHao {
    public void sayNiHao(String param);
}

实现类

package shuai.spring.study.service.impl;

import shuai.spring.study.service.INiHao;

public class NiHaoImpl implements INiHao {

    @Override
    public void sayNiHao(String param) {
        System.out.println("你好:" + param);
    }

}
修改配置文件

<?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:aop="http://www.springframework.org/schema/aop" 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="iHelloService" class="shuai.spring.study.service.impl.HelloServiceImpl"/>
        <bean id="iNiHao" class="shuai.spring.study.service.impl.NiHaoImpl"/>
        
        <!-- <bean id="helloAspect" class="shuai.spring.study.aop.HelloAspect"/> -->
        <!-- <aop:aspectj-autoproxy proxy-target-class="true"/> -->
        <aop:config>
        	<!-- <aop:pointcut expression="execution(* shuai.spring.study.service..*.*(..))" id="pointcut"/> -->
        	<aop:aspect ref="helloAspect">
        		<!-- <aop:before 
        			method="beforeHello(java.lang.String,java.lang.String)" 
        			pointcut="execution(* shuai.spring.study.service..*.*(..)) and args(param0,param1)" 
        			arg-names="param0,param1"/> -->
        		<aop:declare-parents 
        			types-matching="shuai.spring.study.service.IHelloService+" 
        			implement-interface="shuai.spring.study.service.INiHao"
        			delegate-ref="iNiHao"/>
        	</aop:aspect>
        </aop:config>

</beans>

注意,有关引入新接口的内容都写在declare-parents里面

types-matching表示要为哪些类配置新的接口:shuai.spring.study.service.IHelloService+表示实现了IHelloService接口的类

implement-interface表示要为这些类配置什么接口

delegate-ref表示implement-interface所代表接口的默认实现,delegate-ref表示引用一个bean,也可以写成default-impl="shuai.spring.study.service.impl.NiHaoImpl"

测试类

package shuai.spring.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import shuai.spring.study.service.INiHao;

public class HelloTest {
    @Test
    public void testHelloWorld() {

        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorld.xml");

        INiHao iHelloService = context.getBean("iHelloService", INiHao.class);
        iHelloService.sayNiHao("世界");
    }
}

测试结果:

你好:世界

这个结果跟那个默认实现是有关系的。




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值