3.3 change the helloworld instance to the constructor injection mode for implementation
Spring supports setter injection and constructor injection. However, we recommend that you use set injection. As mentioned above, the first instance implementing helloworld in Chapter 2nd is implemented using the set injection method. For details, refer to the example in Chapter 2nd. Next, I will change this instance to a constructor injection method. The rewrite idea is: first modify the class helloworld, add a constructor to the class, then modify the spring configuration file config. XML, and finally write the test program testhelloworld. java.
3.3.1 modify helloworld. Java
Modify COM. GC. helloworld under the action package. java, add a constructor and use the string MSG to be injected as the parameter. The Code is as follows. Add a constructor public helloworld (string MSG) to the helloworld class ).
// ******** Helloworld. Java **************
Package com. gc. Action;
Public class helloworld {
// This variable is used to store strings
Public String MSG = NULL;
// Added a constructor.
Public helloworld (string MSG ){
This. MSG = MSG;
}
// Set the MSG variable's Set Method
Public void setmsg (string MSG ){
This. MSG = MSG;
}
// Get the MSG variable's get Method
Public String getmsg (){
Return this. MSG;
}
}
3.3.2 modify config. xml
In spring, when set injection and construction injection are used, the syntax used in the XML configuration document is different. The configuration file config. XML is modified as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en"
Http://www.springframework.org/dtd/spring-beans.dtd>
<Beans>
<! -- Define a bean and inject it through the constructor -->
<Bean id = "helloworld" class = "com. gc. Action. helloworld">
<! -- Inject through constructor -->
<Constructor-Arg Index = "0">
<Value> helloworld </value>
</Constructor-Arg>
</Bean>
</Beans>
Code Description:
● Constructor-Arg, used to indicate that parameters are injected through constructor construction.
● Index = "0" indicates the first parameter in the constructor. If there is only one parameter, you do not need to set this attribute value.
3.3.3 compile the test program testhelloworld. Java
Modify testhelloworld. Java with the following code:
// ******** Testhelloworld. Java **************
Package com. gc. test;
Import org. springframework. Context. applicationcontext;
Import org. springframework. Context. Support. filesystemxmlapplicationcontext;
Import com. gc. Action. helloworld;
Public class testhelloworld {
Public static void main (string [] ARGs ){
// Obtain the spring configuration file through applicationcontext
Applicationcontext actx = new filesystemxmlapplicationcontext ("config. xml ");
Helloworld = (helloworld) actx. getbean ("helloworld ");
System. Out. println (helloworld. getmsg ());
}
}
3.3.4 run the test program and view the output result
To run a Java program in eclipse, follow these steps:
(1) Make sure that the testhelloworld. Java file is edited in eclipse.
(2) Select Run → run as → Java application command, and eclipse will run testhelloworld. java.
(3) the output result is "helloworld", as shown in Figure 3.1.
Figure 3.1 output result: "helloworld"