Import the package First:
Commons-logging-1.1.1.jar:spring-dependent packages;
Spring-beans-4.0.0.release.jar;
Spring-context-4.0.0.release.jar;
Spring-core-4.0.0.release.jar;
Spring-expression-4.0.0.release.jar;
1 PackageCom.model;2 3 Public classHelloWorld {4 5 PrivateString username;6 7 Public voidSetusername (String username) {8 This. Username =username;9 }Ten One Public voidHello () { ASystem.out.println ("Hello:" +username); - } - the}
View Code
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >5 6 <BeanID= "Hello"class= "Com.model.HelloWorld">7 < Propertyname= "username"value= "Spring4"></ Property>8 </Bean>9 Ten One </Beans>
1 PackageCom.model;2 3 ImportOrg.springframework.context.ApplicationContext;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6 Public classMain {7 8 Public Static voidMain (string[] args) {9 //get the IOC container for springTenApplicationContext AC =NewClasspathxmlapplicationcontext ("Hello.xml"); One A //get Bean;hello from a container the ID of the bean in the XML file -HelloWorld HelloWorld = (HelloWorld) ac.getbean ("Hello"); - System.out.println (helloWorld); the //Calling Methods - Helloworld.hello (); - } - +}
Printing results:
December 06, 2014 9:51:47 am org.springframework.context.support.AbstractApplicationContext Preparerefresh
info:refreshing org[email protected]1642bd6:startup Date [Sat Dec 09:51:47 CST 2014]; Root of context Hierarchy
December 06, 2014 9:51:47 am org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadbeandefinitions
Info:loading XML Bean definitions from class path resource [Hello.xml]
[Email protected]
Hello:spring4
Small trial Sledgehammer Spring's HelloWorld