Spring framework sequential (1) - original

xiaoxiao2021-03-05  19

Author: Wang Bin 2005-04-02

Spring Framework is a powerful framework that solves a common problem in J2EE development, using Spring Framework to achieve efficient independent highly reused solutions! It is based on powerful JavaBeans-based configuration management, which makes organizations Applications become easy and rapid. There is no longer full of single-case garbage in your code, nor will there be a troublesome attribute file. Instead, the application of consistent and elegant methods. But strong features must bring complex learning curves, the author combines its own learning experience through "SpringGuide", step by step to guide you into Spring Framework. The IDE in this article is Eclipse

1. Download the latest version of SpringFramework and decompressed to the specified directory. E: / SPRING

2. Create a project in IDE and join all JAR packets in E: / Spring / Dist /.

3.Spring uses Apache Common_Logging and combines Apache log4j as a log output component. In order to observe the Spring log output during the debugging process, these two packages should be added in the project, and the new log4j.properties configuration file in ClassPath should be built, the content is as follows:

log4j.rootLogger = DEBUG, stdoutlog4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout = org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern =% c {1} -% m % N

4. Define an action interface: Action interface defines an Execute method, in our example, different Action implementation provides respective Execute methods to complete the target logic.

Action.java

Package QS; public interface action {public string execute (string str);}

5. Implement an action interface, write two types of Upperaction, LowerAction, respectively

Upperaction.java

package qs; public class UpperAction implements Action {private String message; public String getMessage () {return message;} public void setMessage (String string) {message = string;} public String execute (String str) {return (getMessage () Str) .touppercase ();}}

LowerAction.java

package qs; public class LowerAction implements Action {private String message; public String getMessage () {return message;} public void setMessage (String string) {message = string;} public String execute (String str) {return (getMessage () Str) .tolowercase ();}}

5. Define the Spring profile (bean.xml)

Spring Quick Start hello: < / bean> (Make sure the configuration bean.xml is under the working path, pay attention to the working path is not equivalent to the ClassPath, the default work path of Eclipse is the project root path, which is the directory where the .project file is located, and the default The output directory / bin is part of the project classpath, not working path.)

6. Test code, write Test.java

package qs; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; public class Test {public static void main (String [] args) {ApplicationContext ctx = new FileSystemXmlApplicationContext ( "bean.xml") Action action = (action) ctx.getbean ("theAction"); system.ut.println (action.execute ("spring");}}

Run the test code TEST.CLASS, we see the console output: ... Hello: Spring

We will modify the configuration in bean.xml: Run the test code again, see: ... hello: Spring

Example is completed!

转载请注明原文地址:https://www.9cbs.com/read-39356.html

New Post(0)