Spring Framework Progressive Shallow (1)-Original

Source: Internet
Author: User
Tags define getmessage interface log return log4j
Original
Author: Wang Bin 2005-04-02

The spring framework is a powerful framework for solving many of the problems common in Java EE development, using the spring framework to enable efficient, independent, highly reusable solutions! It is based on powerful JavaBeans configuration management, It makes it easy and quick to organize applications. Your code is no longer flooded with a single instance of garbage, and there is no more trouble with the property file. Replaced by the application of consistent and elegant methods. But the powerful function inevitably brings the complex learning curve, the author through "Springguide" combines own learning experience, step-by-Step guide you into the spring Framework. The IDE in this article is eclipse

1. Download the latest version of Springframework and unzip it to the specified directory. such as E:pring

2. Create a new project in the IDE and add all the jar packages under E:pring\dist\ to the project.

3.Spring uses Apache common_logging and combines Apache log4j as a log output component. In order to observe spring's log output during debugging, you should include both packages in your project, and you should put the new log4j.properties configuration file (log4j.properties) in Classpath. The contents are 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 the action interface: The action interface defines an Execute method, and in our example, different Action implementations provide their own execute methods to accomplish the goal logic.

Action.java


Package Qs;public interface Action {

public string execute (String str);}


5. Implement the action interface, write two classes upperaction, loweraction

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 Spring configuration file (bean.xml)

<beans><description>spring Quick start</description><bean id= "theaction" class= "Qs. Upperaction "><property name=" message "><value>HeLLo:</value></property></bean> </beans>

(Make sure that the configuration Bean.xml is located under the work path, and that the working path is not the same as CLASSPATH, the default work path for Eclipse is the project root path, which is the directory where the. project file resides, and the default output directory/ Bin is part of the project Classpath, not a 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.out.println (Action.execute ("Spring");}}


Run the test code Test.class, we see the console output: ... Hello:spring

We will bean.xml in the configuration slightly modified: <bean id= "Theaction class=" Qs. Loweraction "/> Run the test code again and see: ... hello:spring


Sample COMPLETE!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.