[Java Web] Spring

Source: Internet
Author: User

I. Overview

Spring is a lightweight enterprise application development framework that provides control inversion (IoC) and aspect-oriented programming (AOP) technology.

Second, control reversal

Control reversal means that the relationship between programs is no longer controlled directly using code, but instead is controlled using a container. So control is transferred from program code to external container, control transfer is control inversion. Because the dependencies between program components are controlled by the container. Dependency injection is a form of inversion control that dynamically injects dependencies into components through a container during a program run. In the IOC, a configuration file describes the dependencies between objects, and no longer calls other class objects using code. During a program run, the IOC container injects dependencies between objects by loading configuration files.

Beanfactory provides the core functionality of spring, using Factory mode for reverse control and dependency injection. Read the definition of JavaBean by reading an XML file or a property file for JavaBean creation, configuration, and management. Beanfactory is generally used as a container to obtain JavaBean. Use the Beanfactory subinterface ApplicationContext to read the XML configuration file and load the JavaBean.

//Message.java PackageBean; Public classMessage {PrivateString content;  PublicString getcontent () {returncontent; }     Public voidsetcontent (String content) { This. Content =content; }}<!--beans.xml--><?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.xsd" > <beanclass= "Bean. Message "name=" message "> <property name=" Content "value=" This is a spring test! " /> </bean></beans>//Solution.javaImportBean. Message;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classSolution { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Beans.xml"); Message Message= (message) Context.getbean ("message"));    System.out.println (Message.getcontent ()); }}
View Code

Assignment injection.

//Company.java PackageBean; Public classCompany {Private intID; PrivateString name;  Public intgetId () {returnID; }     PublicString GetName () {returnname; }     Public voidSetId (intID) { This. ID =ID; }     Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {returnString.Format ("[%d:%s]", ID, name); }}//Worker.java PackageBean;Importjava.util.List; Public classWorker {Private intID; PrivateString name; PrivateCompany company ; PrivateList<string>Like ;  Public intgetId () {returnID; }     PublicString GetName () {returnname; }     PublicCompany Getcompany () {returnCompany ; }     PublicList<string>getlike () {returnLike ; }     Public voidSetId (intID) { This. ID =ID; }     Public voidsetName (String name) { This. Name =name; }     Public voidSetcompany (company company) { This. Company =Company ; }     Public voidSetlike (list<string>Like ) {         This. like =Like ; } @Override PublicString toString () {returnString.Format ("[%d:%s] works at%s and likes%s", ID, name, company, like); }}//Spring-config.xml<?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.xsd" > <bean id= "Comp"class= "Bean. Company "> <property name=" id "value=" 101 "/> <property name=" name "value=" Google "/> </be an> <bean id= "worker"class= "Bean. Worker "> <property name=" id "value=" 1001 "/> <property name=" name "value=" Lee "/> &LT;PR Operty name= "Company" ref= "comp"/> <property name= "like" > <list> <value >Coding</value> <value>Game</value> &LT;VALUE&GT;BASKETBALL&LT;/VALUE&G            T </list> </property> </bean></beans>//Solution.javaImportBean. Worker;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classSolution { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Spring-config.xml"); Worker worker= (worker) Context.getbean ("worker"));    System.out.println (Worker.tostring ()); }}
View Code

Constructor injection.

//Company.java PackageBean; Public classCompany {Private intID; PrivateString name;  PublicCompany (intID, String name) {         This. ID =ID;  This. Name =name; } @Override PublicString toString () {returnString.Format ("[%d:%s]", ID, name); }}//Worker.java PackageBean;Importjava.util.List; Public classWorker {Private intID; PrivateString name; PrivateCompany company ; PrivateList<string>Like ;  PublicWorker (intID, String name, company Company, List<string>Like ) {         This. ID =ID;  This. Name =name;  This. Company =Company ;  This. like =Like ; } @Override PublicString toString () {returnString.Format ("[%d:%s] works at%s and likes%s", ID, name, company, like); }}//Spring-config.xml<?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.xsd" > <bean id= "Comp"class= "Bean. Company "> <constructor-arg index=" 0 "value=" 101 "/> <constructor-arg index=" 1 "value=" Google "/&gt    ; </bean> <bean id= "worker"class= "Bean.        Worker "> <constructor-arg index=" 0 "value=" 1001 "/> <constructor-arg index=" 1 "value=" Lee "/>                <constructor-arg index= "2" ref= "comp"/> <constructor-arg index= "3" > <list> <value>Coding</value> <value>Game</value> <value>bas ketball</value> </list> </constructor-arg> </bean></beans>//Solution.javaImportBean. Worker;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classSolution { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Spring-config.xml"); Worker worker= (worker) Context.getbean ("worker"));    System.out.println (Worker.tostring ()); }}
View Code

[Java Web] Spring

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.