Spring Core Framework by Telusko
Spring Core Framework by Telusko
Spring Core Framework by Telusko
Tutorial
Application Context
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.8.RELEASE</version>
</dependency>
Vehicle.java
package com.hussain.Maven_03_SpringCoreFramwork;
public interface Vehicle {
void drive();
}
Car.java
package com.hussain.Maven_03_SpringCoreFramwork;
public class Car implements Vehicle{
public void drive() {
System.out.println("Car chal raha hai");
}
}
Bike.java
package com.hussain.Maven_03_SpringCoreFramwork;
public class Bike implements Vehicle{
public void drive() {
System.out.println("Bike bhaag raha hai.");
}
}
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- since xml always goes for user defined tags(custom tags)
and these custom tags need a definition. so, beans is to be defined as
follows -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
Note: beans configuration can be taken with the help of Google. Here we are
using the spring 5 xml configuration
AppTest.java(Main file)
package com.hussain.Maven_03_SpringCoreFramwork;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/*
* Now we are done.
* We are not creating any object of Car but Spring has provided us
the object of car.
*
* if we change below
* <bean id="vehicle"
class="com.hussain.Maven_03_SpringCoreFramwork.Car"/>
* to
* <bean id="vehicle"
class="com.hussain.Maven_03_SpringCoreFramwork.Bike"/>
* in spring.xml then it will give the object of Bike
*/
/*
* below is just a simple test that while execution if we change Car
to Bike or vice versa in spring.xml
* we don't need to recompile it. It will be reflected itself. So,
no need to change the source code of java here.
* So, testing becomes easy and cheap.
*/
int i=0;
while(i++<20){
context=new ClassPathXmlApplicationContext("spring.xml");
obj=(Vehicle) context.getBean("vehicle");
obj.drive();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
Annotation Based Configuration
Vehicle.java
package com.hussain.maven_04_annotation;
public interface Vehicle {
void drive();
}
Car.java
package com.hussain.maven_04_annotation;
import org.springframework.stereotype.Component;
@Component
public class Car implements Vehicle{
public void drive(){
System.out.println("Chal Raha Hai.");
}
}
Bike.java
package com.hussain.maven_04_annotation;
import org.springframework.stereotype.Component;
@Component
public class Bike implements Vehicle{
public void drive(){
System.out.println("Bhaag Raha Hai.");
}
}
spring.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-
package="com.hussain.maven_04_annotation"></context:component-scan>
</beans>
App.java
package com.hussain.maven_04_annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
Tyre.java
package com.hussain.maven_05_beanproperty;
public class Tyre {
private String brand;
spring.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
App.java
package com.hussain.maven_05_beanproperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
Vehicle.java
package com.hussain.maven_06_autowiredannotation;
public interface Vehicle {
void drive();
}
Tyre.java
package com.hussain.maven_06_autowiredannotation;
public class Tyre {
private String brand;
public Tyre(){ }
public Tyre(String brand) {
this.brand = brand;
}
//Setters and Getters
public String toString(){
return "Tyre[brand="+brand+"]";
}
}
Car.java
package com.hussain.maven_06_autowiredannotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Car implements Vehicle{
@Autowired
private Tyre tyre;
spring.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Below tag(Annotation based configuration) is used for Class Car -->
<context:component-scan base-package =
"com.hussain.maven_06_autowiredannotation"></context:component-scan>
<!-- below bean tag indicates xml based configuration for Tyre.
@Component annotation is to be written before Tyre then below bean
tag is not required. above context tag will suffice
-->
<bean id="tyre" class="com.hussain.maven_06_autowiredannotation.Tyre">
<property name="brand"><value>CEAT</value></property>
</bean>
</beans>
App.java
package com.hussain.maven_06_autowiredannotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
Note: In this, no xml based configuration is required. Annotation based was introduce in version 2.5
Samsung.java
package com.hussain.maven_07_configurationbean;
import org.springframework.stereotype.Component;
}
AppConfig.java
package com.hussain.maven_07_configurationbean;
import org.springframework.context.annotation.*;
@Configuration
public class AppConfig {
@Bean
public Samsung getPhone(){
return new Samsung();
}
}
App.java
package com.hussain.maven_07_configurationbean;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
MobileProcessor.java
package com.hussain.maven_08_configurationbean02;
Snapdragon.java
package com.hussain.maven_08_configurationbean02;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Autowired
MobileProcessor cpu;
AppConfig.java
package com.hussain.maven_08_configurationbean02;
import org.springframework.context.annotation.*;
@Configuration
public class AppConfig {
@Bean
public Samsung getPhone(){
return new Samsung();
}
@Bean
public MobileProcessor getProcessor(){
return new Snapdragon();
}
}
App.java
package com.hussain.maven_08_configurationbean02;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
In the last two examples, we were defined functions to generate objects in AppConfig.java.
Now, we don’t want to write them. Then, what we need to do is write @Component before Samsung
and Sanpdragon and add one more annotation
@ComponentScan(basePackages="com.hussain.maven_09_configurationbean03"). This
is actually an alternate way of the following configuration in spring.xml
<context:component-scan base-
package="com.hussain.maven_06_autowiredannotation"></context:component-scan>
MobileProcessor.java
package com.hussain.maven_09_configurationbean03;
public interface MobileProcessor {
void process();
}
Snapdragon.java
package com.hussain.maven_09_configurationbean03;
import org.springframework.stereotype.Component;
@Component
public class Snapdragon implements MobileProcessor {
Samsung.java
package com.hussain.maven_09_configurationbean03;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Autowired
MobileProcessor cpu;
}
AppConfig.java
package com.hussain.maven_09_configurationbean03;
import org.springframework.context.annotation.*;
@Configuration
@ComponentScan(basePackages="com.hussain.maven_09_configurationbean03")
public class AppConfig {
App.java
package com.hussain.maven_09_configurationbean03;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
In the last example, if we take one more class MediaTec.java(with @Component) implementing
MobileProcessor.java. So, we have now two classes Snapdragon and MediaTek implementing
MobileProcessor and Samsung needs MobileProcessor reference. So, in this case the compiler will get
confused which instance is to given to Samsung as MobileProcessor reference as there are two beans
Snapdragon and MediaTek that are giving MobileProcessor.
In order to avoid this, we can use @Primary before any of the beans Snapdragon and MediaTek or
@Qualifier("snapdragon") can be used just before the reference data member. If both are used
then @Qualifier("snapdragon") will be considered.
Below is the project structure:
MobileProcessor.java
package com.hussain.maven_09_configurationbean03;
Snapdragon.java
package com.hussain.maven_09_configurationbean03;
import org.springframework.stereotype.Component;
@Component
public class Snapdragon implements MobileProcessor {
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
@Primary
public class MediaTek implements MobileProcessor {
Samsung.java
package com.hussain.maven_09_configurationbean03;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Autowired
@Qualifier("snapdragon")
MobileProcessor cpu;
}
AppConfig.java
package com.hussain.maven_09_configurationbean03;
import org.springframework.context.annotation.*;
@Configuration
@ComponentScan(basePackages="com.hussain.maven_09_configurationbean03")
public class AppConfig {
App.java
package com.hussain.maven_09_configurationbean03;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;