1, what is the spring framework?
What exactly is the spring framework? Is it a web framework like struts, or a data-tier framework like hibernate? The Spring framework is a lightweight container, primarily a lightweight container for managing the life cycle of the JavaBean, which can be used alone or in combination with the Struts framework.
2,spring Framework Overview
3,spring Hello World Instance
First, create a hellospring Java project, then import the spring packages required for the project, create two. java files, and the Helloworld.java code is as follows:
package com.leetch.spring;publicclass HelloWorld { private String message; publicvoidsetMessage(String message){ this.message = message; } publicvoidgetMessage(){ System.out.println("Your Message : " + message); }}
The Mainapp.java file code is as follows:
package com.leetch.spring;< Span class= "Hljs-keyword" >import org.springframework.context.ApplicationContext; import org.springframework.context.support.classpathxmlapplicationcontext;< Span class= "Hljs-keyword" >public class mainapp { public Static void main (string[] args) { ApplicationContext context = new classpathxmlapplicationcontext ( "beans.xml" ); HelloWorld obj = (HelloWorld) context.getbean ( "HelloWorld" ); Obj.getmessage (); }}
Then create a bean configuration file, which is an XML file and is combined as a cement class. Typically the developer retains the name of this file as the Beans.xml file, but must be independent, used in Beans.xml to assign a unique ID to different beans and controls to create different value objects without affecting any Spring source files. For example, using the following file, you can pass any value for the "message" variable, so you can print a different value for the message without affecting the Helloworld.java and Mainapp.java files.
<?xml version= "1.0" encoding= "UTF-8"?><beans xmlns="Http://www.springframework.org/schema/beans"xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/ Schema/beans/spring-beans-3.0.xsd "> <bean id="HelloWorld" class="Com.yiibai.HelloWorld"> < property name="message" value="Hello world!" /> </Bean></Beans>
When the spring application is loaded into memory, the framework leverages the above configuration file to create all the defined beans and assign them a unique ID in the bean tag definition. You can use property tags to pass values that use different variables when you create an object.
Finally, the code is run and the results are as follows:
Message : Hello World!
You can see the flexibility of the Spring application by changing the value of the "message" property and keeping both of the source files intact.
Features of the 4,spring framework
- Facet-oriented (aspect-oriented programming AOP);
- Control rollover (inversion of control IOC);
- Light weight: Not dependent on other objects, used alone;
- Container: Set the declaration period and configuration in XML;
- Framework: Objects in the system are combined by an XML file configuration;
- Advantages:
The lightweight container frame is not intrusive;
The use of IOC containers makes it easier to combine direct relationships between objects, interface-oriented programming, and reduced coupling;
AOP can be more easily extended, following the principles of OCP development;
Creating objects By default is a singleton, and you do not need to use singleton mode for processing.
- Disadvantages:
Business functions rely on spring-specific functionality and are dependent on the spring environment.
OK ~, know the first summed up so many ~, later to add ~
Spring Framework Learning