문제

I'm not a fan of JSF. Spring MVC makes a lot of sense to me and I've used it in the past.

Can you talk me out of it or provide tips for integrating it nicely into JEE6 and share and gotchas with CDI.

I don't mind having an application context just for the MVC stuff but if I use @Inject or @EJB in my MVC controllers, what's going to go wrongly?

Seam3 seems interesting though is it ready for production?

도움이 되었습니까?

해결책

Can you talk me out of it or provide tips for integrating it nicely into JEE6 and share and gotchas with CDI

Maybe I'm wrong but I think that you somehow have to make a choice between CDI and the DI part of Spring, one of them has to be responsible of creating objects for the magic to occur. So if you want to use CDI with Spring MVC, you'd have to make your controllers CDI beans. From JavaOne 2010 : Java EE 6 Panel "What do we do now?" notes:

SpringMVC and CDI? => Technically possible: use CDI beans as controllers (but Reza says they're not seeing enough demand for SpringMVC to do the work).

I also warmly suggest to have a look at Java EE 6 (CDI / EJB 3.1) XOR Spring Core Reloaded.

I don't mind having an application context just for the MVC stuff but if I use @Inject or @EJB in my MVC controllers, what's going to go wrongly?

Spring provides built-in support of "at inject" from JSR-330 and also provides some magic for @EJB. If that's all you're looking for, Spring will work. But that's just not what CDI (JSR-299) gives you.

다른 팁

Another option would be to use JAX-RS as a controller (with the Jersey implementation you can forward control to a JSP), and use JSP's for your view. This way you wouldn't have to worry about integrating Spring with Java EE.

For example, you could have a HomeController:

@Path("/")
@Stateless
public class HomeController {

    @Context
    HttpRequest request; // Injects request object so you set attributes.

    @EJB // Inject your EJB
    EmployeeServiceLocal employeeService;

    @GET
    public Viewable getHomePage() {

        Employee employee = employeeService.getEmployee();
        request.setAttribute("employee", employee);
        return new Viewable("/home.jsp", null);
    }
}

This would direct you to a JSP called home.jsp, and you would be able to get your data on the JSP via the request object. Note that JAX-RS is used for RESTful Web Services, so to understand what is going on in the code below you would have to have an understanding of REST.

Using CDI from Spring explains how to build a CDI-to-Spring bridge with a BeanFactoryPostProcessor which imports all beans from a CDI BeanManager into a Spring application context.

Pascal is right in saying you have to make a choice between Spring DI and CDI, but with this approach you can make the choice per module.

In a typical WAR layout, there is one web module using a number of service and persistence modules in WEB-INF/lib. With the CDI-to-Spring bridge, you can turn the WEB-INF/lib modules into CDI bean archives, while the web module is not a bean archive and only uses Spring DI.

The bridge accesses the CDI BeanManager via JNDI and creates a Spring factory bean for each CDI bean.

I'm using this bridge precisely for your use case: Integrating a Spring MVC web frontend with a Java EE 6 backend.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top