codecamp

弹簧教程 - 弹簧汽车线束

弹簧教程 - 弹簧汽车线束


春天可以自动地蚕豆。要启用它,请在< bean>中定义“autowire"属性。

<bean id="customer" class="com.www.w3cschool.cnmon.Customer" autowire="byName" />

弹簧有五种自动接线模式。

  • no - Default, no auto wiring
  • byName - Auto wiring by property name.
  • byType - Auto wiring by property data type.
  • constructor - byType mode in constructor argument.
  • autodetect - If a default constructor is found, use "autowired by constructor"; Otherwise, use "autowire by type".


Java Bean

客户Java bean。

package com.www.w3cschool.cnmon;
public class Customer 
{
  private Person person;
  public Customer(Person person) {
    this.person = person;
  }
  public void setPerson(Person person) {
    this.person = person;
  }
}

Person Java bean

package com.www.w3cschool.cnmon;
public class Person 
{
}


自动接线“否"

这是默认模式,我们需要通过“ref"属性连接Java bean。

<bean id="customer" class="com.www.w3cschool.cnmon.Customer">
         <property name="person" ref="person" />
</bean>
<bean id="person" class="com.www.w3cschool.cnmon.Person" />

自动接线“byName"

以下代码将autowire byName添加到bean声明中。

<bean id="customer" class="com.www.w3cschool.cnmon.Customer" autowire="byName" />

因为“person"bean的名称与“customer"bean的名称相同“person"属性,Spring将通过setPerson(Person person)方法自动连接。

<bean id="customer" class="com.www.w3cschool.cnmon.Customer" autowire="byName" />
<bean id="person" class="com.www.w3cschool.cnmon.Person" />

自动布线“byType"

以下xml配置将自动连线类型声明为byType。

<bean id="customer" class="com.www.w3cschool.cnmon.Customer" autowire="byType" />

因为“person"bean的数据类型与数据类型相同“客户"bean的属性person对象,Spring将通过方法setPerson(Person person)自动连接它。

<bean id="customer" class="com.www.w3cschool.cnmon.Customer" autowire="byType" />
<bean id="person" class="com.www.w3cschool.cnmon.Person" />

自动布线“构造函数"

以下代码将bean的自动连线类型声明为构造函数

<bean id="customer" class="com.www.w3cschool.cnmon.Customer" autowire="constructor" />

“person"bean的数据类型与“customer"bean的属性(Person对象)中的构造函数参数数据类型相同,Spring将通过构造方法 - “public Customer(Person person)"自动连接它。

<bean id="customer" class="com.www.w3cschool.cnmon.Customer" autowire="constructor" />
<bean id="person" class="com.www.w3cschool.cnmon.Person" />

自动布线“构造函数"...

以下代码显示如何使用autodetect autowire。如果找到构造函数,则使用“constructor"; 否则,使用“byType"。

<bean id="customer" class="com.www.w3cschool.cnmon.Customer" 
      autowire="autodetect" dependency-check="objects />

由于在“Customer"类中有一个构造函数,Spring将通过构造方法 - “public Customer(Person person)"自动连接它。

<bean id="customer" class="com.www.w3cschool.cnmon.Customer" autowire="autodetect" />
<bean id="person" class="com.www.w3cschool.cnmon.Person" />
Spring教程 - Spring Place Holder属性
Spring教程 - Spring Bean自动扫描
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }