Advanced Java Session 3 (Practical of Spring Tight Coupling and Spring Loose Coupling)
1.STC (Spring tight coupling) practical.
Steps to create maven project to demonstrate spring
tight coupling implementation.
Step 1. Create maven project (steps given for
this in previous session).
Step 2. In pom file, Add required maven
dependency and version of spring.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.consyosoft</groupId>
<artifactId>STC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<spring.version>5.3.8</spring.version>
</properties>
</project>
Step 3. Create main class
//access properties of A class in C class to
demonstrate tight coupling
package com.consyosoft;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class Starter {
public
static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
C
c1 = (C) context.getBean("c");
c1.display();
}
}
Step 4. Create class A with print method
package
com.consyosoft;
public class A {
public void
print(){
System.out.println("I
m in A");
}
}
Step 5. Create class C and create object of A
into it, then write display method and in that call print method of A class
using object of A.
package
com.consyosoft;
public class C {
A a = new A();
public void
display(){
a.print();
}
}
Step 6. Create xml file, write context
component scan tag to define base package, configure beans
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.consyosoft" />
<bean id="c" class="com.consyosoft.C"/>
</beans>
Also you can search on google i.e beans tag
for spring 5.3 xml file.
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</beans>
Step 7: In main class, get object of bean
using ApplicationContext, and call method of C.
//access properties of A class in C class to
demonstrate tight coupling
package com.consyosoft;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class Starter {
public
static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
C
c1 = (C) context.getBean("c");
c1.display();
}
}
2.SLC (spring loose coupling) practical
Steps to create maven project to demonstrate
spring loose coupling implementation.
Step 1. Create maven project (steps given for
this in previous session).
Step 2. In pom file, Add required maven
dependency and version of spring.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.consyosoft</groupId>
<artifactId>STC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<spring.version>5.3.8</spring.version>
</properties>
</project>
Step 3. Create file with name Starter.java
(main class)
//access properties of A class in C class to
demonstrate loose coupling
package com.consyosoft;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class Starter {
public
static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
C
c1 = (C) context.getBean("c");
c1.display();
}
}
Step 4. Create class A with print method
package
com.consyosoft;
public class A implements I{
public void
print(){
System.out.println("I
m in A");
}
}
Step 5. Create class B with print method.
package
com.consyosoft;
public class B implements I{
public void
print(){
System.out.println("I
m in B");
}
}
Step 6. Create class C and get object of A
into it, then write display method, and in that call print method of A class
using object of A.
package
com.consyosoft;
public class C {
I i;
public C(I
i){
this.i = i;
}
public void
display(){
i.print();
}
}
Step 7. Create applicationContext.xml file,
write context component scan tag to define base package, configure beans
<?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.consyosoft"/>
<bean id="a" class="com.consyosoft.A"/>
<bean id="b" class="com.consyosoft.B"/>
<bean id="c" class="com.consyosoft.C">
<constructor-arg ref="a" />
</bean>
</beans>
Also you can search on google i.e bean tag
for spring 5.3 xml file.
<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
</bean>
Comments
Post a Comment