Below steps will show how to create Maven project and create a Spring Hello Application :
Step 1
Go to File --> New --> Other
And select Maven Project as in below screen:
Step 2
Check Create a simple Project
Step 3
Here Group Id is package name and Artifact Id is name of the project
Step 4
Below is the Hierarchy of the Maven Project with pom.xml
Step 5
Create a Class
App.java
package com.sks.spring.test;
public class App {
public static void main(String[] args) {
// TODO Auto-generated method stub
Person p = new Person();
p.speak();
}
}
package com.sks.spring.test;
public class Person {
public void speak(){
System.out.println(" I am a person");
}
}
Step 6
Step 7
Double click on pom.xml and open Dependency tab
Step 8
Click on Add and start searching for springframework dependencies and select highlighted basic spring dependencies.You can select as per your requirement. If you wont find required dependencies, please go to https://mvnrepository.com/ and add them in you pom.xml
Thats it !!!
Step 9
Now change App.java as below
package com.sks.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
//
// Person p = new Person();
// p.speak();
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person)context.getBean("person");
person.speak();
}
}
Step 10
Create spring applicationContext.xml in src/main/resources as below:
<?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:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<!-- here we are injecting the bean in spring context -->
<bean id="person" class="com.sks.spring.test.Person"></bean>
</beans>
Step 11
Run App.java and see the below output
No comments:
Post a Comment