原文地址: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
原文链接:https://blog.csdn.net/w605283073/article/details/51435289
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~