I,
DefinitionGreetingserviceInterface
PackageCN. csdn. hr. Service;
PublicInterfaceGreetingservice {
PublicVoidSaygeeting ();
}
II,
DefinitionGreetingserviceimplClass and implementGreetingserviceInterface
PackageCN. csdn. hr. Service;
PublicClassGeetingserviceImplImplements
Greetingservice {
PrivateString
Greeting;
/* BeanConfiguration FilePropertyAttributeName
AndGreetingConsistent, automatically passedSetMethod Injection*/
//Dependency Injection
PublicVoidSetgreeting (string greeting ){
This.Greeting= Greeting;
}
PublicVoidSaygeeting (){
System.Out. Println (Greeting);
}
}
III,
DefinitionApplicationcontext. 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"
Xsi: schemalocation=Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<Bean
ID="Greetingservicebean"
Class="Cn. csdn. hr. Service. geetingserviceImpl"
Scope="Singleton">
<! --
PropertybeanAttribute Value in-->
<Property
Name="Greeting">
<Value>Hi!!</Value>
</Property>
</Bean>
</Beans>
IV,
Test
Package CN. csdn. hr. JUnit;
Import org. JUnit. test;
Import org. springframework. Beans. Factory. beanfactory;
Import org. springframework. Beans. Factory. xml. xmlbeanfactory;
Import org. springframework. Context. applicationcontext;
Import org. springframework. Context. Support. classpathxmlapplicationcontext;
Import org. springframework. Context. Support. filesystemxmlapplicationcontext;
Import org. springframework. Core. Io. classpathresource;
Import org. springframework. Core. Io. filesystemresource;
Import org. springframework. Core. Io. resource;
Import CN. csdn. hr. Service. geetingservicebean;
Import CN. csdn. hr. Service. greetingservice;
Public class appmain {
@ Test
Public void test (){
//AssemblyBean
Applicationcontext context = new filesystemxmlapplicationcontext ("D: \ Users \ Administrator \ workspace \ day \ SRC \ applicationcontext. xml ");
//CreateBeanfacoryFactory
//ObtainBeanfactoryCreated by the factoryBeanObject
Greetingservice = (greetingservice) Context. getbean ("greetingservicebean"); // XMLResource fileClassbean
//UseBeanInstance
Greetingservice. saygeeting ();
}
@ Test
Public void test1 (){
//AssemblyBean
//Create a dependent resource file
Applicationcontext = new classpathxmlapplicationcontext ("applicationcontext. xml ");
//ObtainBeanfactoryCreated by the factoryBeanObject
Greetingservice = (greetingservice) applicationcontext. getbean ("greetingservicebean"); // XMLResource fileClassbeanOfIDName
//UseBeanInstance
Greetingservice. saygeeting ();
}
}