0
이고 EntityManager와 UserTransaction 두 개의 주입 클래스가 있습니다. 나는 UserTransaction이 왜 주석을 달지 않고 바로 주입 될 수 있는지 이해하지 못한다. 그러나 EntityManager는 별도의 클래스에서 @Produces로 정의되고 주석을 달아야 만한다.이 예제에서 두 클래스에 대한 종속성 삽입과 @Produces 주석은
미리 감사드립니다.
CarManager.java
public class CarManager {
@Inject
private EntityManager em;
@Inject
private UserTransaction utx;
private Long id;
private Logger log = Logger.getLogger(CarManager.class.getSimpleName());
public void setId(Long id) {
this.id = id;
}
/**
* Returns either a new instance or a managed instance of {@link Car}.
* The produced entity should be dependent scoped to avoid incompatible proxies between
* JPA and CDI.
*/
@Produces
public Car getCar() {
if (id == null) {
log.info("Returning new instance of Car");
return new Car();
}
log.info("Finding instance of Car with id " + id);
return em.find(Car.class, id);
}
...
}
EntityManagerProducer.java
public class EntityManagerProducer {
@Produces
@PersistenceContext
private EntityManager em;
}
안녕하세요, Petr하지만 UserTransaction은 CDI 빈이 아니라 리소스 빈이라는 말 때문에 다음과 같이 사용합니다 : public class TransactionServlet extends HttpServlet { @Resource UserTransaction transaction; ... } – marlon
이것은 컨테이너에 따라 다를 수 있으며, 어느 것이 사용하고 있습니까? –