I. Create an Interface
Public interface personservice {<br/> Public void save (); <br/>}
Ii. Create an implementation class
Public class personservicebean implements personservice {<br/> Public void save () {<br/> system. Out. println ("Save method is running !. "); <Br/>}< br/>}
3. Create a test class
Import Org. JUnit. beforeclass; <br/> Import Org. JUnit. test; <br/> Import Org. springframework. context. applicationcontext; <br/> Import Org. springframework. context. support. classpathxmlapplicationcontext; <br/> public class personservicebeantest {<br/> @ beforeclass <br/> Public static void setupbeforeclass () throws exception {</P> <p >}</P> <p> @ test <br/> Public void instancespring () {<br/> applicationcontext context = new classpathxmlapplicationcontext ("beans. XML "); <br/> personservice = (personservice) context. getbean ("personservice"); <br/> personservice. save (); <br/>}< br/>}
4. Beans. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans <br/> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <br/> <bean id = "personservice" class = "com. test. spring. test. personservicebean "> </bean> <br/> </beans>
V. Description
I think the bean in spring is to configure the class to be called to implement the corresponding interface. Before using spring, we create it manually, for example: interfacename INAME = new interfacenameimplement (); after spring is used, we will write this guy in the configuration file.