Spring and EJB integration - spring

In my project we are using JSF and Spring WS (web tier), EJB 3.0 (service layer) and JPA (integration layer). We have exposed EJB in Spring container.
All technologies are used with Spring. So, Spring is a used to bind all layers together. Hence, Spring is common for all layers of architecture.
I read that, many features provided by EJB is also available in Spring. Can't we replace EJB with Spring? Why, EJB and Spring both are used together.
I want to understand, what are benefits of using such architecture.

Spring is an alternative for EJB. Usually EJB and Spring are not used together unless it is a legacy application which is already developed based on EJB and Spring is wired later to support dependency injection and other framework benefits.
EJB is a heavy weight container which requires App containers like JBoss, WebSphere, or Weblogic.
Spring is a very light weight container which can be used in in web containers like Tomcat and even in Standalone applications. Also it provides support for many modules from front-end to back-end.
If there is a chance, you should consider replacing EJB with Spring bean in service layer.

Does it means that Spring provides all features which are available from EJB 3.0. Clustering and all other features of EJB can also be acheived from Spring.

Related

How to use Spring AOP with Jersey services

I'm currently porting a large Rest Service app from WebSphere to a Springboot app running in Docker. In the original implementation, I had an AspectJ aspect wrapping around the invocations of all the methods in my JaxRS services. Using the runtime weaver, this worked just fine.
Now, I've moved all of those services and the aspect over to Spring boot, using Spring AOP to load the aspects. The problem I'm having is that the Spring AOP code only appears to apply the aspects to objects that are created/managed by Spring. Unfortunately, the JaxRS objects are being created by Jersey directly, and Spring never gets involved, so the resulting objects are not proxied by the AOP subsystem and as a result none of my aspects get called.
Does anyone know how I can get Spring AOP to manage the JaxRS services (or somehow tell Jersey to allow Spring to create the service instances)?
You cannot with Spring AOP. Spring AOP is a proxy-based "AOP lite" framework which only applies to Spring-managed beans, as explained in the Spring manual. If you need to apply aspects to other classes, it is pretty easy to switch from Spring AOP to native AspectJ via LTW (load-time weaving).

Is Spring Boot just for Microservices ? can i use Spring Boot for Monolithic architecture?

Is Spring Boot just for Microservices or can I use Spring Boot for Monolithic architecture?
Spring Boot in itself has nothing to do with microservices. It's a Spring module which simply makes the configuration of your app easier. As such, it absolutely can be used in a monolithic app.
From the official docs:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run".
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
Features
Create stand-alone Spring applications
Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
Provide opinionated 'starter' dependencies to simplify your build configuration
Automatically configure Spring and 3rd party libraries whenever possible
Provide production-ready features such as metrics, health checks and externalized configuration
Absolutely no code generation and no requirement for XML configuration

Spring container , web container?

I have moderate understanding of Spring Framework, and in various books I read that Spring acts as a container.
Q1) What does it mean to say that Spring is a container?
Q2) Does spring as a container provide services like transactional, connection pooling etc.
Q3) what difference are in containers spring container vs web container -> It might be totally irrelevant comparsion, but if anyone can help me get this understand, really appreciate.
In spring: Spring container contains beans (Java objects that are subject to dependency-injection). It provides the space for residing these beans and maintains the life-cycle of the Java beans.
this is referred as Spring IOC container because of it provides Spring Inversion of Control.
You can learn more about Spring IOC container at http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html
Web Container, Specially Servlet containers contain servlets, filters, listeners, etc. and manages their state and lifecycle.That is the place where you can deploy your java based web applications.(any java web app e.g:- JSP/Servlet based web app, spring based web app etc...)
So keep it remember that these are two different things.

Spring Cloud Netflix - how to access Eureka/Ribbon from traditional web app?

Everything I found on the internet about Spring Cloud Netflix is about running microservices from Boot applications using #EnableEurekaClients and so on.
Now I'm trying to connect my logging microservice within a traditional war application (springmvc, jaxws etc) - piece of legacy which can not be converted to Boot or modified in any way (by technical task).
I've created a new maven module "log-server-client" that knows nothing about upper web layer and intended to be used as a simple dependency in any maven project.
How should I configure access to Spring Cloud Netflix for this simple dependency? At least, how to configure Eureka and Ribbon?
I just extracted some lines of code from RestTemplate and created my custom JmsTemplate (microservice works with jms remoting with apache camel and activemq), exactly how it is done in RestTemplate, but this code stil lacks connection to infrastructure
afaik, we can create a global singleton bean, run a separate thread from this bean, and run Boot app from this thread, but don't you think that it is very ugly and can lead to problems? How it really should be used?
Great question!
One approach is to use a "sidecar". This seems to be a companion Spring Boot application that registers with the Eureka Server on behalf of your traditional web app.
See e.g.:
http://www.java-allandsundry.com/2015/09/spring-cloud-sidecar.html
http://cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html#_polyglot_support_with_sidecar
Another approach is to use the following library:
"A small lib to allow registration of legacy applications in Eureka service discovery."
https://github.com/sawano/eureka-legacy-registrar
This library can be used outside of Spring Boot.

With Spring do you still need a java application server and when?

looks to me you need tomcat or some other servlet engine for the web part.
what about data access part using hibernate and jms? Thanks.
No, you don't need an application server, you can see Spring as a proprietary, modular application server implementation / adapter. But you still need an a servlet container.
Data access part: you can use hibernate and some standalone connection pool
jms: Spring is not a JMS provider, but it nicely integrates POJOs with any JMS provider
Spring also has comprehensive transactions support
Finally you have jmx and aop support built-in and easy integration with bean validation, jpa, web services, rmi, jci, task scheduling, caching...
As you can see you can either use certified application server and Java EE stack or built on top of Tomcat and pick Spring modules you need. Sometimes Spring uses standard Java EE APIs (like JPA), more often it builts its own.

Resources