Creating spring app using Maven

Project Structure


Maven Dependency for spring

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.0.3.RELEASE</version>
</dependency>
 
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.0.3.RELEASE</version>
</dependency>

Employee.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.training.Maven_App_Spring;
 
public class Employee 
{
 
	String emp_name;
 
 
	//getter method
	public String getEmp_name()
	{
		return emp_name;
	}
 
    //setter method
	public void setEmp_name(String emp_name) 
	{
		this.emp_name = emp_name;
	}
 
}

TestMain.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.training.Maven_App_Spring;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class TestMain 
{
	public static void main(String[] args)
	{
		System.out.println("**********************");
		ApplicationContext context=new ClassPathXmlApplicationContext("Spring-config.xml");
	       Employee e=	(Employee)context.getBean("emp");
	       String name= e.getEmp_name();
	       System.out.println("Hello: "+name);
	       System.out.println("***********************");
	}
}

Spring-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
 
<bean id="emp" class="com.training.Maven_App_Spring.Employee">  
 
<property name="emp_name"  value="Bhavesh"/>
 
</bean>
 
</beans>

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>com.training</groupId>
  <artifactId>Maven-App-Spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 
  <name>Maven-App-Spring</name>
  <url>http://maven.apache.org</url>
 
<repositories>
 
<repository>
<id>spring-milestone</id>
<name>Spring Milestone Repository</name>
<url>https://repo.spring.io/milestone</url>
</repository>
 
</repositories>
 
 
  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
      <scope>provided</scope>
    </dependency>
 
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>
 
   <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
   <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-core</artifactId>
    <version>3.0.3.RELEASE</version>
  </dependency>
 
 
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-context</artifactId>
	    <version>5.0.0.M5</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.0.3.RELEASE</version>
</dependency>
 
</dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <source>1.4</source>
          <target>1.4</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *