Spring Beans 自动装配 使用XML配置列子(带源码)
来源:互联网 发布:使命召唤mac 编辑:程序博客网 时间:2024/06/08 16:12
原文地址: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; } @Override public 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; } @Override public 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.xsd http://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; } @Override public 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; } @Override public 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.xsd http://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=byType Employee 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; } @Override public 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; } @Override public 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.xsd http://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=constructor Performer 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; } @Override public 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; } @Override public 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.xsd http://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=default Driver 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
0 0
- Spring Beans 自动装配 使用XML配置列子(带源码)
- Spring XML配置自动装配的Bean
- spring-xml配置bean自动装配
- Spring beans.xml(applicationContext.xml) 配置
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- 使用spring注解 自动装配以及自动扫描机制 实现零xml配置的前提
- Spring 配置使用 - Bean 自动装配
- 【Spring】装配Bean-(1)-使用XML配置
- 简化Spring XML配置---使用注解装配
- Spring装配Bean---使用xml配置
- (笔记)Spring实战_最小化Spring XML配置(1)_自动装配Bean属性
- 简化Spring的xml文件配置-自动装配Bean属性
- 通过xml文件配置 spring自动装配5中模式
- 简化Spring的XML配置(一)——自动装配bean属性
- 《Spring3实战》摘要(3-1)--最小化Spring XML配置之自动装配Bean属性
- Spring学习(2)--在xml中使用autowire自动装配Bean属性
- 关于直播翻页问题研究
- JavaWeb学习总结(十七)——JSP中的九个内置对象
- 第10、11周项目2 - 存储班长信息的学生类(改法2)
- 【数据结构】 栈和队列 YTU 2239 十进制与八进制的转换(栈和队列)
- 斗地主智能(AI)出牌算法
- Spring Beans 自动装配 使用XML配置列子(带源码)
- 利用 NGINX 最大化 Python 性能,第二部分:负载均衡和监控
- Java EnumMap工作原理及实现(一)
- Android学习路线
- ASP.NET学习路线图
- 第12周-项目1(2)
- mybatis入门基础(一)JDBC的简介以及Mybatis的入门程序
- hdu Knight Moves(bfs)
- GIT版本管理:Git Flow模型 --> 陶邦仁 又发现一牛人