SPRING
Simple:
Light weight - just 2.5MB jar file can provide the Spring framework. Overhead is also negligible.
Inversion of Control – Loose coupling, known as Dependency injection (DI). Instead of an object waiting for dependency from container, container provides tem on initialization time.
BeanFactory is core of the Spring’s DI container. A ”bean” is any component that is managed by the container.
Aspect-Oriented – separate application logic from other basic services, i.e. auditing and transaction management can be moved apart from application logic.
Container – contains and manages objects, one can specify the way an object to be created and association among them.
Framework –
Why Spring :
It provides enterprise services to POJO.
Non-invasive- lesser dependency b/w application code and framework.
Pluggability – application objects are considered as named services, thus one can easily replace one service from application without affecting other code.
Easy testing – easy mock can be achieved as spring APIs will normally in the form of interfaces.
Simple example with xml configuration:
Result:
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [SimpleIntCalculator.xml]
intrest for 100 rupies for 12 months is : 168.0
Inversion of Control :
Interface Injection – similar to J2EE, interfaces with associated configuration meta data.
Setter Injection –
Constructor Injection –
Wiring – creating association b/w beans
Prototyping v/s Singleton
BeanFactory always gives an instance of a bean through getBean(). BeanFactory.getBean(); returns same instance all the time. Hence by default all the Spring beans by default are singleton. Whenever they are fetched same instance is available. This is cost effective in terms of resource utilization.
There may be situations where one needs to create multiple instances, this can be achieved by specifying scope attribute to bean.
Container never holds the state of the bean, whenever asked it initializes the bean and supplies.
Switching to annotations…. @...
tag <context:annotation-config /> tells spring that the config is using annotations
tag <context:component-scan base-package=”…” /> tells spring which package to be scanned for the annotations
@Required – value of a bean property must not be null
@Autowired – specifies relation between beans
@Resource – declaration to resource such as datasource
@PostConstruct –
@PreDestroy –
@Component – given to mention the class annotated is a bean
@Controller – class annotated acts as controller
@Repository – class annotated acts as data access object
@Service – classes annotated implements the business logic
@Transactional – transactional configuration
@AspectJ -
Simple:
Light weight - just 2.5MB jar file can provide the Spring framework. Overhead is also negligible.
Inversion of Control – Loose coupling, known as Dependency injection (DI). Instead of an object waiting for dependency from container, container provides tem on initialization time.
BeanFactory is core of the Spring’s DI container. A ”bean” is any component that is managed by the container.
Aspect-Oriented – separate application logic from other basic services, i.e. auditing and transaction management can be moved apart from application logic.
Container – contains and manages objects, one can specify the way an object to be created and association among them.
Framework –
Why Spring :
It provides enterprise services to POJO.
Non-invasive- lesser dependency b/w application code and framework.
Pluggability – application objects are considered as named services, thus one can easily replace one service from application without affecting other code.
Easy testing – easy mock can be achieved as spring APIs will normally in the form of interfaces.
Simple example with xml configuration:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.beans.factory.BeanFactory; | |
import org.springframework.beans.factory.xml.XmlBeanFactory; | |
import org.springframework.core.io.ClassPathResource; | |
import org.springframework.core.io.Resource; | |
public class IntrestCalculatorClient { | |
public static void main(String args[]) throws Exception { | |
Resource res = new ClassPathResource("SimpleIntCalculator.xml"); | |
BeanFactory factory = new XmlBeanFactory(res); | |
SimpleIntCalculator cal= (SimpleIntCalculator) factory.getBean("intCalculator"); | |
double amount = cal.simpleIntrest(100.0, 12); | |
System.out.println("intrest for 100 rupies for 12 months is : "+amount); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface SimpleIntCalculator { | |
public double simpleIntrest(double principle,int months); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:aop="http://www.springframework.org/schema/aop" | |
xsi:schemaLocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> | |
<bean id="intCalculator" class="com.com.test.SimpleIntCalculatorImpl"> | |
<property name="intrestRate" value="14.0" /> | |
</bean> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SimpleIntCalculatorImpl implements SimpleIntCalculator{ | |
private double intrestRate; | |
public double getIntrestRate() { | |
return intrestRate; | |
} | |
public void setIntrestRate(double intrestRate) { | |
this.intrestRate = intrestRate; | |
} | |
public double simpleIntrest(double principle, int months) { | |
return principle*months*intrestRate/100; | |
} | |
} |
Result:
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [SimpleIntCalculator.xml]
intrest for 100 rupies for 12 months is : 168.0
Inversion of Control :
Interface Injection – similar to J2EE, interfaces with associated configuration meta data.
Setter Injection –
Constructor Injection –
Wiring – creating association b/w beans
Prototyping v/s Singleton
BeanFactory always gives an instance of a bean through getBean(). BeanFactory.getBean(); returns same instance all the time. Hence by default all the Spring beans by default are singleton. Whenever they are fetched same instance is available. This is cost effective in terms of resource utilization.
There may be situations where one needs to create multiple instances, this can be achieved by specifying scope attribute to bean.
Container never holds the state of the bean, whenever asked it initializes the bean and supplies.
Switching to annotations…. @...
tag <context:annotation-config /> tells spring that the config is using annotations
tag <context:component-scan base-package=”…” /> tells spring which package to be scanned for the annotations
@Required – value of a bean property must not be null
@Autowired – specifies relation between beans
@Resource – declaration to resource such as datasource
@PostConstruct –
@PreDestroy –
@Component – given to mention the class annotated is a bean
@Controller – class annotated acts as controller
@Repository – class annotated acts as data access object
@Service – classes annotated implements the business logic
@Transactional – transactional configuration
@AspectJ -