<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.5.RELEASE</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <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.mycompany</groupId> <artifactId>SpringCore</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <!-- Spring framework --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.0.5.RELEASE</version> </dependency> </dependencies> </project>
HelloWorld.java
(in src/main/java/com/tutorial/springhelloworld)package com.tutorial.springhelloworld; public class HelloWorld { private String name; public void setName(String name) { this.name = name; } public void printHello() { System.out.println("Hello ! " + name); } }
SpringBean.xml
in the resources folder<?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-2.5.xsd"> <bean id="helloBean" class="com.tutorial.springhelloworld.HelloWorld"> <property name="name" value="Catchmycity" /> </bean> </beans>
Application.java
, it will load the Spring bean configuration file (SpringBean.xml) and retrieve the Spring bean via getBean()
method.ApplicationContext
is used to load the Spring bean into the memoryClassPathXmlApplicationContext
will point to the resources folder, the files inside the resources folder can be access using thisFileSystemXmlApplicationContext
to get the pathpackage com.tutorial.springhelloworld; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class Application { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("SpringBean.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloBean"); obj.printHello(); } }
In this example, The HelloWorld object is initialized from the Spring bean, the one-time instantiated object is retrieved using the context.getBean() bean and we can invoke the printHello() method of the HelloWorld class
--- exec-maven-plugin:1.2.1:exec (default-cli) @ SpringHelloWorld --- Dec 10, 2014 12:59:09 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@176fe71: startup date [Wed Dec 10 00:59:09 IST 2014]; root of context hierarchy Dec 10, 2014 12:59:09 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [SpringBean.xml] Hello ! Catchmycity ------------------------------------------------------------------------ BUILD SUCCESS ------------------------------------------------------------------------