Search code examples
javaspringjakarta-eeejbcdi

Migrating CDI/EJB annotations to Spring annotations


I'm trying to replace my CDI/EJB annotations with Spring ones. But I'm struggeling how to do it right.

This is what I have in CDI/EJB:

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
class Service {
    @Inject
    EntityManager em;
}


@Named
@RequestScoped
class Facade {
    @Inject
    Service service;
}

Now I would do the following:

@Stateless
@Transactional
@Repository
class Service {
    @Inject
    EntityManager em;   
}

What about the stateless? What is the aquivalent in spring? Obvious I cannot just remove this annotation, bc then I'm getting these exception:

javax.el.PropertyNotFoundException: /input.xhtml @15,30 registerButtonAction="#{facade.createNew()}": The class 'Facade$Proxy$_$$_WeldClientProxy' does not have the property ...

Further:

@Named
@Service
class Facade {
    @Autowired
    Service service;
}

Do I have to simply replace all @Inject annotations with @Autowired? Is there something in Spring that takes care of EL naming, so that I can remove the @Named?

Do I have to annotate my JPA entities too?


Solution

  • Stateless and Stateful Beans are EJB concepts, but Spring offers similar services through Service Beans. Put the @Service annotation in your Business Logic classes, and if you want your beans to be "Stateless" or "Stateful" just configure your bean scope (like Request or Session).

    Spring also has a built-in transaction management API, so your Transaction annotations may need to be changed.

    Finally, Spring is compatible with many persistence frameworks including JPA. IF you want to keep JPA it is OK, and feel free to change it for another technology if you desire (maybe Hibernate, or MyBatis)