使用AOP对公共字段进行保存(后台管理系统)

spring  hibernate

1、新建公共字段bean

@MappedSuperclass
public class BaseEntity {
	
	@Id
	@Column(name="id")
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	
	@Column(name = "isdelete")
	private Integer isdelete;
	
	@Column(name = "createrId")
	private Long createrId;
	
	@Column(name = "createDate")
	private Date createDate;
	
	@Column(name = "updaterId")
	private Long updaterId;
	
	@Column(name = "updateDate")
	private Date updateDate;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Integer getIsdelete() {
		return isdelete;
	}

	public void setIsdelete(Integer isdelete) {
		this.isdelete = isdelete;
	}

	public Long getCreaterId() {
		return createrId;
	}

	public void setCreaterId(Long createrId) {
		this.createrId = createrId;
	}

	@DateTimeFormat(pattern="yyyy-MM-dd")
	public Date getCreateDate() {
		return createDate;
	}

	@DateTimeFormat(pattern="yyyy-MM-dd")
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Long getUpdaterId() {
		return updaterId;
	}

	public void setUpdaterId(Long updaterId) {
		this.updaterId = updaterId;
	}

	@DateTimeFormat(pattern="yyyy-MM-dd")
	public Date getUpdateDate() {
		return updateDate;
	}
	
	@DateTimeFormat(pattern="yyyy-MM-dd")
	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}
}

2、让业务实体继承
@Entity
@Table(name="thou_featurekeyword")
public class FeatureKeyWord extends BaseEntity{

	
	@Column(name = "name")
	private String name;
	@Column(name="paixu")
	private Integer paixu;
	
	public Integer getPaixu() {
		return paixu;
	}
	public void setPaixu(Integer paixu) {
		this.paixu = paixu;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	

}

3、编写AOP方法
public class serviceSaveAOP {

	public void saveBefor(JoinPoint jp){  
		Object[] obj = jp.getArgs();
		for(Object o:obj){
			if(o instanceof BaseEntity){
				BaseEntity baseEntity = (BaseEntity) o;
				Operator op = (Operator) SecurityContextHolder.getContext()
					    .getAuthentication()
					    .getPrincipal();
				if(baseEntity.getId()!=null){
					GenericManagerImpl genericManagerImpl = (GenericManagerImpl) jp.getTarget();
					BaseEntity oldbaseEntity = (BaseEntity) genericManagerImpl.getById(baseEntity.getId());
					baseEntity.setCreaterId(oldbaseEntity.getId());
					baseEntity.setCreateDate(oldbaseEntity.getCreateDate());
					baseEntity.setUpdaterId(op.getId());
					baseEntity.setUpdateDate(new Date());
				}else{
					baseEntity.setCreaterId(op.getId());
					baseEntity.setIsdelete(1);
					baseEntity.setCreateDate(new Date());
					baseEntity.setUpdateDate(new Date());
				}
			}
		}
	}
}

解释:此方法是在保存之前,设置它的创建时间,创建人,逻辑删除字段,修改时间,修改人,因为我们系统的保存和修改共用一个方法,其判断根据ID。Operator op就是操作人实体,GenericManagerImpl genericManagerImpl是service的公共父类
4、配置XML中AOP
    <aop:config>
        <aop:pointcut id="serviceSavePointCuts"
            expression="execution(* com.risejoin.framework.service.impl.GenericManagerImpl+.save(..))"/> 
        <aop:aspect ref="saveAOP">
        	<aop:before pointcut-ref="serviceSavePointCuts" method="saveBefor"/>
        </aop:aspect>
    </aop:config> 

详情查看AOP配置
5、测试
创建单元测试,调用save方法,看数据库中是否有值

你可能感兴趣的:(java,AOP)