6 Hybris Spring
6 Hybris Spring
J2EE is a collection of Java APIs owned by Oracle that software developers can use to write server-side
applications. It was formerly known as Java 2 Platform, Enterprise Edition, or J2EE.
Java EE applications are hosted on application servers, such as IBM's WebSphere, Weblogic, Oracle's GlassFish
or Red Hat's WildFly server, all of which run either in the cloud or within a corporate data center.
J2EE Archictecture
Spring
• Is an open source application framework that aims to make J2EE development easier
• Development made easier with these concepts / techniques
➢ IoC & DI = Promotes loose coupling
➢ POJO = Lightweight, easy testing
➢ AOP = Promotes separation of concerns, maintainability etc.
➢ Templates = Provides a standard programming model which does the heavy lifting for you
➢ Spring WEB framework has a well-designed web MVC framework
JDK 9 Spring 5.0 Spring Boot 2.0 Spring IO platform (Spring IO foundation + Spring IO execution layers)
Spring is one-stop shop for all your enterprise applications. However, Spring is modular, allowing you to pick
and choose which modules are applicable to you, without having to bring in the rest.
Beans = Provides BeanFactory, which is implemenation of factory pattern ( factory pattern is used to create
instances of different classes of same)
Core = Provides IoC (Inversion of Control) & DI (Dependency Injection)
Contact us for more information [email protected] (Java–Salesforce–SAP Portal–UI5/Fiori–Hybris)
Context = Provides the implementation of ApplicationContext. It uses Beans and Cores module
Expression Language = Used for querying and manipulating objects at runtime
AOP (Aspect Oriented Programming) module = AOP allows to define interceptors and point-cuts to seprate
code as required.
Ascepts module = supports integration with AspectJ, which is again a powerful AOP framework.
Instrumenation module = Instumentation is the ability to monitor the level of products performance, to
diagnose the errors and write the trace information
Web = Provides support for features like multipart file upload, initialization of the IoC container using servlet
listeners, application context etc.
Servlet (spring-web-mvc) = Provides MVC implementation for web applications.
Portlet (spring-webmvc-portlet) = Provides the support for spring based portlets
Test = Supports to test the spring modules using junit frame work
Spring beans are objects which are created and managed completely by spring container. Spring container
manages the life cycle of beans (initializing, creating, destroying etc.)
DI (Dependency Injection) = Dependency Injection is a design pattern on which dependency of object is injected
by framework rather than created by Object itself.
Dependency Injection reduces coupling between multiple objects as its dynamically injected by framework
One of the implementation of DI is Inversion of Control (IOC) on which framework like Spring controls object’s
dependency.
1) Constructor Injection = The container invokes a class constructor with a number of arguments, each
representing a dependency on other class.
2) Setter/Getter Injection = The container calling setter methods on your beans after invoking a no-
argument constructor to instantiate your bean.
When to use constructor based Injection and when to use setter based injection?
To make it simple, we can use constructor based dependency injection for mandatory dependencies and setter
based injection for optional dependencies. It is a rule of thumb!!
For example - If you want to instantiate a class, you always do it with its constructor. So if you are using
constructor based injection, the only way to instantiate the class is through that constructor. If you pass the
dependency through constructor it becomes evident that it is a mandatory dependency.
On the other hand, if you have a setter method in a POJO class, you may or may not set value for your class
variable using that setter method. It is completely based on your need. i.e. it is optional. So if you pass the
dependency through setter method of a class it implicitly means that it is an optional dependency.
Model = Represents data (The model encapsulates the application data & in general they will consist of POJO)
View = Represents UI part (The view is responsible for rendering the model data and in general it generates
HTML output that the client’s browser can interrupt)
Controller = Manages the application flow (The controller is responsible for processing user requests and
building an appropriate model and passed it to the view for rendering)
Singleton = Only one per spring container (means spring creates only one object during application context
initialization). Same object is used. This is default and by default all the beans in spring are singleton.
Prototype = New bean created with every request or reference. The bean created when the getBean(“..”) is
called not during initialization.
Web-aware context bean scopes = Spring ties very well with web application (ex- JSP, Servlet). You can tie bean
scopes to the request and session.
global session = New bean per global HTTP Session (portlet context).
Prototype creates a brand-new instance every time you call getBean on the ApplicationContext. Whereas for
Request, only one instance is created for an HttpRequest. So in a single HttpRequest, I can call getBean twice on
Application and there will ever be one bean instantiated, whereas that same bean scoped to Prototype in that
same single HttpRequest would get 2 different instances.
HttpRequest scope
Mark mark1 = context.getBean("mark"); Mark mark2 = context.getBean("mark");
mark1 == mark2; //This will return true
Prototype scope
Mark mark1 = context.getBean("mark"); Mark mark2 = context.getBean("mark");
mark1 == mark2; //This will return false
Autowiring = Autowire is an easy way of doing dependency injection
byName = Spring container looks for bean name / id same as property name of the class for autowiring
Note = if you notice above xml the properties are removed because of autowire=”byName”
byType = Spring container selects the bean by class type for autowiring (The data type of the attribute should
be same as the data type of the bean defined in the spring configuration file)
constructor = spring searches for the type of constructor argument inside spring configuration file
Note = You can specify autowire for all beans using a single tag “default-autowire” some thing like this.
Spring frame work provides 3 main ways to confiure (1) XML (2) Annotations (3) Java-based configuration
• Starting from spring 2.5 version the Dependency Injection can be done using annotations.
• Annotation injection is performed before xml injection
• Annotation wiring is not turned on in the Spring container by default. So, before we can use annotation-
based wiring, we will need to enable it in our Spring configuration
<beans>
<context:annotation-config/>
<!-- bean definitions go here -->
</beans>
@Autowired = Can apply to constructor, bean property setter method, any method & property
Java-based configuration option enables you to write most of your Spring configuration without XML but with
the help of few Java-based annotations.
@Configuration = Annotating a class with this indicates the class can be used by the Spring IoC container as a
source of bean definitions
Spring scans all the beans that has annoted with @Component, @Service, @Controller, @Repository etc.
Can we just use @Component for all the components ( means instead of @Service, @Controller,
@Repository) for auto scanning in all layers?
Yes, you can use @Component and spring will scan all your components but is not good practice. For readibility
and understandable you need to use @Service, @Controller, @Repository for specific layers.
What is the difference between bean name vs bean id in the xml configuration file?
Both id and name used to uniquely identify a bean. If you a bean have more than one name then it is considered
as alias. Usability of alias is in the case where application is too large and we want loose coupling of even bean
names among each other.
What happens if you have same bean name in different xml files ( lets say beans1.xml & beans2.xml)?
Beans1.xml = <bean id=”test” name=”test1, test2” class=”com.Testing” />
Beans2.xml = <bean id=”abc” name=”xyz, test2” class=”com.Hello” />
The documenation says the last bean definition with the same id or name wins.
What is @Profile?
@Profile – used to execute a perticular class / method against dev, test, prod etc. For example in dev we may
need to use Mock mail server and in production need to use SMTP server for sending mails. You can define the
enviornment in the properties file and then annotate with @profile at method level or class level etc.
Request Related Annotations
@RequestMapping = Used to map web requests onto specific handler classes and / or handler methods
@RequestMapping(method =RequestMethod.POST)
@RequestParam Sometimes we get parameters in the request URL, mostly in GET requests. We can use
@RequestMapping with @RequestParam annotation to retrieve the URL parameter and map it to the method
argument.
@RequiredHardLogin Custom annotation written in hybris for validating the request is secure or not before
continuing the request.
@PathVariable Used where the data is fetched dynamically depending on the URL
For example, consider an e-commerce website where all the products under a particular category has to be
retrieved
@RequestParam used for accessing the values of the query parameters whereas @PathVariable used for
accessing the values from the URI template.
URI template is a way to specify a URL that includes parameters that must be substituted before the URL is
resolved. It’s a part of URL containing variable names enclosed in braces
example - www.example.com/user/{id}.