0

我有这种情况。这是我的Village.java

public class Village{

    private Integer vid;
    private String villageName;
    private String district;

    public Integer getVid() {
        return vid;
    }
    public void setVid(Integer vid) {
        this.vid = vid;
    }
    public String getVillageName() {
        return villageName;
    }
    public void setVillageName(String villageName) {
        this.villageName = villageName;
    }
    public String getDistrict() {
        return district;
    }
    public void setDistrict(String district) {
        this.district = district;
    }
}

这是我的Dao.java界面:

public interface Dao<T> {
    public void insert();
    public void update();
    public void delete();
}

这是我的方面Village_Dao.aj(你可以忽略静态方法逻辑):

import org.apache.ibatis.session.SqlSession;
import com.madx.finance.data.utils.persistence.Dao;
import com.madx.finance.data.utils.factory.ConnectionFactory;

public aspect Village_Dao {
    declare parents: Village implements Dao<Village>;

    public void Village.insert() {
        Village.insertVillage(this);
    }

    public void Village.update() {
        Village.updateVillage(this);
    }

    public void Village.delete() {
        Village.deleteVillage(this.getVid());
    }

    public Village Village.getData() {
        return Village.getDataVillage(this.getVid());
    }

    public static void Village.insertVillage(Village village) {
        SqlSession session = ConnectionFactory.getSqlSessionFactory().openSession();
        VillageMapper mapper = session.getMapper(VillageMapper.class);
        mapper.insertVillage(village);
        session.commit();
        session.close();
    }

    public static void Village.updateVillage(Village village) {
        SqlSession session = ConnectionFactory.getSqlSessionFactory().openSession();
        VillageMapper mapper = session.getMapper(VillageMapper.class);
        mapper.updateVillage(village);
        session.commit();
        session.close();
    }

    public static void Village.deleteVillage(Integer id) {
        SqlSession session = ConnectionFactory.getSqlSessionFactory().openSession();
        VillageMapper mapper = session.getMapper(VillageMapper.class);
        mapper.deleteVillage(id);
        session.commit();
        session.close();
    }

    public static Village Village.getDataVillage(Integer id) {
        SqlSession session = ConnectionFactory.getSqlSessionFactory().openSession();
        VillageMapper mapper = session.getMapper(VillageMapper.class);
        Village village = mapper.selectVillage(id);
        session.close();
        return village;
    }
}

我正在尝试将其转换Village_Dao.aj为带注释的版本,但没有成功Village_Dao_Java.java。我只是设法使该类实现了 Dao,但我无法编写方法(在此文件中分别插入、更新和删除Village_Dao_Java.java)。

这是Village_Dao_Java.java(我阅读了这个链接但我无法让它适用于这种情况)的版本(仍然不完整):

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;

import com.madx.finance.data.utils.persistence.Dao;

@Aspect
public class Village_Dao_Java {

    @DeclareParents("com.madx.demo.Village")
    private Dao<Village> implementedInterface;
}
4

1 回答 1

2

@AspectJ 风格无法实现您想要的,您需要使用更强大的本机语法。(为什么还要切换?)

这背后的原因是 Java 编译器只能转换为您在方面定义的任何接口实现的子类的@DeclareParents形式,例如:Village

@Aspect
public class Village_Dao_Java {
    public static class VillageDao implements Dao<Village> {
        @Override
        public void insert() {
            Village.insertVillage(this);
        }

        @Override
        public void update() {
            Village.updateVillage(this);
        }

        @Override
        public void delete() {
            Village.deleteVillage(this.getVid());
        }

        public Village getData() {
            return getDataVillage(this.getVid());
        }

    }

    @DeclareParents(
        value = "de.scrum_master.app.Village",
        defaultImpl = VillageDao.class
    )
    private Dao<Village> villageDao;
}

但是这种方法有几个问题:

  • 这些方法尝试从未来的VillageDao子类中访问静态方法Village,因此您不能在其中声明静态方法,VillageDao而必须Village直接在其中声明它们。
  • 相反,如果您直接在其中声明它们,VillageDao您可以调用它们,例如 via VillageDao.insertVillage(this),但是签名public static void insertVillage(Village village)将不再适合,因为this它是一个VillageDao,而不是它自己的子类Village
  • 出于类似原因,您不能调用this.getVid(),因为thisis aVillageDao而不是 a ,但 getter 方法在原始类Village中具有固定签名。Village

等等。Java 编译器的功能不足以完成 AspectJ 编译器所做的事情:将代码直接编入原始类文件中。

Ergo:请坚持使用原生语法。它不仅功能更强大,而且 IMO 也更具可读性。我从来不明白为什么这么多人试图将强大的 AspectJ 哄骗到糟糕的替代 @AspectJ 语法中。不知何故,他们似乎相信他们从纯 Java 语法中获得了收益。我不同意。他们只是得到了技术手段的退化和糟糕的语法,这些语法从来都不打算用于成熟的 AOP。

于 2016-04-23T13:55:54.653 回答