原文地址:http://websystique.com/spring/spring-beans-auto-wiring-example-using-xml-configuration/

【项目代码,在文章末尾提供下载地址】

【翻译 by  明明如月 QQ 605283073】

上一篇:Spring 4 Hello World 例子(带源码)

Bean 装配是为了提供需要完成任务所依赖bean。Spring中,bean可以通过手动和自动两种方式装配。

手动装配 : 通过 <property> 或者 <constructor> 标签中的ref 属性

<!-- default example (autowire="no") -->
<bean id="driver" class="com.websystique.spring.domain.Driver"><property name="license" ref="license"/>
</bean><bean id="license" class="com.websystique.spring.domain.License" ><property name="number" value="123456ABCD"/>
</bean>

自动装配:使用<bean> 标签中的autowire

<bean id="application" class="com.websystique.spring.domain.Application" autowire="byName"/>

在此例中,bean将会通过Spring自动装配特性自动的装配。

有4种方式:

  • autowire="byName" : 根据属性名自动装配。如果另外一个bean属性名于此相同则自动装配。
  • autowire="byType" : 根据bean的类型进行自动装配。
  • autowire="constructor" : 根据构造方法装自动装配。如果其他bean的构造参数和此相同则此bean将被装配到其他bean的构造方法。
  • autowire="no" : 不自动装配。和通过ref指定一样。

1. autowire=”byName” 例子

package com.websystique.spring.domain;public class Application {private ApplicationUser applicationUser;public ApplicationUser getApplicationUser() {return applicationUser;}public void setApplicationUser(ApplicationUser applicationUser) {this.applicationUser = applicationUser;}@Overridepublic String toString() {return "Application [applicationUser=" + applicationUser + "]";}
}
package com.websystique.spring.domain;public class ApplicationUser {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "ApplicationUser [name=" + name + "]";}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- byName example --><bean id="application" class="com.websystique.spring.domain.Application" autowire="byName"/><bean id="applicationUser" class="com.websystique.spring.domain.ApplicationUser" ><property name="name" value="superUser"/></bean></beans>

application类的applicationUser属性 和 ApplicationUser bean的id相同。

package com.websystique.spring;import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.websystique.spring.domain.Application;public class AppMain {public static void main(String args[]){AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");//autowire=byName Application application = (Application)context.getBean("application");System.out.println("Application Details : "+application);}
}

运行结果:

Application Details : Application [applicationUser=ApplicationUser [name=superUser]]

2. autowire=”byType”例子

package com.websystique.spring.domain;public class Employee {private EmployeeAddress address;public EmployeeAddress getAddress() {return address;}public void setAddress(EmployeeAddress address) {this.address = address;}@Overridepublic String toString() {return "Employee [address=" + address + "]";}
}

package com.websystique.spring.domain;public class EmployeeAddress {private String Street;private String city;public String getStreet() {return Street;}public void setStreet(String street) {Street = street;}public String getCity() {return city;}public void setCity(String city) {this.city = city;}@Overridepublic String toString() {return "EmployeeAddress [Street=" + Street + ", city=" + city + "]";}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- byType example --><bean id="employee" class="com.websystique.spring.domain.Employee" autowire="byType"/><bean id="employeeAddress" class="com.websystique.spring.domain.EmployeeAddress" ><property name="street" value="112/223,SantaVila"/><property name="city" value="Nebraska"/></bean></beans>

Employee 类中 EmployeeAddress address 和employeeAddress 并不相同,但是是按照类型注入。所以可以装配进去。

运行:
package com.websystique.spring;import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.websystique.spring.domain.Employee;public class AppMain {public static void main(String args[]){AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");//autowire=byTypeEmployee employee = (Employee)context.getBean("employee");System.out.println("Employee Details : "+employee);}
}

输出:

Employee Details : Employee [address=EmployeeAddress [Street=112/223,SantaVila, city=Nebraska]]

3. autowire=”constructor”

package com.websystique.spring.domain;public class Performer {private Instrument instrument;public Performer(Instrument instrument){this.instrument = instrument;}@Overridepublic String toString() {return "Performer [instrument=" + instrument + "]";}
}
package com.websystique.spring.domain;public class Instrument {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Instrument [name=" + name + "]";}
}

注意:Performer类有一个接收Instrument类型的参数的构造方法。

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" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- constructor example --><bean id="performer" class="com.websystique.spring.domain.Performer" autowire="constructor"/><bean id="instrument" class="com.websystique.spring.domain.Instrument" ><property name="name" value="PIANO"/></bean></beans>
Performer<span style="font-weight: bold;"><span style="font-family:Consolas, Bitstream Vera Sans Mono, Courier New, Courier, monospace;"><span style="font-size: 13px; line-height: 14.3px; white-space: pre; background-color: rgb(245, 245, 245);"><span style="color:#0000ff;"><span style="color: rgb(64, 64, 64); font-family: 'Open Sans', sans-serif; font-size: 15px; line-height: 22.5px;">类有一个接收Instrument类型的参数的构造方法。将在Spring 定义的id为instrument的装配进去,。</span></span></span></span></span>
<pre name="code" class="java">package com.websystique.spring;import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.websystique.spring.domain.Performer;public class AppMain {public static void main(String args[]){AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");//autowire=constructorPerformer performer = (Performer)context.getBean("performer");System.out.println("Performer Details : "+performer);}}
运行结果:
<pre name="code" class="plain">Performer Details : Performer [instrument=Instrument [name=PIANO]]

3. autowire=”no”

<pre name="code" class="java">package com.websystique.spring.domain;public class Driver {private License license;public void setLicense(License license) {this.license = license;}public License getLicense() {return license;}@Overridepublic String toString() {return "Driver [license=" + license + "]";}
}
package com.websystique.spring.domain;public class License {private String number;public String getNumber() {return number;}public void setNumber(String number) {this.number = number;}@Overridepublic String toString() {return "License [number=" + number + "]";}}

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- default example (autowire="no") --><bean id="driver" class="com.websystique.spring.domain.Driver" autowire="no"><property name="license" ref="license"/></bean><bean id="license" class="com.websystique.spring.domain.License" ><property name="number" value="123456ABCD"/></bean></beans>

autowire="no" 没有什么作用。可以不写。只是不自动装载。

运行:

package com.websystique.spring;import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.websystique.spring.domain.Driver;public class AppMain {public static void main(String args[]){AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");//autowire=defaultDriver driver = (Driver)context.getBean("driver");System.out.println("Driver Details : "+driver);}
}

结果:

Driver Details : Driver [license=License [number=123456ABCD]]

下载地址:http://websystique.com/?smd_process_download=1&download_id=785

Spring Beans 自动装配 使用XML配置列子(带源码)相关推荐

  1. Spring Beans 自动装配

    Spring 容器可以在不使用和 元素的情况下自动装配相互协作的 bean 之间的关系,这有助于减少编写一个大的基于 Spring 的应用程序的 XML 配置的数量. By Name 使用By Nam ...

  2. Spring 4 Hello World 例子(带源码)

    原文地址:http://websystique.com/spring/spring-4-hello-world-example-annotation-tutorial-full-example/ [项 ...

  3. Spring的自动装配方法

    Spring的装配方式 我们已经了解了依赖注入的基本原理,初识了@Component和@AutoWired标注的用法.为一个应用中的Beans的配置依赖注入关系的过程称之为装配(Wiring). Sp ...

  4. jdom编写xml自动缩进_Spring Beans 自动装配

    Spring 容器可以在不使用和 元素的情况下自动装配相互协作的 bean 之间的关系,这有助于减少编写一个大的基于 Spring 的应用程序的 XML 配置的数量. # By Name 使用By N ...

  5. spring Bean自动装配

    spring Bean自动装配 自动装配是使用spring满足bean依赖的一种方式. spring会在应用上下文中为某个bean寻找其依赖的bean. spring自动装配需要从两个角度来实现,或者 ...

  6. (Spring)自动装配bean

    文章目录 自动装配bean 1. 环境搭建 2. byName自动装配 3. byType自动装配 4. 使用注解自动装配 4.1 @Autowired和@Qualifier 4.2 @Resourc ...

  7. spring中使用注解代替xml配置

    今天两部分内容,第一部分是注解,使用注解配置Spring,然后第二个是Spring中的AOP,今天就需要这两部分,也没有练习,第一个注解配置Spring,这三大框架都是用注解来配置,这三大框架,都是支 ...

  8. Spring Autowire自动装配(转http://lep1985520.blog.163.com/blog/static/56600480200901441338486/)

    Spring Autowire自动装配 技术收藏 2009-01-14 16:13:38 阅读284 评论0   字号:大中小 订阅 Spring Autowire自动装配   在应用中,我们常常使用 ...

  9. Spring学习——自动装配

    自动装配说明 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean. Spring中bean有三种装配机制,分别是: 在xml中显式配置 ...

最新文章

  1. 腾讯——这可是一只“骨骼清奇”的狗
  2. Citrix XenDesktop 7.X 视频播放优化
  3. python连接oracle导出数据文件
  4. [云炬mysql数据库笔记] Work2
  5. n个点组成多少个三角形Java,农田开发 NOJ (已知N个点选取3个求最大三角形面积问题)...
  6. python np vstack_numpy vstack内部for循环
  7. aop 获取方法入参出参_Spring AOP 如何将参数传递给需要织入的方法
  8. so库调用java函数_linux下so动态库调用主程序函数
  9. 不占内存的浏览器_4款黑科技办公网站,高效实用,不占内存,高手的必备神器...
  10. 【转】No Persistence provider for EntityManager问题
  11. 安装和运行,意思差异应该很明显
  12. Leetcode: Maximum XOR of Two Numbers in an Array
  13. 图像识别的工作原理是什么?商业上如何使用它?
  14. c语言简单表白语言程序,c语言简单代码(c语言简单表白代码)
  15. 税控服务器组件接口v2.1.1.1,税控开票服务器组件接口规范标准版V1.9(2016.04.04).pdf...
  16. crmeb知识付费二开文档
  17. easywechat微信开发系列(2):公众号网页支付
  18. EST封面: 南方科技大学夏雨团队
  19. 数组排序:声明一个整型数组并填充数据,排序,输出排序后数据,倒序输出数据。
  20. 解锁三星bl锁有几种方法_如何判断三星手机bootloader是否解锁_免费解锁BL的3个方法...

热门文章

  1. 调用WindowsMedia播放器与Flash播放器
  2. 程序猿小哥用12万行代码堆出来个「蔡徐坤」!编译竟然还能通过
  3. opencv inrange函数
  4. 交易履约之订单中心实践
  5. ubuntu14.04的自带的拼音输入法问题
  6. python作排产计划表_生产排程计划表
  7. spec2006 测试
  8. 求职 | Python、数据分析、Java
  9. 机器人传感器网络的覆盖优化和空间负载均衡
  10. 解决中端投资痛点,“轻中端”能否抢占投资价值高地?