Bean
public class HelloWorld {private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
spring-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" xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="messanger" class="com.shambhu.bean.HelloWorld">
<property name="msg" value="Hello World" />
</bean>
</beans>
Testing
public class HelloMain {
public static void main(String[] args) {
ApplicationContext beanFactory = new ClassPathXmlApplicationContext("spring-beans.xml");
HelloWorld messanger= (HelloWorld) beanFactory.getBean("messanger");
System.out.println(messanger.getMsg());
}
}
pom.xml
<properties>
<spring.version>4.0.3.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
Jun 06, 2014 10:40:38 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4f114133: startup date [Fri Jun 06 22:40:38 IST 2014]; root of context hierarchyJun 06, 2014 10:40:38 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring-beans.xml]
Hello World
0 comments: