Notes on setting up the spring framework

Source: Internet
Author: User

◆ Introduction
Purpose: To solve the complexity of Enterprise Application Development: Use Basic JavaBean instead of EJB, and provide more enterprise application functions: any Java application
The Spring framework is a layered architecture consisting of seven well-defined modules. The Spring module is built on the core container. The core container defines how to create, configure, and manage beans.



Each module (or component) that makes up the Spring framework can exist independently or be implemented together with one or more modules. The functions of each module are as follows:
?Core container: The Core container provides the basic functions of the Spring framework. The main component of the core container is BeanFactory, which is the implementation of the factory mode. BeanFactory uses the control reversal (IOC) mode to separate application configuration and dependency specifications from actual application code.
?Spring Context <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release + release/K83MzhuanJz8/CzsTQxc + ioaNTcHJpbmcgyc/release/s7xo6zA/release/pzqq7 + release/release + o6y + zb/ examples/examples + examples/IDxzdHJvbmc + examples/templates/a1xLTtzvPP + 8 + examples + jqMD9yOe08r + examples/IDxzdHJvbmc + examples/examples + keys + keys/CzsTEo7/keys/CzsTEo7/keys + keys/Pws7EoaPL + keys/keys + vH89LUvLC9q8frx/keys/K21M/keys + keys/WoaPNqLn9st/keys/ 8rzcseSzyc6quN + 2yL/weight + ClNwcmluZyC/weight + 3bfDzsq21M/weight + bXEttTP87/weight + Weight = "magplus" title = "click to view original size image "src =" http://www.bkjia.com/uploads/allimg/140627/0052095c6-1.jpg "height =" 380 "width =" 760 "alt =" \ ">

◆ Term description
① Lightweight ).Spring core package capacity less than 1 MB, Spring can be used in many small devices.
② Non-invasive ).Enhance the reusability of application components and reduce dependencies on the framework.
③ Container ).Objects and attributes are automatically generated based on the configuration file. No code is required to generate objects.
④ Inversion of Control (IOC) and Dependency Injection (DI). The objective of IOC is to rely on abstraction. The relationship between objects is injected into the specified object by the container according to the configuration file.
⑤ AOP (Aspect-oriented programming ).Based on the proxy and interceptor mechanism, combined with Spring IOC, the runtime Weaving method is used to use various declarative system-level services in Spring framework applications.
⑥ Persistent ).Spring provides DAO, programming transactions, and declarative transactions, which simplify the integration and use of ORM tools (Hibernate and iBATIS.
7. Java EE service.You can use the services provided by the Java EE standard and integrate them seamlessly.

◆ Advantages of Spring framework
① It makes J2EE easy to use and promotes programming habits.
Spring does not re-develop existing things. Therefore, in Spring, you will find packages without logging, no connection pools, and no distributed transaction scheduling. These are provided by open-source projects (such as Commons Logging for all log output, or Commons DBCP for data connection pools), or by your application server. For the same reason, we have not provided the O/R mapping layer. For this reason, there are good solutions such as Hibernate and JPA.
② Make existing technologies easier to use.
For example, although we do not have the underlying transaction coordination processing, we provide an abstraction layer covering JTA or any other transaction policies.
③ There is no direct competition with other open-source projects.
For example, many developers have never been happy with Struts and feel that there is still room for improvement in the MVC framework. In some fields, such as lightweight IoC containers and AOP frameworks, Spring has direct competition, but there are no more popular solutions in these fields. (Spring is a pioneer in these areas .)
④ Spring can be transplanted between application servers.Ensuring portability is always a challenge, but Spring avoids any specific platform or non-standardization and supports users on WebLogic, Tomcat, Resin, JBoss, WebSphere, and other application servers.
⑤ Convenient decoupling and simplified development.
Through the IoC container provided by Spring, we can control the dependency between objects by Spring to avoid excessive program coupling caused by hard coding. With Spring, you do not have to write code for underlying requirements such as single-instance mode classes and attribute file parsing, so you can focus more on upper-layer applications.
⑥ Support for AOP programming.
With the AOP function provided by Spring, it is easy to implement Aspect-Oriented Programming. Many features that are not easy to use and implemented by traditional OOP can be easily handled through AOP.
7. Support for declarative transactions
In Spring, we can free up from the tedious Transaction Management Code and flexibly manage transactions in declarative mode to improve development efficiency and quality.
Bytes Convenient program testing
You can use non-container-dependent programming to perform almost all the testing work. In Spring, testing is no longer an expensive operation, but an easy task to do.
Bytes Easy integration of various excellent frameworks
Spring does not exclude all kinds of excellent open-source frameworks. On the contrary, Spring can reduce the difficulty of using various frameworks. Spring provides direct support for various excellent frameworks (such as Struts and Hibernate.
Bytes Reduces the difficulty of using Java EE APIs
Spring provides a thin encapsulation layer for many difficult Java EE APIs (such as JDBC, JavaMail, and remote calls) through Spring's simple encapsulation, the difficulty of using these Java ee apis is greatly reduced.

◆ Download and set Spring
Http://springframework.org/




◆ Prepare the Spring design environment



◆ Write the first Spring Program
HelloWorld interface:

Java code

    /***** @ Copyright (C), 2009-2010 SISE Java Team. * @ Author: easinchu * @ Email: easinchu@gmail.com * @ Description: */public interface HelloWorld {public void sayHello ();}
    HelloWorldBean implementation class:
    Java code
      /***** @ Copyright (C), 2009-2010 SISE Java Team. * @ Author: easinchu * @ Email: easinchu@gmail.com * @ Description: */public class HelloWorldBean implements HelloWorld {private String helloWorld; public void setHelloWorld (String helloWorld) {this. helloWorld = helloWorld;} public void sayHello () {System. out. println (helloWorld );}}
      Spring XML configuration file ioc-config.xml:
      Xml Code
        Hello, Welcome To Spring World!
        Test class:
        Java code
          Import org. springframework. beans. factory. beanFactory; import org. springframework. beans. factory. xml. xmlBeanFactory; import org. springframework. core. io. classPathResource; import org. springframework. core. io. resource;/***** @ Copyright (C), 2009-2010 SISE Java Team. * @ Author: easinchu * @ Email: easinchu@gmail.com * @ Description: Use Spring BeanFactory to construct the IoC container. */public class FirstSpringDemo {public static void main (String [] args) {// ----------- BeanFactory IoC container ------------------- // load XML configuration information from the classpath Resource = new ClassPathResource ("ioc-config.xml"); // instantiate the IOC container, at this point the container does not instantiate the various managed beans defined by the beans-config.xml. beanFactory factory = new XmlBeanFactory (resource); // get managed bean HelloWorld hello = (HelloWorld) factory. getBean ("helloWorldBean"); hello. sayHello ();}}
          Summary
          ① Use XmlBeanFactory to read the xml configuration file and create a BeanFactory instance
          ② BeanFactory completes dependency Injection Based on the configuration file
          ③ Use the getBean () method to specify the Bean name to retrieve the Bean instance

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.