Spring控制反轉容器的使用例子,spring容器

來源:互聯網
上載者:User

Spring控制反轉容器的使用例子,spring容器

詳細代碼如下:

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 name="product" class="com.mstf.bean.Product"/>        <!-- 通過參數名傳遞參數 -->    <bean name="featuredProduct" class="com.mstf.bean.Product"><constructor-arg name="name" value="孝感"/>        <constructor-arg name="description" value="簡介"/><constructor-arg name="price" value="9.95"/>    </bean>        <!-- 通過指數方式傳遞參數 -->    <bean name="featuredProduct2" class="com.mstf.bean.Product">        <constructor-arg index="0" value="孝感"/>        <constructor-arg index="1" value="簡介"/>        <constructor-arg index="2" value="9.95"/>    </bean>    <bean id="calendar" class="java.util.Calendar" factory-method="getInstance"/>        <!-- 通過構造器方式執行個體化 -->    <bean name="employee1" class="com.mstf.bean.Employee">        <property name="homeAddress" ref="simpleAddress"/>        <property name="firstName" value="孝"/>        <property name="lastName" value="感"/>    </bean><!-- 構造器方式依賴注入 -->    <bean name="employee2" class="com.mstf.bean.Employee">        <constructor-arg name="firstName" value="孝"/>        <constructor-arg name="lastName" value="感"/>        <constructor-arg name="homeAddress" ref="simpleAddress"/>    </bean>        <!-- 被依賴類(Employee依賴於Address類,以下配置是為了每個Employee執行個體都能包含Address執行個體) -->    <bean name="simpleAddress" class="com.mstf.bean.Address">        <constructor-arg name="line1" value="湖北省孝感市"/>        <constructor-arg name="line2" value=""/>        <constructor-arg name="city" value="孝感市"/>        <constructor-arg name="state" value="China"/>        <constructor-arg name="zipCode" value="99999"/>        <constructor-arg name="country" value="CN"/>    </bean></beans>

  Address

package com.mstf.bean;/** * Address類和Employee類屬於setter方式依賴注入 * @author wangzheng * */public class Address {private String line1;private String line2;private String city;private String state;private String zipCode;private String country;public Address(String line1, String line2, String city, String state, String zipCode, String country) {super();this.line1 = line1;this.line2 = line2;this.city = city;this.state = state;this.zipCode = zipCode;this.country = country;}@Overridepublic String toString() {return "Address [line1=" + line1 + ", line2=" + line2 + ", city=" + city + ", state=" + state + ", zipCode="+ zipCode + ", country=" + country + "]";}}

  Employee

package com.mstf.bean;/** * Employee類和Address類屬於setter方式依賴注入 * @author wangzheng * */public class Employee {private String firstName;private String lastName;private Address homeAddress;public Employee() {}public Employee(String firstName, String lastName, Address homeAddress) {super();this.firstName = firstName;this.lastName = lastName;this.homeAddress = homeAddress;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public Address getHomeAddress() {return homeAddress;}public void setHomeAddress(Address homeAddress) {this.homeAddress = homeAddress;}@Overridepublic String toString() {return "Employee [firstName=" + firstName + ", lastName=" + lastName + ", homeAddress=" + homeAddress + "]";}}

  Product

package com.mstf.bean;import java.io.Serializable;/** * 帶參數的構造器來初始化類 * @author wangzheng * */public class Product implements Serializable { // product類private static final long serialVersionUID = 1L;private String name;private String descripition;private float price;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDescripition() {return descripition;}public void setDescripition(String descripition) {this.descripition = descripition;}public float getPrice() {return price;}public void setPrice(float price) {this.price = price;}public Product() {}public Product(String name, String descripition, float price) {super();this.name = name;this.descripition = descripition;this.price = price;}}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.