Posts

Showing posts from September, 2017

How to configure List in Spring Bean

We can configure List in Spring bean using <list> tag within <property> tag. We can configure list literals and object reference. Let's configure list of String and Address below Employee class- package com.javagladiator.spring.core.trainning; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Employee { private Integer empId; private String name; private List<String> phones; private List<Address> addresses; public Integer getEmpId() { return empId; } public void setEmpId(Integer empId) { this.empId = empId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getPhones() { return phones; } public void setPhones(List<String> phones) { this.phones = phones; } public List<Address> getAddresses() { return addresses; } public void setAddresses(List<Address> addresses) { this

How to use ref element in Spring Framework

We have seen how to configure object reference using ref attribute of property element in previous blog We will configure object reference using <ref> element. We need to use one of the following attribute in <ref> element 1. local 2. parent 3. bean It is possible to create more than one container by making one container as parent for another container. All container will have separate Spring configuration file(XML file). Child container will load its own configs and reference of parent container in constructor. Below example loads parent and its own configs. ApplicationContext appParentContext = new ClassPathXmlApplicationContext("application-parent.xml"); String configs[] = {"application.xml"}; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configs, appParentContext); 1. local: If local attribute is used in ref element than Spring IoC container will try to find dependency in local container b

Spring Framework Dependency Injection Part 2 : Configure Object in Spring Bean

We have seen how to configure primitives of class in previous blog . We will see how to configure objects of Class. If ClassA whats to call method of ClassB to complete its business logic, ClassA has to create object of ClassB. So ClassA is depends upon ClassB. Let's see how Spring helps us to inject object of ClassB into ClassA. We will create 2 classes i.e. Employee and Address in our example. We will see how Spring framework helps us to inject object of Address into Employee. Employee class has primitive as well as object. package com.javagladiator.spring.core.trainning; public class Employee { private Integer empId; private String name; private Address address; public Integer getEmpId() { return empId; } public void setEmpId(Integer empId) { this.empId = empId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) {

Dependency Injection in Spring framework

Image
Before understanding Dependency Injection(DI) in Spring framework, I recommend you to understand Dependency Injection design pattern As explained in above link, Dependency Injection is a design pattern in which object's properties are set by external entity. External entity can be simple java class which configure object's properties. Spring core (IoC) container acts as external entity to configure object's properties. There are 2 ways in Spring framework to achieve DI. 1. Setter Injection 2. Constructor Injection Let's write simple example to understand DI in Spring framework. We will set primitive values in Employee class using setter injection. I have used maven to add Spring framework related jars. You can add these external jars in eclipse by right click on project --> Build Path --> Configure Build Path... --> Add external JARs --> select jar --> Ok.  Project Structure:  POM.xml:  <project xmlns:xsi="http://www.w3.org/

Introduction to Spring framework and its modules

Image
Spring framework is widely used, light weight application framework to develop enterprise application. You can develop all layers or specific layer of your application using spring framework. It does not force you to use it in your entire application. It gives you flexibility to develop one particular layer in spring and remaining layers in other frameworks. Spring is non-invasive framework . Non-invasive frameworks are those frameworks which does not force you to extend/implement framework specific classes/interfaces. Since you don't need to extend/implement spring related class/interface in your spring bean code, you don't need to override any methods. So your spring bean class will be clean, you will write only business logic in bean classes. You can reuse these class with other framework as well. You can use spring framework to develop any type of applications like desktop applications or web applications Spring does not depends on any third party container. Y