切面之引入

偶尔学到了springAOP中的引入,着实强大,记录一下吧

 

我们假设现在有一个UserService

 

首先有个User类,很简单

package com.wxl.spring.declare.parents;

public class User {
    private String name;
    private String password;

}

 

 

 

然后UserService接口,里面只有一个添加用户的方法

 

 

package com.wxl.spring.declare.parents;

public interface UserService {
    
    void addUser(User user);
}

 

 

 

 

 

实现,不要忘记注册bean。。。假装添加成功好了

 

package com.wxl.spring.declare.parents;

import org.springframework.stereotype.Component;

@Component
public class UserServiceImpl implements UserService {
    public void addUser(User user) {
        // insert User
        System.out.println("insert success");
    }
}

 

 

现在我们又新写了一个接口用作验证用户如下

 

 

package com.wxl.spring.declare.parents;

public interface Verifier {
    
    boolean verifier(User user);
}


实现如下

 

 

 

 

 

 

package com.wxl.spring.declare.parents;

import org.springframework.stereotype.Component;

@Component
public class VerifierImpl implements Verifier {
    public boolean verifier(User user) {
        return user.getName().equals("admin") && user.getPassword().equals("admin");
    }
}


现在创建一个测试类,验证用户并添加

 

 

 

package com.wxl.spring.test;

import com.wxl.spring.config.SpringConfig;
import com.wxl.spring.declare.parents.User;
import com.wxl.spring.declare.parents.UserService;
import com.wxl.spring.declare.parents.Verifier;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class TestCommon {

    @Autowired private UserService userService;

    @Autowired private Verifier verifier;

    @Test
    public void test() throws Exception {
        User user = new User("admin","admin");
        if (verifier.verifier(user))
            userService.addUser(user);
        else
            System.out.println("verifier fail");
    }
}

运行可以看到 控制台打印   insert success 添加成功

 

但是我发现我注入了两个bean UserService和Verifier 如果还需要更多的功能难道挨个注入更多的bean吗,有没有一种方法可以一劳永逸让一个bean完成所有功能呢?有!那就是引入。

下面定义切面类 

 

package com.wxl.spring.declare.parents;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AspectUtil {

    @DeclareParents(value = "com.wxl.spring.declare.parents.UserService+",defaultImpl = VerifierImpl.class)
    public Verifier verifier;

}

 

 

 

 

 


其中@DeclareParents注解的value参数代表要为哪些bean引入功能,+代表该接口的所有实现类,defaultImpl参数代表引入了哪个具体实现类 被注解的方法的返回类型代表了被引入的实现类实现的接口,相当于替UserService的所有实现类又实现了Verifier接口。

接下来

再看之前的测试类

 

 

package com.wxl.spring.test;

import com.wxl.spring.config.SpringConfig;
import com.wxl.spring.declare.parents.User;
import com.wxl.spring.declare.parents.UserService;
import com.wxl.spring.declare.parents.Verifier;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class TestCommon {

    @Autowired private UserService userService;


    @Test
    public void test() throws Exception {
        User user = new User("admin","admin");
        if (((Verifier)userService).verifier(user))
            userService.addUser(user);
        else
            System.out.println("verifier fail");
    }
}

 

 

运行~同样显示insert success!而且我们只注入了一个bean,只是在需要用到verifier方法时进行了一次临时的类型强制转换。感觉十分方便啊~
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值