Spring is very useful, which is related to his injection, today, a brief talk about spring's basic injection, the great god please drift over.
1. Add MAVEN Support
<Dependency> <groupId>Org.springframework</groupId> <Artifactid>Spring-context</Artifactid> <version>4.0.2.RELEASE</version> </Dependency>
The basic injection, as long as this one dependency can be, I'm pro-test.
2. Write several test classes
Suppose a scenario is convenient for code logic
package Com.cnblogs.yjmyzz.domain; public abstract class computer { public abstract void Showinfo ();}
Package Com.cnblogs.yjmyzz.domain; Public class extends Computer { @Override publicvoid showinfo () { System.out.println ( "\tapple MAC book");} }
package Com.cnblogs.yjmyzz.domain; public class ThinkPad extends computer {@Override public void Showinfo () {System.out.println ( "\tlenovo ThinkPad"
package Com.cnblogs.yjmyzz.domain; public abstract class Pet { public abstract void Welcomemetohome ();}
Package Com.cnblogs.yjmyzz.domain; Public class extends Pet { @Override publicvoid welcomemetohome () { System.out.println ( "\twang! wang! " ); }}
PackageCom.cnblogs.yjmyzz.domain;Importjava.util.List; Public classProgrammer {PrivateString name; PrivatePet Pet; PrivateList<computer>computers; Public voidsetName (String name) { This. Name =name; } PublicList<computer>getcomputers () {returncomputers; } Public voidSetcomputers (list<computer>computers) { This. Computers =computers; } Public voidSetpet (Pet pet) { This. Pet =Pet; } Public voidShow () {System.out.print ("My name is" +name); System.out.print (", and I have" + computers.size () + "Computer" + (Computers.size () > 1? " S ":" ") +": "); System.out.println (); for(computer c:computers) {c.showinfo (); } System.out.println ("And I have a pet, everyday,when I go home, it'll welcome me by"); Pet.welcomemetohome (); }}
3, spring configuration file, here is the most basic configuration file, the rest of the configuration please add yourself
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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.xsd" > <BeanID= "Jimmy"class= "Com.cnblogs.yjmyzz.domain.Programmer"> < Propertyname= "Name"value= "Jimmy.yang"/> < Propertyname= "Pet"ref= "Wangcai"/> < Propertyname= "Computers"> <List> <refBean= "T60"/> <refBean= "Macbook_pro"/> </List> </ Property> </Bean> <BeanID= "T60"class= "Com.cnblogs.yjmyzz.domain.ThinkPad"/> <BeanID= "Macbook_pro"class= "Com.cnblogs.yjmyzz.domain.MacBook"/> <BeanID= "Wangcai"class= "Com.cnblogs.yjmyzz.domain.Dog"/></Beans>
In this configuration file Simplebeans.xml, there are 4 bean instances configured, which are instantiated by the spring container (by default, singleton mode instantiation), and are kept in the container, when needed, Can be manually obtained by ApplicationContext's Getbean method
4. Test injection
For convenience, you need to write a tool class Springutils
Packagecom.cnblogs.yjmyzz.utils;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classSpringutils {Private StaticApplicationContext ApplicationContext; Private StaticApplicationContext GetContext () {if(ApplicationContext = =NULL) {ApplicationContext=NewClasspathxmlapplicationcontext ("Simplebeans.xml"); } returnApplicationContext; } Public Static<T> T Getbean (class<t>Clazz, String beanname) { returnGetContext (). Getbean (Beanname, clazz); }}
You can then use this tool class to get the injected object instance through Getbean.
Package com.cnblogs.yjmyzz.test; Import Com.cnblogs.yjmyzz.domain.Programmer; Import com.cnblogs.yjmyzz.utils.SpringUtils; Import org.junit.Test; Public class testspring { @Test publicvoid testsimpleinject () { Programmer Jimmy= Springutils.getbean (Programmer. Class, "Jimmy"); Jimmy.show (); }}
The above code from my myeclipse copied out, pro-Test available, please contact me where in doubt.
Spring 4.0-the most basic injection