偶尔学到了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方法时进行了一次临时的类型强制转换。感觉十分方便啊~