zoukankan      html  css  js  c++  java
  • Spring的事务机制

    ---恢复内容开始---

    内定的=>(只需要在xml 中添加一个bean)

    在xml 中添加

    <bean id="listener" class="com.test.事件机制.listener">

    </bean>

    测试

    public void listenerTest(){
    AbstractApplicationContext aContext=new ClassPathXmlApplicationContext("bean.xml");
    aContext.refresh();
    }

    public class listener implements ApplicationListener{

    @Override
    public void onApplicationEvent(ApplicationEvent arg0) {
    // TODO Auto-generated method stub
    if (arg0 instanceof ContextClosedEvent) {
    System.out.println(arg0.getClass().getSimpleName()+"事实发生");
    }

    }

    自定义事件==》

    event:

    public class myselfEvent extends ApplicationEvent{
    String name;
    public myselfEvent(Object source) {
    super(source);

    }
    public myselfEvent(Object o,String name){
    super(o);
    this.name=name;
    }
    }

    发布:

    public class animal implements ApplicationContextAware{
    private ApplicationContext ac;
    private String sleepsString;

    public String getSleepsString() {

    ac.publishEvent(new myselfEvent(this, this.sleepsString));
    return sleepsString;
    }

    public void setSleepsString(String sleepsString) {
    this.sleepsString = sleepsString;
    }

    @Override
    public void setApplicationContext(ApplicationContext arg0)
    throws BeansException {
    // TODO Auto-generated method stub
    this.ac=arg0;
    }

    }

    监听:

    public class listener implements ApplicationListener{

    @Override
    public void onApplicationEvent(ApplicationEvent arg0) {
    // TODO Auto-generated method stub

    if (arg0 instanceof myselfEvent) {
    System.out.println(arg0.getClass().getSimpleName()+"事实发生");
    }else{
    System.out.println(arg0.getClass().getName()+"other");
    }

    }

    }

    测试:

    public void myselfListenerTest(){
    AbstractApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
    animal a=(animal) context.getBean("myselfListener");
    System.out.println(a.getSleepsString());
    }

  • 相关阅读:
    使用淘宝Str2varlist与str2numlist 代替 in/exist ,提升性能(Oracle)
    由浅入深理解索引的实现
    你知道数据库索引的工作原理吗?
    深入理解数据库磁盘存储(Disk Storage)
    如何解析oracle执行计划
    Beyond Compare 4 最新中文版 注册码 key
    并发和并行的区别
    代码复用的规则
    Some Java exceptions, messages and errors.
    菜鸟学SSH(十六)——Struts2内部是如何工作的
  • 原文地址:https://www.cnblogs.com/nedhome/p/4442157.html
Copyright © 2011-2022 走看看