Introductions
@Repository
public class UserDao implements IUserDao {
public void save(){
System.out.println("保存成功,无返回值");
}
@MyAnnotation
public String findName(){
System.out.println("查询名字,返回张三");
return "张三";
}
public void updateById(CustomParameter parameter){
System.out.println("根据id更新");
}
public void updateByName(String name){
System.out.println("根据name更新");
}
public void updateByNameAndId(String name, Integer id){
System.out.println("根据name和id更新");
}
}
@Repository
public class TestDao {
}
@Component
@Aspect
public class Advice {
@DeclareParents(value = "com.liaoxiang.dao.impl.*",defaultImpl = UserDao.class)
public static IUserDao iUserDao;
}
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
IUserDao dao = (IUserDao)ac.getBean("testDao");
System.out.println(dao);
dao.save();
}
}
执行结果:
com.liaoxiang.dao.impl.TestDao@117e949d
保存成功,无返回值
Process finished with exit code 0
可以看到我们只是声明了一个类叫TestDao
并没有写任何的方法,下面代码的作用可以理解为com.liaoxiang.dao.impl
包下的所有类都去实现IUserDao接口,默认实现的的方法跟UserDao
类中一样
@DeclareParents(value = "com.liaoxiang.dao.impl.*",defaultImpl = UserDao.class)
public static IUserDao iUserDao;