美文网首页
Spring AOP简化笔记--泛型中的应用(三)

Spring AOP简化笔记--泛型中的应用(三)

作者: LeeSpringFly | 来源:发表于2017-07-04 16:12 被阅读0次

目录

  • 文件目录
  • 编码
  • 测试
  • 测试结果
  • 说明

文件目录

> config
      ConcertConfig
> aspectCut
      BaseAspect
> entity
      ObjectA
      ObjectB
> server
      > base
           Base
           BaseImpl
      > impl
           ObjectAServiceImpl
           ObjectBServiceImpl
      ObjectAService
      ObjectBService

编码

config.ConcertConfig

@Configuration
@EnableAspectJAutoProxy
@ComponentScan({"aspectCut", "service"})
public class ConcertConfig {
}

entity.ObjectA

public class ObjectA {
}

entity.ObjectB

public class ObjectB {
}

service.base.Base

public interface Base<T> {
    public void save(T t);

    public void update(T t);

    public void delete(Long id);
}

service.base.BaseImpl

public class BaseImpl<T> implements Base<T> {
    @Override
    public void save(T t) {
        System.out.println("save()");
    }

    @Override
    public void update(T t) {
        System.out.println("update()");
    }

    @Override
    public void delete(Long id) {
        System.out.println("delete()");
    }
}

service.ObjectAService

public interface ObjectAService extends Base<ObjectA> {
}

service.ObjectBService

public interface ObjectBService extends Base<ObjectB> {
}

service.impl.ObjectAServiceImpl

@Service("obaService")
public class ObjectAServiceImpl extends BaseImpl<ObjectA> implements ObjectAService {
}

service.impl.ObjectBServiceImpl

@Service("obbService")
public class ObjectBServiceImpl extends BaseImpl<ObjectB> implements ObjectBService {
}

aspectCut.BaseAspect

@Aspect
@Component
public class BaseAspect<T> {

    @Before("execution(* service.base.BaseImpl.save(..)) && args(t)")
    public void save(T t) {
        System.out.println("save(), " + t.getClass().toString());
    }

    @Before("execution(* service.base.BaseImpl.update(..)) && args(t)")
    public void update(T t) {
        System.out.println("update(), " + t.getClass().toString());
    }

    @Before("execution(* service.base.BaseImpl.delete(..)) && args(id)")
    public void delete(Long id) {
        System.out.println("delete(), " + id);
    }
}

测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { config.ConcertConfig.class })
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ObjectAServiceImplTest { 
    @Autowired
    private ObjectAService obaService;

    @Autowired
    private ObjectBService obbService;

    @Test
    public void testSupportMethod() throws Exception {
        System.out.println("==== TEST A ====");
        obaService.save(new ObjectA());
        obaService.update(new ObjectA());
        obaService.delete(10L);

        System.out.println("==== TEST B ====");
        obbService.save(new ObjectB());
        obbService.update(new ObjectB());
        obbService.delete(20L);
    }
}

测试结果

==== TEST A ====
save(), class entity.ObjectA
save()
update(), class entity.ObjectA
update()
delete(), 10
delete()
==== TEST B ====
save(), class entity.ObjectB
save()
update(), class entity.ObjectB
update()
delete(), 20
delete()

Process finished with exit code 0

说明

_ Spring AOP 无法找到父类方法,所以要在定位方法的时候,定位到父类方法 _

相关文章

网友评论

      本文标题:Spring AOP简化笔记--泛型中的应用(三)

      本文链接:https://www.haomeiwen.com/subject/ztlxhxtx.html