First, Spring package structure diagram:
Second, FIRST STEP
Ethereal example: (using WSAD package structure) Java Souce
COM
...... .. Test
......... Domain
Action.java
LowerAction.java
Upperaction.java
......... service
TestquickStart.java
Web Content
WEB-INF
Classes
Bean.xml
Log4j.properties
Before you start, you need to Copy the necessary bags, including Spring.jar, Log4j.jar, commons-logging.jar to see the specific code:
Action.java (is an interface)
Package com.sscm.test.domain;
Public interface action {
Public String Execute (String STR);
}
Loweraction.java (implementation class)
Package com.sscm.test.domain;
Public class lowection implements action {
PRIVATE STRING MESSAGE
Public string getMessage () {
Return Message;
}
Public void setmessage (string message) {
THIS.MESSAGE = Message;
}
Public String Execute (String Str) {
Return (GetMessage () STR) .tolowercase ();
}
}
Upperaction.java
Package com.sscm.test.domain;
Public class uppection implements action {
PRIVATE STRING MESSAGE
Public string getMessage () {
Return Message;
}
Public void setmessage (string message) {
THIS.MESSAGE = Message;
}
Public String Execute (String Str) {
Return (GetMessage () STR) .touppercase ();
}
}
Let's look at Spring profile (bean.xml)
XML Version = "1.0" encoding = "UTF-8"?>
Class = "com.sscm.test.domain.upperaction"> Property> bean> beans> Implementation code: TestquickStart.java Package com.sscm.test.service; Import com.sscm.test.domain. *; import org.springframework.context.support.classpathXMLApplicationContext; Public Class TestquickStart { Public static void testquickstart1 () { Try { ClassPathxmlapplicationContext CTX = New classpathXMLApplicationContext ("/ bean.xml"); Action action = (action) CTX.getBean ("THEAAACTION"); //System.out.println ("aaa "); System.out.println (Action.execute ("Rod Johnson")); } catch (exception e) { } } Public static void main (String [] args) { TestquickStart1 (); } } Run the test code, we see the console output: ...... Hello Rod Johnson The example is completed. Carefully observe the above code, you can see: 1. In all of our program code (except for test code), there is no component in spring. 2. Upperaction and LowerAction's Message properties are moved by Spring by reading profiles (bean.xml) State setting. 3. Customer code (here is our test code) only for interface programming, without having to know the specific name of the class. at the same time, We can simply switch the specific underlying implementation class by modifying the configuration file. What is mentioned above, what is helpful for our actual development? Ø First, our components do not need to implement the interface specified by the framework, so you can easily get the components from Spring, even No modification is required (this is unimaginable in applications based on EJB framework). Ø Secondly, the dependence between components is reduced, which greatly improves the reusability of code. Using Spring greatly reduces the coupling between components, Implementation of the true meaning of the components. This is also one of the most valuable features of Spring. Ø Programming interface. It is true that even if there is no Spring, it is not difficult to implement an interface-oriented design. Spring is the meaning of interface design, It provides a more natural platform for its interface programming. Based on Spring Development, the programmer will naturally tend to Use the interface to define the relationship between different levels, this spontaneous tendency, from Spring provides simple feelings provided by Spring Suitable dependent injection implementation. Spring makes the definition of the interface and use it no longer cumbersome (traditional encoding) during the traditional encoding process In the process, introducing an interface, often means that it is necessary to introduce a Factory class, perhaps there is an additional configuration Parts and their read and write code). Third, Spring basic semantics 1. Control the reverse rotation (IOC = Inversion Of Control) is the relationship between the container control program, not the traditional implementation, directly controlled by the program code. This is the concept of the so-called "control reversal": control is transferred from the application code to the external container, the transfer of control, is a so-called reverse. 2. Dependent the injection (Di = Dependency Injection), that is, the dependency between components is determined by the container in the runtime, that is, the container dynamics into the component in the component. For the understanding of the abstract concept above, you can give a simple example: Use a U disk or a mobile hard disk from Laptop COPY data. The laptop is connected to the peripheral storage device by a pre-specified interface (USB), and for the notebook, only the data specified by the user is sent to the USB interface, and the data is determined by the currently accessible USB device. Before the USB device is loaded, the notebook cannot expect the user to access what device on the USB interface. After the USB device is accessed, the dependency between such devices has begun to form. Corresponding to the above description of the dependency injection mechanism, at runtime (system boot, USB device load) is contained by the container (running in notebook) Windows operating system) Injects dependencies (Notebook dependent USB devices) into the component (Windows File Access Components). This is a version that relies on the injection mode in the real world. Relying on the injection mechanism reduces the dependencies between components, and it also greatly improves the portability of components, which means that components are reused. The opportunity will be more. 3. Spring bean packaging mechanism The core of Spring is a DI container. Provides a non-incoming high expansion frame. . That is, it is not necessary to include Spring proprietary classes, you can include Spring containers for management. Spring introduces Java's Reflection mechanism, avoiding hard-coding constraints by dynamic calling. Its core component is BeanFactory, which is based on the implementation of dependency injection. The Org.SpringFramework.beans package includes the implementation class of these core components, the core in the core is BeanWrapper and BeanFactory classes. With BeanWrapper, we can specify the JavaBean implementation class and attribute values when encoding, through the configuration file Setting, you can create an object during operation and set its properties (dependencies). Bean Factory is responsible for creating a bean instance based on the configuration file, the items that can be configured are: 1. Bean attribute value and dependencies (references to other beans) 2. Bean creates a mode (whether Singleton mode, that is, if you only maintain a global unique instance for the specified class) 3. Bean initialization and destruction method 4. BEAN's dependency Such as bean.xml. BeanWrapper implements the property settings for a single bean Type operation. BEANFACTORY is a management container for multiple beans, read according to a given profile, BeanFactory reads Class name, attribute name / value, then BEAN load and property settings via the Reflection mechanism. ApplicationContext covers all features of BeanFactory and provides more features. 5. Spring advanced features Web-based MVC1 frames in J2EE in the world, the world is booming, currently more MVC, old cards with struts, webwork. Emerging MVC frame has Spring MVC, TapeStry, JSF, etc. Most of these are famous teams, and there are also some edge teams' works, and it is quite good. Such as DINAMICA, VRAPTOR, etc. How to choose a suitable framework?