当插入或更新时,触发IDefaultListener:
普通插入:
Test test = new Test(); test.setId(100); test.setName("test"); testService.save(test);
结果:
加入监听器:
//若在Spring容器内,则不需要手动添加 testService.addDefaultListener(new TestDefaultListener()); Test test = new Test(); test.setId(100); test.setName("test"); System.out.println("Name: " + test.getName()); testService.save(test); test = testService.get(test.getId()); System.out.println("After insert Name: " + test.getName()); testService.update(test); System.out.println("After update Name: " + test.getName());
结果:
注意:本测试直接将监听对象加入Service中,若在Spring容器中,只需将TestDefaultListener标记@Service即可。