เมื่อฉันรัน Cobertura จะทำให้เกิดข้อผิดพลาดการเดินสายอัตโนมัติของ Spring ต่อไปนี้:
เกิดจาก: org.springframework.beans.factory.BeanCreationException: เกิดข้อผิดพลาดในการสร้าง bean ด้วยชื่อ 'userResource': การฉีดการอ้างอิงแบบ autowired ล้มเหลว; ข้อยกเว้นแบบซ้อนคือ org.springframework.beans.factory.BeanCreationException: ไม่สามารถฟิลด์ autowire: ส่วนตัว com.dnb.components.storage.service.UserService com.dnb.components.storage.rest.resource.UserResource.userService; ข้อยกเว้นแบบซ้อนคือ java.lang.IllegalArgumentException: ไม่สามารถตั้งค่าฟิลด์ com.dnb.components.storage.service.UserService com.dnb.components.storage.rest.resource.UserResource.userService เป็น com.sun.proxy.$Proxy56 ที่ org .springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
ตามที่แนะนำในโพสต์ที่เกี่ยวข้องอื่น ๆ ฉันพยายามบังคับให้ Spring ใช้ CGLIB โดยเปลี่ยน "proxyTargetClass=true" แต่ส่งผลให้เกิดข้อผิดพลาดอื่น:
เกิดจาก: org.springframework.beans.factory.BeanCreationException: เกิดข้อผิดพลาดในการสร้าง bean ด้วยชื่อ 'userResource': การฉีดการอ้างอิงแบบ autowired ล้มเหลว; ข้อยกเว้นแบบซ้อนคือ org.springframework.beans.factory.BeanCreationException: ไม่สามารถฟิลด์ autowire: ส่วนตัว com.dnb.components.storage.service.UserService com.dnb.components.storage.rest.resource.UserResource.userService; ข้อยกเว้นแบบซ้อนคือ org.springframework.beans.factory.BeanCreationException: เกิดข้อผิดพลาดในการสร้าง bean ด้วยชื่อ 'userService': การฉีดการอ้างอิงแบบ autowired ล้มเหลว ข้อยกเว้นแบบซ้อนคือ org.springframework.beans.factory.BeanCreationException: ไม่สามารถฟิลด์ autowire: com.dnb.components.storage.repository.UserRepository com.dnb.components.storage.service.UserService.repository; ข้อยกเว้นแบบซ้อนคือ org.springframework.beans.factory.BeanCreationException: เกิดข้อผิดพลาดในการสร้าง bean ด้วยชื่อ 'userRepository': การประมวลผลภายหลังของวัตถุของ FactoryBean ล้มเหลว; ข้อยกเว้นแบบซ้อนคือ org.springframework.aop.framework.AopConfigException: ไม่สามารถสร้างคลาสย่อย CGLIB ของคลาส [คลาส com.sun.proxy.$Proxy54]: สาเหตุทั่วไปของปัญหานี้ ได้แก่ การใช้คลาสสุดท้ายหรือคลาสที่มองไม่เห็น ข้อยกเว้นแบบซ้อนคือ java.lang.IllegalArgumentException: ไม่สามารถซับคลาสคลาสสุดท้ายคลาส com.sun.proxy.$Proxy54
นี่คือลักษณะของคลาส Repository:
@Transactional
public interface UserRepository extends CrudRepository<User, Long> {
public User findByCrmId(String crmIdId);
}
นี่คือบริการที่ได้รับการฉีดเข้าไปใน UserRepository:
@Service
@Transactional
public class UserService extends TraceablePersistenceService<User, UserRepository> {
@Autowired
UserRepository repository;
@Transactional
public Iterable<User> findAll() {
return repository.findAll();
}
public User findOne(Long id) {
return repository.findOne(id);
}
}
การกำหนดค่าของเราอยู่ใน
@Configuration
@EnableTransactionManagement(proxyTargetClass=true)
@EnableJpaRepositories(basePackages = {"com.dnb.components.storage.repository"})
@Profile("inmemory")
public class InMemoryStandaloneStorageConfig extends BasicJpaStorageConfig {
....
(ละเว้นเพื่อความกระชับ)
UserResource.java:
@Component
@Path(UserResource.uri)
public class UserResource extends AbstractResource {
public final static String uri = BASE_URI + "/users";
@Override
public String getURI() {
return BASE_URI + uri;
}
@Autowired
private UserService userService;
...
ดูเหมือนว่าคลาส Repository ที่สร้างโดย Spring Data JPA จะไม่สามารถพรอกซีได้ด้วยวิธีใดวิธีหนึ่งโดยไม่ทำให้การเดินสายอัตโนมัติเสียหาย
มีการแก้ไขสำหรับสิ่งนี้หรือไม่? เป็นความคิดที่ดีหรือไม่ที่จะใส่คำอธิบายประกอบวิธีการ Repository ด้วย @Transactional คำอธิบายประกอบควรอยู่ในระดับบริการเท่านั้นหรือไม่