Dependency Injection via set collection property

Dependency Injection via set collection property is simple just object injection through set.

Employee.java


package com.pkjavacode.com;

public class Employee {
String name;

public void setName(String name) {
this.name = name;
}

public void getEmployeeDetails() {
System.out.println(name);
}

}

 

EmployeeTest.java


package com.pkjavacode.com;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class EmployeeTest {

/**
* @param args
*/
public static void main(String[] args) {

BeanFactory b = new XmlBeanFactory(new ClassPathResource("beans.xml"));

Object o = b.getBean("emp");
Employee e = (Employee) o;
e.getEmployeeDetails();
}

}

beans.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="emp" class="com.pkjavacode.com.Employee">
<property name="name">
<set value-type="String">
<value>Pradeep</value>
<value>Varun</value>
<value>Amit</value>
<value>Monty</value>
<value>Raj</value>
</set>
</property>
</bean>
</beans>

Advertisements

Features of Spring Framework

Spring Framework provides many features which some are as follows:

 

1. Lightweight: Spring Framework is lightweight that is it takes less resources in request processing.

2.Inversion of control (IOC): Loose coupling is achieved in spring using the technique Inversion of Control. It acheived through dependency injection(DI).

3. Aspect Oriented Programming (AOP): Spring framework supports AOP and enables cohesive development by separating business logic from system services.

4. Container: Spring Framework(IoC as container) manages the life cycle and configuration of application objects.

5. MVC Framework: Spring supports MVC(Model, View, Controller) web application framework, which build on core Spring functionality. It is highly configurable through strategy interfaces, and accommodates multiple view technologies like JSP, Velocity, Tiles, iText, and POI.

6. JDBC Exception Handling: The Spring framework offers a meaningful exception hierarchy, which simplifies the error handling strategy.Spring framework provides better Integration services with Hibernate, JDO and iBATIS

7. Transaction Management: Spring framework provides a generic abstraction layer for transaction management. This allowing the developer to add the pluggable transaction managers, and making it easy to demarcate transactions without dealing with low-level issues. it may used in container less environments.It is not tied to J2EE environments.

Advantages of Spring framework

Spring provide a lot of benifites to developer few are as follows:

  • Use what you need and leave if you don’t need anythings due to it’s layered architecture.
  • Spring provide facilities of pojo Programming which means through pojo it’s possible to easy continuous integration and testability.
  • Dependency Injection(DI) and Inversion of Control(IoC) Simplifies jdbc.
  • Open source and no vendor lock-in.

Dependency Injection(DI) via constructor

Here is an example where you will see how to inject object via constructor.

Write Employee.java class with constructor and getter method.


package com.pkjavacode.com;

public class Employee {
String name;

public String getName() {
return name;
}

public int getSalary() {
return salary;
}

public String getCountry() {
return country;
}

int salary;
String country;

public Employee(String name, int salary, String country) {
this.name = name;
this.salary = salary;
this.country = country;
}

public String toString() {
return "Name :" + name + "Salary :" + salary + "Country :" + country;

}

public void getEmployeeDetails() {
System.out.println("Employee Name :" + getName() + ", Salary :"
+ getSalary() + ", Country :" + getCountry());
}

}

 

After that create EmployeeTest.java with main() method where you will have to create as follows.


package com.pkjavacode.com;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.pkjavacode.com.Employee;

public class EmployeeTest {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"beans.xml");
Employee emp = (Employee) context.getBean("emp");
emp.getEmployeeDetails();
}

}

 

 

Then create beans.xml file to configure.

 


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="emp" class="com.pkjavacode.com.Employee">
<constructor-arg value="Pradeep Yadav" />
<constructor-arg value="28000" />
<constructor-arg value="India" />
</bean>
</beans>

 

Output :

 

Employee Name :Pradeep Yadav,  Salary :28000,  Country :India

Spring Framework Definition

The Spring framework is written by Rod Johnson and was first released under the Apache 2.0 license in June 2003. It is an application framework and inversion of control(IOC) container for the Java based platform.The framework is fully open source and it’s core features can be used by any Java based application. Through spring framework you can develop any size of application with low coupling. It is based on layered architecture.

Dependency Injection(DI) via setter method

Dependency Injection(DI) via setter method is easy and take following steps:

First create a class with setter method :

Here SpringDemo.java class for that:-

package mypkg;
/**
*
* @author pradeep
*/
public class SpringDemo {
private String msg;
/**
* @return the msg
*/
public String getMsg() {
return msg;
}
/**
* @param msg the msg to set
*/
public void setMsg(String msg) {
this.msg = msg;
}

}

After that you have to create another class with main() method where you will test demo.

Here SpringTest.java class for that.

package mypkg;
import org.springframework.beans.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*
* @author pradeep
*/
public class SpringTest {

public static void main(String args[]) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

SpringDemo ad = (SpringDemo) context.getBean("demo");
ad.getMsg();
}
}

After that you will have to create configuration file called beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

 <bean id="demo"
 class="mypkg.SpringDemo">
 <property name="msg" value="hello pradeep yadav"/>
 </bean>
</beans>