[Java Web] Spring

來源:互聯網
上載者:User

標籤:ddd   檔案讀取   cto   encoding   企業   get   模式   c中   support   

一、概述

Spring是一種輕量級公司專屬應用程式開發架構,提供了控制反轉(IoC)和面向切面編程(AOP)技術。

 

二、控制反轉

控制反轉是指程式之間的關係不再直接使用代碼進行控制,而是使用容器進行控制。如此控制權就由程式碼轉移到外部容器,控制權轉移就是控制反轉。由於程式組件之間的依賴關係是由容器控制的。在程式運行期間,通過容器動態地將依賴關係注入到組件中就是依賴注入,依賴注入是反轉控制的一種形式。在IoC中通過設定檔描述對象之間的依賴關係,而不再使用代碼調用其他類對象。在程式運行期間,IoC容器通過載入設定檔,將對象之間的依賴關係注入。

BeanFactory提供了Spring的核心功能,採用原廠模式實現反轉控制和依賴注入。通過讀取XML檔案或屬性檔案讀取JavaBean的定義,從而實現JavaBean的建立、配置和管理。一般將BeanFactory作為容器來擷取JavaBean。使用BeanFactory的子介面ApplicationContext讀取XML設定檔並載入JavaBean。

//Message.javapackage bean;public class Message {    private String content;    public String getContent() {        return content;    }    public void setContent(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">    <bean class="bean.Message" name="message">        <property name="content" value="this is a spring test!"/>    </bean></beans>//Solution.javaimport bean.Message;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Solution {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");        Message message = (Message) context.getBean("message");        System.out.println(message.getContent());    }}
View Code

賦值注入。

//Company.javapackage bean;public class Company {    private int id;    private String name;    public int getId() {        return id;    }    public String getName() {        return name;    }    public void setId(int id) {        this.id = id;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return String.format("[%d: %s]", id, name);    }}//Worker.javapackage bean;import java.util.List;public class Worker {    private int id;    private String name;    private Company company;    private List<String> like;    public int getId() {        return id;    }    public String getName() {        return name;    }    public Company getCompany() {        return company;    }    public List<String> getLike() {        return like;    }    public void setId(int id) {        this.id = id;    }    public void setName(String name) {        this.name = name;    }    public void setCompany(Company company) {        this.company = company;    }    public void setLike(List<String> like) {        this.like = like;    }    @Override    public String toString() {        return String.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"/>    </bean>    <bean id="worker" class="bean.Worker">        <property name="id" value="1001"/>        <property name="name" value="Lee"/>        <property name="company" ref="comp"/>        <property name="like">            <list>                <value>Coding</value>                <value>Game</value>                <value>Basketball</value>            </list>        </property>    </bean></beans>//Solution.javaimport bean.Worker;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Solution {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");        Worker worker = (Worker) context.getBean("worker");        System.out.println(worker.toString());    }}
View Code

構造器注入。

//Company.javapackage bean;public class Company {    private int id;    private String name;    public Company(int id, String name) {        this.id = id;        this.name = name;    }    @Override    public String toString() {        return String.format("[%d: %s]", id, name);    }}//Worker.javapackage bean;import java.util.List;public class Worker {    private int id;    private String name;    private Company company;    private List<String> like;    public Worker(int id, String name, Company company, List<String> like) {        this.id = id;        this.name = name;        this.company = company;        this.like = like;    }    @Override    public String toString() {        return String.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"/>    </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>Basketball</value>            </list>        </constructor-arg>    </bean></beans>//Solution.javaimport bean.Worker;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Solution {    public static void main(String[] args) {        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");        Worker worker = (Worker) context.getBean("worker");        System.out.println(worker.toString());    }}
View Code

 

[Java Web] Spring

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.