Posts

Showing posts with the label Spring Framework

MScIT&CA Semester I December 2017 Advance Java Question Paper

Image

Mapping List in Collection Mapping

Mapping List in Collection Mapping (using xml file) If our persistent class has List object, we can map the List easily either by <list> element of class in mapping file or by annotation. Here, we are using the scenario of Forum where one question has multiple answers. In this example, we are going to see full example of collection mapping by list. This is the example of List that stores string value not entity reference that is why are going to use element instead of one-to- many within the list element. Question.java package com.smgc; import java.util.List; public class Question {        private int id ;        private String qname ;        private List<String> answers ;        //generate getters and setters } Question.hbm.xml <? xml version = '1.0' encoding = 'UTF-8' ?> <! DOCTYPE hi...

Spring with Caster

Spring with Castor Example 1. Spring and Castor Integration 2. Example of Spring and Castor Integration By the help of CastorMarshaller class, we can marshal the java objects into xml and vice-versa using castor. It is the implementation class for Marshaller and Unmarshaller interfaces. It doesn't require any further configuration bydefault. Example of Spring and Castor Integration (Marshalling Java Object into XML) You need to create following files for marshalling java object into XML using Spring with Castor: 1. Employee.java 2. applicationContext.xml 3. mapping.xml 4. Client.java Required Jar files To run this example, you need to load: Spring Core jar files Spring Web jar files castor-1.3.jar castor-1.3-core.jar Employee.java If defines three properties id, name and salary with setters and getters. package com.smgc; public class Employee { private int id; private String name; private float salary; public i...

Spring with Xstream

Spring with Xstream Example 1. Spring and Xstream Integration 2. Example of Spring and Xstream Integration Xstream is a library to serialize objects to xml and vice-versa without requirement of any mapping file. Notice that castor requires an mapping file. XStreamMarshaller class provides facility to marshal objects into xml and vice-versa. Example of Spring and Xstream Integration (Marshalling Java Object into XML) You need to create following files for marshalling java object into XML using Spring with Xstream: 1. Employee.java 2. applicationContext.xml 3. Client.java Required Jar files To run this example, you need to load: Spring Core jar files Spring Web jar files xstream-1.3.jar Employee.java If defines three properties id, name and salary with setters and getters. package com.smgc; public class Employee { private int id; private String name; private float salary; public int getId() { return id; } ...

Spring with JAXB

Spring and JAXB Integration Example 1. Spring and JAXB Integration 2. Example of Spring and JAXB Integration JAXB is an acronym for Java Architecture for XML Binding . It allows java developers to map Java class to XML representation. JAXB can be used to marshal java objects into XML and vice-versa. It is an OXM (Object XML Mapping) or O/M framework provided by Sun. Advantage of JAXB No need to create or use a SAX or DOM parser and write callback methods. Example of Spring and JAXB Integration (Marshalling Java Object into XML) You need to create following files for marshalling java object into XML using Spring with JAXB: 1. Employee.java 2. applicationContext.xml 3. Client.java Required Jar files To run this example, you need to load: Spring Core jar files Spring Web jar files Employee.java If defines three properties id, name and salary. We have used following annotations in this class: 1. @XmlRootElement It specifies the ...

Spring Bean Scopes

Spring Bean Scope In the spring bean configurations, bean attribute called 'scope' defines what kind of object has to created and returned. There are 5 types of bean scopes available, they are: 1) singleton:  Returns a single bean instance per Spring IoC container. 2) prototype:  Returns a new bean instance each time when requested. 3) request:  Returns a single instance for every HTTP request call. 4) session:  Returns a single instance for every HTTP session. 5) global session:  global session scope is equal as session scope on portlet-based web applications. If no bean scope is specified in bean configuration file, then it will be by default 'singleton'. The scope of a bean defines the life cycle and visibility of that bean in the contexts in which it is used. Spring defines 5 types of scopes: singleton prototype request session globalSession When defining a <bean> in Spring, you have the option of declaring a s...