There are few spring tutorials provided on the Internet, and a little better information contains hundreds of pages, which is a great test for beginners. Therefore, the original intention of this article is to let you start with spring in the simplest way, however, the usage and introduction of spring will not be provided in this article. This article mainly introduces how to make it easier for beginners to use spring as soon as possible. After reading this article, you will be able to learn how to use the simple hello World Program in spring architecture.
I. File Download
The project created in this article requires a minimum package of spring. jar, log4j-1.2.14.jar, commons-logging-1.1.jar. You can download it from the corresponding official website.
Spring Official Website: http://www.springsource.org/
Log4j Official Website: http://logging.apache.org/log4j/
2. Create a project
1. directly go to File> New> Project in Eclipse IDE and name it spring.
2. introduce three racks: we know how to introduce them and how to use eclipse.
3. Create several packages and files for testing, as shown in project structure 1.
Figure 1 Project Structure
Iii. Code Parsing
1. Interface action. Java
Package net. rocket. spring. QS; </P> <p> Public interface action {</P> <p> Public String execute (string Str); <br/>}< br/>
2. Inheritance class: loweraction. Java and upperaction. Java
Package net. rocket. spring. QS; </P> <p> public class loweraction {</P> <p> private string message; </P> <p> Public void setmessage (string message) <br/>{< br/> This. message = message; <br/>}< br/> Public String getmessage () <br/>{< br/> return this. message; <br/>}< br/> Public String execute (string Str) <br/>{< br/> return (getmessage () + Str ). tolowercase (); <br/>}</P> <p >}< br/>
Upperaction. Java
Package net. rocket. spring. QS; </P> <p> public class upperaction implements action {</P> <p> private string message; </P> <p> Public void setmessage (string message) <br/>{< br/> This. message = message; <br/>}< br/> Public String getmessage () <br/>{< br/> return this. message; <br/>}< br/> Public String execute (string Str) <br/>{< br/> return (getmessage () + Str ). touppercase (); <br/>}< br/>
3. Test class: testquickstart. Java
Package test; <br/> Import net. rocket. spring. qs. *; <br/> Import Org. springframework. context. applicationcontext; <br/> Import Org. springframework. context. support. filesystemxmlapplicationcontext; </P> <p> public class testquickstart {<br/> Public static void main (string ARGs []) {</P> <p> applicationcontext CTX = new filesystemxmlapplicationcontext ("bean. XML "); <br/> action Action = (Action) CTX. getbean ("theaction"); <br/> system.out.println(action.exe cute ("Rod Jonson"); <br/>}< br/>
4. Bean. xml: The position is in spring/bean. xml. This is very important; otherwise, an error is reported.
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype beans public "-// spring // DTD bean 2.0 // en" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <br/> <beans> <br/> <description> spring Quick Start </ description> <br/> <bean id = "theaction" class = "net. rocket. spring. qs. upperaction "> <br/> <property name =" message "> <br/> <value> Hello </value> <br/> </property> <br/> </bean> <br/> </beans>
5. In order to observe the log output of spring during debugging, create a log4j. properties file in classpath, which is located under spring/src /.
# For JBoss: Avoid to setup log4j outside $ jboss_home/Server/default/deploy/log4j. xml! <Br/> # For all other servers: Comment out of the log4j listener in web. XML to activate log4j. <br/> log4j. rootlogger = debug, stdout </P> <p> log4j. appender. stdout = org. apache. log4j. leleappender <br/> log4j. appender. stdout. layout = org. apache. log4j. patternlayout <br/> log4j. appender. stdout. layout. conversionpattern = % d % P [% C]-% m % n <br/>
Iv. Test Run
1. Run the class where the main function is located. The output result is displayed on the console.
17:17:30, 390 debug [org. springframework. Beans. Factory. Support. defaultlistablebeanfactory]-returning cached instance of Singleton bean 'theaction'
Hellorod Jonson
V. Exception Handling and summary
During the debugging process, an exception error occurs as follows. We hope you can solve the problem in the same situation:
(1) bean. xml file format error: http://xc403367041.blog.163.com/blog/static/40442622200810325751618/
(2) forget to import the rack package: the three packages mentioned at the beginning of this article cannot be fewer; otherwise, an error will occur.
(3) Spring version: The version tested in this article is spring 2.0. If you are using another version, change the namespace by yourself.
Now, you can create classes and methods with the assistance of spring, and realize the convenience of layering for the first time.