Spring core Project - Smart Tech Guides

Latest

Hi this is Sravan Kumar from India. SMART TECH GUIDES is a technical blog. it helps you to learn about Java topics, frameworks and tools like Jenkins, GitHub & related explanations, data base related topics. All these Explanations are explained in simple and understandable manner.

Subscribe Us

SMART TECH GUIDES

Spring core Project

Spring Project


creating spring project using maven build tool. 


steps involved in spring project(core) development 

1) create new Maven project 

2) add spring context jar inside pom.xml 

3) create beans configured Java/xml file 

4) write Java code inside src/main/java Java code

Java code

 Card Interface
package com.smarttechguides.cards;
public interface Cards {
void deposite(double amount);
}
view raw cards.java hosted with ❤ by GitHub
KotakBank implements Card Interface
package com.smarttechguides.cards;
public class KotakBankCard implements Cards {
public void deposite(double amount) {
System.out.println("amount debited from Kotak Bank " + amount);
}
}
AxisBank implements Card Interface
package com.smarttechguides.cards;
public class AxisBankCard implements Cards {
public void deposite(double amount) {
System.out.println("amount debited from Axis Bank " + amount);
}
}
Injecting Card by Perform Setter Injection
package com.smarttechguides.cards.process;
import com.smarttechguides.cards.Cards;
public class PaymentsProcess {
private Cards card;
// setter injection
public void setCard(Cards card) {
this.card = card;
}
public void doPayment(double amount) {
card.deposite(amount);
}
}
Application Start Point
package com.smarttechguides;
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;
import com.smarttechguides.cards.process.PaymentsProcess;
public class Application {
public static void main(String[] args) {
// loading xml file from classpath
Resource resource = new ClassPathResource("Beans.xml");
// IOC container started with lazy loading mechanisum
BeanFactory fatory = new XmlBeanFactory(resource);
// bean object created by calling getbean method
PaymentsProcess bean = fatory.getBean("pay", PaymentsProcess.class);
bean.doPayment(100);
}
}
 


Project Folder Structure

Spring folder Structure



 pom.xml

<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.smarttechguides</groupId>
<artifactId>BankApplication</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.13</version>
</dependency>
</dependencies>
</project>
view raw pom.xml hosted with ❤ by GitHub
 


 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="kotak" class="com.smarttechguides.cards.KotakBankCard" />
<bean id="axis" class="com.smarttechguides.cards.AxisBankCard" />
<bean id="pay"
class="com.smarttechguides.cards.process.PaymentsProcess">
<property name="card" ref="axis"></property>
</bean>
</beans>
view raw Beans.xml hosted with ❤ by GitHub

Note: spring beans we can configure in 3 ways

1) xml based configuration file

 Configure <beans> inside beans.xml file

 

2) annotation-based configuration

Configure <context:annotation-config/> inside beans.xml file and use annotations inside Java class. 


3) Java based configuration

Configurations happen in Java file without xml file.

with the help of annotations like @configuration, @component, @service, @controller.. etc.! 


No comments:

Post a Comment