A simple example of a preliminary understanding of the spring framework
1, create Java project, import the corresponding spring package, placed in the Lib folder
2, Interface Ihellomessage
1 Package Com.jike.spring.chapter01; 2 3 Public Interface ihellomessage {4 5 // an abstract method in the interface that is used to output greeting messages to everyone 6 Public String SayHello (); 7 8 }
3, Java class Hellochina.java, for the output of "Chinese language"
1 Package Com.jike.spring.chapter01;2 3 Public classHellochina implements Ihellomessage {4 5 6 //inherits and implements the SayHello method in the Ihellomessage interface,7 //I 've been exporting Hello China information to everyone.8 @Override9 PublicString SayHello () {Ten One return "Hello china!"; A } - -}
4, Java class Helloworld.java, for output "World Talk"
1 Package Com.jike.spring.chapter01;2 3 Public classHelloWorld implements ihellomessage{4 5 //inherits and implements the SayHello method in the Ihellomessage interface,6 //I 've been exporting Hello China information to everyone.7 @Override8 PublicString SayHello () {9 return "Hello world!";Ten } One A}
5. Person class
1 Package Com.jike.spring.chapter01;2 3 Public classPerson {4 5 //Use Ihellomessage as a property to output greeting messages to everyone6 Privateihellomessage HelloMessage;7 8 9 Publicihellomessage gethellomessage () {Ten returnHelloMessage; One } A - Public voidsethellomessage (Ihellomessage hellomessage) { - This. HelloMessage =HelloMessage; the } - - //used to invoke the Ihellomessage interface to output greeting messages to the user, - //specific greeting information, allocated and determined by the spring configuration file + //1. When the HelloWorld entity is assigned to person in the configuration file, the output "Hello world! "The information; - //2. When the Hellochina entity is assigned to person in the configuration file, the output "Hello china! "The information; + PublicString SayHello () { A return This. Hellomessage.sayhello (); at } -}
6. Main class
1 Package Com.jike.spring.chapter01;2 3 import org.springframework.beans.factory.BeanFactory;4 import org.springframework.beans.factory.xml.XmlBeanFactory;5 import Org.springframework.core.io.FileSystemResource;6 import Org.springframework.core.io.Resource;7 8 Public classMain {9 Ten Public Static voidMain (string[] args) { One A //reading a configuration file with Filesystemresource -Resource r =NewFilesystemresource ("Hellomessage.xml"); - //use Xmlbeanfactory to load configuration files, launch IOC container theBeanfactory f =NewXmlbeanfactory (r); - //get an instance of the person class from the IOC container -Person person = (person) F.getbean (" Person"); - //Person instance output greeting message to everyone +String s =Person.sayhello (); - //The greeting information is printed in the system console, as the configuration file in this configuration is an instance of HelloWorld. + //So, here's the string: HelloWorld ASystem. out. println ("The person is currently saying:"+s); at } - -}
7, Configuration Hellomessage.xml
1<?xml version="1.0"encoding="UTF-8"?>2<! DOCTYPE Beans Public"-//SPRING/DTD bean/en" 3 "HTTP://WWW.SPRINGFRAMEWORK.ORG/DTD/SPRING-BEANS.DTD">4<beans>5<bean id="HelloWorld" class="Com.jike.spring.chapter01.HelloWorld"></bean>6<bean id="Hellochina" class="Com.jike.spring.chapter01.HelloChina"></bean>7<bean id=" Person" class="Com.jike.spring.chapter01.Person">8<property name="HelloMessage" ref="Hellochina"/>9</bean>Ten</beans>
Operation Result:
Change the configuration file to:
<?xml version="1.0"encoding="UTF-8"? ><! DOCTYPE Beans Public"-//SPRING/DTD bean/en" "HTTP://WWW.SPRINGFRAMEWORK.ORG/DTD/SPRING-BEANS.DTD"><beans> <bean id="HelloWorld" class="Com.jike.spring.chapter01.HelloWorld"></bean> <bean id="Hellochina" class="Com.jike.spring.chapter01.HelloChina"></bean> <bean id=" Person" class="Com.jike.spring.chapter01.Person"> <property name="HelloMessage" ref="HelloWorld"/> </bean></beans>
The result is:
Spring Small Sample