Spring-----2, Spring download and installation

Source: Internet
Author: User

I. Using spring in JAVASE applications

1. Download spring (download 1)

Download: Spring-frameword-3.05.release-with-docs.zip (Spring framework and documentation) + Spring-frameword-3.0.5.release-dependencies.zip ( The spring framework compiles and runs dependent third-party class libraries)

SPRING-FRAMEWORD-3.05.RELEASE-WITH-DOCS.ZIP directory structure:

Dist: Contains the Spring jar package, consists of 20 modules of the jar package, different jar packages provide different functions, the general core container corresponding jar package is the use of spring must

Docs: Documents that store spring, including development guides, API reference documents

Projects: Project structure for storing spring sub-modules, including project source code, MAVEN build file, Ant+lvy makefile

SRC: Project source code containing the spring modules; The source of each jar package containing a sub-module of the project

Other license and project-related documents on spring


2. Copy the jar package for the required modules in the Dist directory to the project Classpath path, and publish the jar packages together if you want to publish the app, and if there are not too many constraints, it is recommended that you copy all the jar packages in the Dist directory


3. Unzip the Spring-frameword-3.0.5.release-dependencies.zip; This directory contains the third-party class libraries on which the spring framework compiles and runs, depending on the project used by the application, and which jar files are required, usually by adding common-loggin Jar files in folders such as G




Ii. using spring in Web applications

Copy all jar packages under the dist path of the spring project to the Web App Web-inf/lib path

Copy the required third-party class library under spring's Spring-framework-3.0.5.release-dependencies.zip decompression path to the Web App Web-inf/lib path

Iii. developing spring applications in eclipse

New User library adds spring support for this project

Add Libraries--User Library

Write the main program, the main program only initializes the spring container, the spring container is the core of the spring application, which manages the Java components in the container public class Springtest {public static void main (                String[] (args) {//Read the configuration file create a spring container The bean component that creates the Bean:spring container in spring is created and initialized when the container is created and instantiated       ApplicationContext context = new Classpathxmlapplicationcontext ("Bean.xml");             ApplicationContext context = new Filesystemxmlapplicationcontext (//"beans.xml");       Resource Resource = new Classpathresource ("Beans.xml");             Beanfactory context = new xmlbeanfactory (Resource);             Output Spring container System.out.println (context);       Get Personservice p = Context.getbean ("Personservice", Personservice.class) through the spring container;    P.info (); The}}applicationcontext instance is the spring container, which is the core of spring and is a huge, powerful factory; Once you get the spring container, It is up to the container to access the Beanspring container in the spring container without forcing the managed component to be the standard JavaBean spring container based on the profile information, responsible for creating the Personservice instance, and set property values for it-----the way that the spring container sets properties for an object is called inversion of control (inversion of CONTROL,IOC)

public class Personservice {    private String name;       Omit name Setter, getter method public       void info () {       System.out.println ("This person is named:" + name);}    }

Bean.xml:<?xml version= "1.0" encoding= "UTF-8"?><!--the root element of the spring configuration file, using spring-beans-3.0.xsd semantics constraints--><    Beans xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://www.springframework.org/schema/beans" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd "default-lazy-init=" true "> <!-deploy Personservice class as Bean in Spring container--<bean id=" Perso Nservice "class=" com.       Personservice "lazy-init=" true "scope=" singleton "init-method=" Myinit "destroy-method=" Mhydestroy "> Normal attribute injection: <property name= "name" value= "Lin Bingdong"/> Class type Attribute injection: <property name= "B1" ref= "Bean1"/> </bean&gt     ; <bean id= "Bean1" class= "come. Bean1 "/>--------------------------------------------------------------------------------------------------- -----------root tag: defualt-lazy-init= "True" (all object deferred creation) bean tag: lazy-init= "True" (deferred creation of bean object; When the container is created, the object is not created when the Getbean method is calledis created)--------------------------------------------------------------------------------------------------------      ------scope= "Singleton": Spring creates objects by default in singleton mode, where objects created with the same ID are the same object scope values: Singleton: Singleton (only one object) Prototype: Each call to the Getbean method container creates a new bean for the web app only, placing the object in the following specified scope Request:session:global session : Equivalent to Application------------------------------------------------------------------------------------------------ --------------init-metod= "Object Initialization method Name": Specifies the method that is called when the object is initialized destroy-method= "Object Destroy Method Name": Specifies the method that is called when the object is destroyed; The property is only scope= "singlet    On "is only effective when the container is closed or destroyed when the construction method is injected: private A;     Private String B;       Public Main (a A, String b) {this.a = A;    this.b = b;      }----------------------------------------------------------<constructor-arg index= "0" ref= "a"/>-                                     <constructor-arg index= "1" value= "abc"/>-</bean>         --<bean id= "a" class= "com. A "></bean>---------------------------------------------------------</beans>




Spring-----2, Spring download and installation

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.