Regux

目標文字:

when
sampleEntity : SampleEntity($code1:code)
sampleEntity2 : SampleEntity2()
if ($code1 == "1" && #max(1, 0) == 1 && #trim("apple ").equals("apple")) do[_1]
else if ($code1 == "2") do[_2]
then

需透過Regular pattern, 取得井字號後面的function name以及input args

原始Pattern #(\w+)\((.+)\)

  • function name正確取得max
  • 但args卻取了整串到: 1, 0) == 1 && #trim("apple ").equals("apple")
  • 看起來是中間的 . 範圍太大, 導致到最後一個 \)之前都剛成args了

改。Pattern #(\w+)\(([^\)]+)\)

  • 加入了不等於 ^ 的判斷
  • function name正確取得maxstr
  • args也正確抓到了1, 0以及apple
  • 但多了使用限制:input args中不能有close字元
  • #trim(" ) ")就無法接受
    正在思考更好的解決方案…

A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance

Hibernate/JPA在使用到 @OneToMany , 也就是Entity中的關聯是一個 Collection時

當該Object已經在persistent scope中, 一定要避免assign另一個reference到那個 @OneToMany

否則錯誤就會發生

org.hibernate.HibernateException:
A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:

如:

@JsonManagedReference("process_operation")
@OneToMany(mappedBy = "process", fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, orphanRemoval = true)
private List operations;

不使用setOperations(new ArrayList)

而是 getOperations().clear() 再 getOperations().add()

Aspect-oriented programming

Aspect-oriented programming

Aspect

Regular classes or regular classes annotated with the @Aspect annotation.

<br /><br />...



Join point

Always represents a method execution.

Advice

An interceptor, maintaining a chain of interceptors around the join point.

  • Before advice
  • After returning advice
  • After throwing advice
  • After (finally) advice
  • Around advice
<br /><br /><br /><br />

Pointcut

A predicate that matches join points.

<br /><br /><br />

Supported Designators

Common pointcut definitions:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)
throws-pattern?)
  • modifiers-pattern: *, public, private, etc. (Spring AOP only supports advising public methods.)
  • ret-type-pattern: int, long, etc.
  • declaring-type-pattern If the class and advice are in the same package then package name is not required.
  • name-pattern: wildcard supported.
  • param-pattern: Two dots(..) means any number and type of parameters.

Any public method:

execution(public * *(..))

Any method with a name beginning with “set”:

execution(* set*(..))

Any method defined by the AccountService interface:

execution(* com.xyz.service.AccountService.*(..))

Any method defined in the service package:

execution(* com.xyz.service.service.*.*(..))

Any method defined in the service package or a sub-package:

execution(* com.xyz.service..*.*(..))

see more @ Sharing common pointcut definitions

Proxies

  • Standard JDK dynamic proxies (by interface) (default)
  • CGLIB proxies (by subclassing)
<!-- force to use CGLIB proxies -->

<!-- other beans defined here... -->

AspectJ

A style of declaring aspects as regular Java classes annotated with annotations.
In Spring AOP, it is not possible to have aspects themselves be the target of advice from other aspects. The @Aspect annotation on a class marks it as an aspect, and hence excludes it from auto-proxying.

@Aspect
public class NotVeryUsefulAspect {
@Pointcut("execution(* transfer(..))") // the pointcut expression
private void anyOldTransfer() {}
}

Enabling @AspectJ Support:

<br />

Example

spring-aop.xml

<br /><br /><br /><br /><br />

EntityAspect

public class EntityAspect {
public void prePersist(final GenericEntity entity) {
entity.setCreatedTime(LocalDateTime.now());
entity.setModifiedTime(entity.getCreatedTime());
}
public void preUpdate(final GenericEntity entity) {
entity.setModifiedTime(LocalDateTime.now());
}
}