1

For example, the User entity :

@Configurable
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
@DynamicInsert
@DynamicUpdate
public class User extends AbstractAggregateRoot<User> implements Serializable {

    private static final long serialVersionUID = 1L;

    @Autowired
    @Transient
    private UserRepository userRepository;

    @Autowired
    @Transient
    private EmailRepository emailRepository;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String nickname;

    @Version
    @Column(columnDefinition = "timestamp")
    @Source(value = SourceType.DB)
    private Timestamp ts;

    public void doSavedComplete() {
        UserSavedEvent event = new UserSavedEvent();
        event.setUser(this);
        event.setTs(new Date());
        andEvent(event);
        userRepository.save(this);
    }

    @Data
    public static class UserSavedEvent {
        private User user;
        private Date ts;
    }
}

and it's repository :

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

}

When I create a new User object:

User ob = new User();

the user's userRepository and emailRepository are null, because User and Email have respective Repository classes, UserRepository and EmailRepository;

When I use another class UserAggregateRoot which has no Spring Data JPA Repository, the load time weave works great:

@Configurable
@Entity
public class UserAggregateRoot extends AbstractAggregateRoot<User> implements Serializable {
    @Autowired
    @Transient
    private UserRepository userRepository;

    @Autowired
    @Transient
    private EmailRepository emailRepository;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String nickname;

    @Version
    @Column(columnDefinition = "timestamp")
    @Source(value = SourceType.DB)
    private Timestamp ts;
}

UserAggregateRoot has no Repository, so when I create a new UserAggregateRoot object, the fields of UserAggregateRoot, UserRepository and EmailRepository are not null;

So I would like to know why? Is it a bug?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
陈振阳
  • 131
  • 1
  • 4
  • Could you please point a link to the feature you want to utilize here? Also, the configuration you are using might be helpful. – Jens Schauder Nov 13 '17 at 07:08
  • Oh I think I found it: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop-atconfigurable right? Do you happen to see messages matching this in the log: "Instances of @Configurable objects created before the aspect has been configured will result in a message being issued to the debug log and no configuration of the object taking place. "? – Jens Schauder Nov 13 '17 at 07:30
  • thanks,I have read the doc that link above point; Now ,I know about it a little;so I have a new question, the User class is a hibernate entity, it's not a spring bean;I try to configure annotation Configurable and DependsOn on User class, it doesn't work; so what should I do, can I Inject UserRepository into User object;@JensSchauder – 陈振阳 Nov 13 '17 at 08:50
  • `@Configurable` will not work with Spring AOP, it will only work if you use AspectJ directly, with spring-aspects/`AnnotationBeanConfigurerAspect` correctly weaved into your code (`User` class in your particular example). – Nándor Előd Fekete Nov 14 '17 at 02:54
  • thanks,I think that I should learn aspectj first.@Fekete,@Jens – 陈振阳 Nov 14 '17 at 10:12

0 Answers0