groupId : com.example
artifactId : SpringHello
packaging : war
Edit the pom.xml file and add Spring dependencies.This is my pom.xml file.
<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>prageeth.example</groupId>
<artifactId>SpringHello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<springRealeaseVersion>4.1.3.RELEASE</springRealeaseVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springRealeaseVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springRealeaseVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springRealeaseVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springRealeaseVersion}</version>
</dependency>
</dependencies>
</project>
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>prageeth.example</groupId>
<artifactId>SpringHello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<springRealeaseVersion>4.1.3.RELEASE</springRealeaseVersion>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springRealeaseVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springRealeaseVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springRealeaseVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springRealeaseVersion}</version>
</dependency>
</dependencies>
</project>
Create a new Java class "SayHello" in com.example package. This is my SayHello class. This is the class which I am going to convert a Spring bean.
package com.example;
public class SayHello {
public String getHelloMessage(String username) {
return "Hello " + username;
}
}
public class SayHello {
public String getHelloMessage(String username) {
return "Hello " + username;
}
}
Now I want to make this class a Spring bean. To do that I need to have a beans xml file.
Create a new folder in src/main/webapp folder and name it as WEB-INF. Create new xml file in this newly created WEB-INF folder. I name the file as context.xml.
You can use any name for it. This is your beans xml file in which you are going to define your Spring beans.
I define my com.example.SayHello as a Spring bean in this file. This is my context.xml file.
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="helloBean" class="com.example.SayHello"/>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="helloBean" class="com.example.SayHello"/>
</beans>
Now what you need to do is tell Spring what your beans xml file is. You can do it in web.xml file. This is my web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<description></description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.example.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<description></description>
<display-name>TestServlet</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.example.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>
That is all. Now my bean is configured and I can use it in my application. For an example assume that I want to invoke a method of helloBean in a servlet.
I can do it like this.
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
SayHello helloBean =(SayHello)ContextLoader.getCurrentWebApplicationContext().getBean("helloBean");
String message = helloBean.getHelloMessage("John");
System.out.println(message);
}
SayHello helloBean =(SayHello)ContextLoader.getCurrentWebApplicationContext().getBean("helloBean");
String message = helloBean.getHelloMessage("John");
System.out.println(message);
}
Note 1:-
If your Spring application is not a web application, you cannot the configure contextConfigLocation and ContextLoaderListener in a web xml.
In this case you can directly load the context xml by code as below.
First create the context xml file in the src folder. Then load the file and invoke the method like this.
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
SayHello helloBean = (SayHello) context.getBean("helloBean");
String message = helloBean.getHelloMessage("John");
System.out.println(message);
}
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
SayHello helloBean = (SayHello) context.getBean("helloBean");
String message = helloBean.getHelloMessage("John");
System.out.println(message);
}
Note 2:-
You have to design your project to prevent loading the context xml file multiple times. You can easily do this by implementing the singleton design patten.
No comments:
Post a Comment