Java學習筆記:Spring架構

來源:互聯網
上載者:User

標籤:getname   port   system   tca   spring   3.3   spi   amp   nfa   

1.什麼是Spring?

  1.1 spring 是一個開源架構

  1.2 spirng 為簡化企業級應用開發而生,使用 spring 可以使簡單的 JavaBean 實現以前只有 EJB 才能實現的功能

  1.3 spring 是一個 IOC(DI) 和 AOP 容器架構

2.Spring入門樣本   

  2.1 HelloWorld.java

package com.oneline.spring.day01;public class HelloWorld {     private String name;        public void setName(String name){        this.name=name;    }        public void hello(){        System.out.println("hello:"+name);    }}

  2.2 Main.java

package com.oneline.spring.day01;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {    public static void main(String[]args){        // 1.建立 Spring 的 IOC 容器物件        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");        // 2.從 IOC 容器中擷取 Bean 的執行個體        HelloWorld helloWorld=(HelloWorld) ctx.getBean("helloWorld");        // 3.調用 hello 方法        helloWorld.hello();    }    }

  2.3 applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">    <!-- 配置bean -->    <bean id="helloWorld" class="com.oneline.spring.day01.HelloWorld">        <property name="name" value="Spring"></property>    </bean></beans>

    2.3.1 class: bean 的全類名,通過反射的方式中IOC容器中建立Bean,所以要求Bean中必須有無參數的構造器

    2.3.2 id:標識容器中的bean

3.IOC & DI 

  3.1 BeanFactory:IOC 容器的基本實現

  3.2 ApplicationContext:提供類更多的進階特性,是BeanFactory的子介面

  3.3 BeanFactory 是 Spring 架構的基礎設施,面向Spring本身,ApplicationContext 面向使用Spring架構的開發人員,幾乎所有的應用場合都直接使用ApplicationContext 而並非底層的 BeanFactory

  3.4 無論使用何種方式,設定檔時相同的

4.DI 依賴注入

  4.1 通過set方法注入,最常用的注入方式

  <!-- 通過 set 方法來配置 bean 的屬性 -->    <property name="name" value="Spring"></property>

  4.2 通過構造方法來注入

    4.2.1 Car.java

package com.oneline.spring.day01;public class Car {    private String name;        private Integer price;    public Car(String name, Integer price) {        super();        this.name = name;        this.price = price;    }    @Override    public String toString() {        return "Car [name=" + name + ", price=" + price + "]";    }}
  <!-- 通過構造方法來配置 bean 的屬性 -->    <bean id="car" class="com.oneline.spring.day01.Car">        <constructor-arg value="Audi" index="0" ></constructor-arg>        <constructor-arg value="3000" type="java.lang.Integer"></constructor-arg>    </bean>    <!-- 如果注入的屬性中含有特殊字元,參考以下寫法 -->    <bean id="car" class="com.oneline.spring.day01.Car">        <constructor-arg index="0" >            <value><![CDATA[<ShangHai>]]></value>        </constructor-arg>        <constructor-arg type="java.lang.Integer">            <value>250</value>        </constructor-arg>    </bean>

PS:constructor-arg 標籤的屬性詳解, value:注入屬性的屬性值 ,index:注入屬性的位置, type:注入屬性的類型(可以利用這些屬性值調用指定的構造器)

  4.3 類之間相互引用的配置

package com.oneline.spring.day01;public class Person {    private String name;        private Integer age;        private Car car;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public Car getCar() {        return car;    }    public void setCar(Car car) {        this.car = car;    }    @Override    public String toString() {        return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";    }    }
  <bean id="person" class="com.oneline.spring.day01.Person">        <property name="name" value="Tom"></property>        <property name="age" value="24"></property>        <!-- 可以使用property的ref屬性來建立bean之間的參考關聯性 -->        <property name="car" ref="car2"></property>    </bean>

 

Java學習筆記: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.