Today, with the initial usage of spring, it feels no different from instantiating an entity class, like this simple code is not as straightforward as being instantiated directly, so using spring would require three files altogether.
The first is of course an entity class, define the attributes, getter and setter methods, and don't forget to construct an argument-free construction method.
Public classHelloWorld {PrivateString Mingzi; PrivateString Dizhi; Privateuser User; Public voidSayHello () {System.out.println ("Hello" +mingzi+user); } //set and Get methods for property Mingzi Public voidSetmingzi (String mingzi) {System.out.println ("Set Mingzi =" +Mingzi); This. Mingzi =Mingzi; } PublicString Getmingzi () {returnMingzi; } //define a property again PublicString Getdizhi () {returnDizhi; } Public voidSetdizhi (String Dizhi) {System.out.println ("Set Dizhi =" +Dizhi); This. Dizhi =Dizhi; } //define the get and set methods of the property user PublicUser GetUser () {returnuser; } Public voidsetUser (user user) { This. user =user; }}
Another is the entry of the Main method to invoke the written entity class.
ImportOrg.springframework.context.ApplicationContext;//reference to line 1thImportOrg.springframework.context.support.ClassPathXmlApplicationContext;//reference to line 1th Public classMain { Public Static voidMain (string[] args) {//call a method by way of spring//The set method is called when the container building container is builtApplicationContext AC =NewClasspathxmlapplicationcontext ("Applicationcontext.xml");//Line 1th//get an instance of JavaBean from a container control invert IOCHelloWorld hw1 = (HelloWorld) ac.getbean ("HW"); Hw1.sayhello (); }}
The remaining one is also the most important file, the XML configuration file
<?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" > <!--put a definition of a class on this side - <BeanID= "HW"class= "Com.hanqi.HelloWorld"> <!--the Dependency injection (DI) implemented here - < Propertyname= "Dizhi"value= "Zibo, Shandong"></ Property> < Propertyname= "Mingzi"value= "Xiao Qiang"></ Property> < Propertyname= "User"ref= "User1"></ Property> </Bean> <BeanID= "User1"class= "Com.hanqi.User"> <!--constructor Injection, if the data type is unique, the order can be irrelevant - <!--You can add a type or index= "ordinal" to match the sequence number - <Constructor-argvalue= " a"Index= "2"></Constructor-arg> <Constructor-argvalue= "Han"Index= "3"></Constructor-arg> <Constructor-argvalue= "Xiao Ming"Index= "0"></Constructor-arg> <Constructor-argvalue= "Male"Index= "1"></Constructor-arg> </Bean></Beans>
Define a user class
Public classUser {PrivateString age; PrivateString sex; PrivateString Minz; PrivateString name; PublicUser () {} PublicUser (string name, String sex, String age, String Minz) {Super(); This. Name =name; This. Age =Age ; This. Sex =sex; This. Minz =Minz; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString getage () {returnAge ; } Public voidsetage (String age) { This. Age =Age ; } PublicString Getminz () {returnMinz; } Public voidSetminz (String Minz) { This. Minz =Minz; } PublicString Getsex () {returnsex; } Public voidsetsex (String sex) { This. Sex =sex; } @Override PublicString toString () {return"User [name=" + name + ", age=" + Age + ", sex=" + Sex + ", minz=" + Minz + "]"; }}
Here are the main points to note:
One, the <property> property label settings, Name= "" value, must be the method name after the set method, a letter can not be wrong, but does not include the case, that is, the method name can have capital letters, to the Name= "" property, it does not matter, As long as you write the letters right, OK.
Second, when using the constructor for attribute injection
<Constructor-argvalue= " a"Index= "2"></Constructor-arg> <Constructor-argvalue= "Han"Index= "3"></Constructor-arg> <Constructor-argvalue= "Xiao Ming"Index= "0"></Constructor-arg> <Constructor-argvalue= "Male"Index= "1"></Constructor-arg>
Of course, you can define a index= "" as above, but note that the sequence number starts at 0 and the first property is numbered 0.
If there is no index, the constructor can recognize the assignment of each property itself, but it is maintained in the same order as the constructor method, that is, the constructor with the parameter
Public User (string name, String sex, String age, String Minz) { Super(); this. Name = name; this. Age = Age ; this. Sex = sex; this. Minz = Minz; }
Here the order of the parameters of this constructor is name, sex, age, Minz (I have set the string type for the sake of testing), so the constructor order should also be written like this
<Constructor-argvalue= "Xiao Ming"></Constructor-arg>→string name<Constructor-argvalue= "Male"></Constructor-arg>→string Sex<Constructor-argvalue= " a"></Constructor-arg>→string Age<Constructor-argvalue= "Han"></Constructor-arg>→string Minz
Spring simply obtains an instance of the entity class, with several considerations for using the ApplicationContext () method