先来一发手动装配的bean
package com.ruge.testAutoWire;
/**
* 描述:
* 人员信息
*
* @outhor 爱丽丝、如歌
* @create 2017-11-04 9:35
*/
public class Person
{
private String name;
private Car car;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", car=" + car +
", address=" + address +
'}';
}
}
package com.ruge.testAutoWire;
/**
* 描述:
*
* @outhor 爱丽丝、如歌
* @create 2017-11-04 9:36
*/
public class Car {
private String carName;
private String carPrice;
@Override
public String toString() {
return "Car{" +
"carName='" + carName + '\'' +
", carPrice='" + carPrice + '\'' +
'}';
}
public String getCarPrice() {
return carPrice;
}
public void setCarPrice(String carPrice) {
this.carPrice = carPrice;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
}
package com.ruge.testAutoWire;
/**
* 描述:
*
* @outhor 爱丽丝、如歌
* @create 2017-11-04 9:38
*/
public class Address {
private String country;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Address{" +
"country='" + country + '\'' +
'}';
}
}
<?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 id="car" class="com.ruge.testAutoWire.Car">
<property name="carName" value="五菱宏光"></property>
<property name="carPrice" value="300万"></property>
</bean>
<bean id="address" class="com.ruge.testAutoWire.Address">
<property name="country" value="中国"></property>
</bean>
<bean id="person" class="com.ruge.testAutoWire.Person">
<property name="name" value="周杰伦"></property>
<property name="address" ref="address"></property>
<property name="car" ref="car"></property>
</bean>
</beans>
package com.ruge.testAutoWire;
import javafx.application.Application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 描述:
* 测试
*
* @outhor 爱丽丝、如歌
* @create 2017-11-04 9:52
*/
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-AutoWire.xml");
Person person = (Person) applicationContext.getBean("person");
System.out.println(person);
}
}
D:\download\tool\jdk\jdk8_64\install\bin\java -Didea.launcher.port=7533 -Didea.launcher.bin.path=D:\download\tool\ide\idea\2016\ideaIU-2016\install\bin -Dfile.encoding=UTF-8 -classpath D:\download\tool\jdk\jdk8_64\install\jre\lib\charsets.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\deploy.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\access-bridge-64.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\cldrdata.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\dnsns.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\jaccess.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\jfxrt.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\localedata.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\nashorn.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunec.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunjce_provider.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunmscapi.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunpkcs11.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\zipfs.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\javaws.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jce.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jfr.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jfxswt.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jsse.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\management-agent.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\plugin.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\resources.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\rt.jar;D:\project_idea\spring\target\classes;D:\download\tool\versionControl\maven\localRepository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-beans\5.0.0.RELEASE\spring-beans-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-context\5.0.0.RELEASE\spring-context-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-aop\5.0.0.RELEASE\spring-aop-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-core\5.0.0.RELEASE\spring-core-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-jcl\5.0.0.RELEASE\spring-jcl-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-expression\5.0.0.RELEASE\spring-expression-5.0.0.RELEASE.jar;D:\download\tool\ide\idea\2016\ideaIU-2016\install\lib\idea_rt.jar com.intellij.rt.execution.application.AppMain com.ruge.testAutoWire.test
十一月 04, 2017 9:54:10 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5387f9e0: startup date [Sat Nov 04 09:54:10 CST 2017]; root of context hierarchy
十一月 04, 2017 9:54:10 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean-AutoWire.xml]
Person{name='周杰伦', car=Car{carName='五菱宏光', carPrice='300万'}, address=Address{country='中国'}}
Process finished with exit code 0
手动装配大功告成
自动装配 → byName
<?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"
default-autowire="byName">
<!-- ↑↑↑
byName:通过名称进行自动匹配;
byType:根据类型进行自动匹配;-->
<bean id="car" class="com.ruge.testAutoWire.Car">
<property name="carName" value="五菱宏光"></property>
<property name="carPrice" value="300万"></property>
</bean>
<bean id="address" class="com.ruge.testAutoWire.Address">
<property name="country" value="中国"></property>
</bean>
<bean id="person" class="com.ruge.testAutoWire.Person">
<property name="name" value="周杰伦"></property>
</bean>
</beans>
D:\download\tool\jdk\jdk8_64\install\bin\java -Didea.launcher.port=7532 -Didea.launcher.bin.path=D:\download\tool\ide\idea\2016\ideaIU-2016\install\bin -Dfile.encoding=UTF-8 -classpath D:\download\tool\jdk\jdk8_64\install\jre\lib\charsets.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\deploy.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\access-bridge-64.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\cldrdata.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\dnsns.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\jaccess.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\jfxrt.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\localedata.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\nashorn.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunec.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunjce_provider.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunmscapi.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunpkcs11.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\zipfs.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\javaws.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jce.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jfr.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jfxswt.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jsse.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\management-agent.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\plugin.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\resources.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\rt.jar;D:\project_idea\spring\target\classes;D:\download\tool\versionControl\maven\localRepository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-beans\5.0.0.RELEASE\spring-beans-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-context\5.0.0.RELEASE\spring-context-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-aop\5.0.0.RELEASE\spring-aop-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-core\5.0.0.RELEASE\spring-core-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-jcl\5.0.0.RELEASE\spring-jcl-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-expression\5.0.0.RELEASE\spring-expression-5.0.0.RELEASE.jar;D:\download\tool\ide\idea\2016\ideaIU-2016\install\lib\idea_rt.jar com.intellij.rt.execution.application.AppMain com.ruge.testAutoWire.test
十一月 04, 2017 10:08:38 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5387f9e0: startup date [Sat Nov 04 10:08:38 CST 2017]; root of context hierarchy
十一月 04, 2017 10:08:38 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean-AutoWire.xml]
Person{name='周杰伦', car=Car{carName='五菱宏光', carPrice='300万'}, address=Address{country='中国'}}
Process finished with exit code 0
自动装配 → byType
<?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"
default-autowire="byType">
<!-- ↑↑↑
byName:通过名称进行自动匹配;
byType:根据类型进行自动匹配;-->
<bean id="car" class="com.ruge.testAutoWire.Car">
<property name="carName" value="五菱宏光"></property>
<property name="carPrice" value="300万"></property>
</bean>
<bean id="address" class="com.ruge.testAutoWire.Address">
<property name="country" value="中国"></property>
</bean>
<bean id="person" class="com.ruge.testAutoWire.Person">
<property name="name" value="周杰伦"></property>
</bean>
</beans>
D:\download\tool\jdk\jdk8_64\install\bin\java -Didea.launcher.port=7534 -Didea.launcher.bin.path=D:\download\tool\ide\idea\2016\ideaIU-2016\install\bin -Dfile.encoding=UTF-8 -classpath D:\download\tool\jdk\jdk8_64\install\jre\lib\charsets.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\deploy.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\access-bridge-64.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\cldrdata.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\dnsns.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\jaccess.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\jfxrt.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\localedata.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\nashorn.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunec.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunjce_provider.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunmscapi.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunpkcs11.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\zipfs.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\javaws.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jce.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jfr.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jfxswt.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jsse.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\management-agent.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\plugin.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\resources.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\rt.jar;D:\project_idea\spring\target\classes;D:\download\tool\versionControl\maven\localRepository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-beans\5.0.0.RELEASE\spring-beans-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-context\5.0.0.RELEASE\spring-context-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-aop\5.0.0.RELEASE\spring-aop-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-core\5.0.0.RELEASE\spring-core-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-jcl\5.0.0.RELEASE\spring-jcl-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-expression\5.0.0.RELEASE\spring-expression-5.0.0.RELEASE.jar;D:\download\tool\ide\idea\2016\ideaIU-2016\install\lib\idea_rt.jar com.intellij.rt.execution.application.AppMain com.ruge.testAutoWire.test
十一月 04, 2017 10:10:29 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5387f9e0: startup date [Sat Nov 04 10:10:29 CST 2017]; root of context hierarchy
十一月 04, 2017 10:10:29 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean-AutoWire.xml]
Person{name='周杰伦', car=Car{carName='五菱宏光', carPrice='300万'}, address=Address{country='中国'}}
Process finished with exit code 0