computer2.run();
//default
Computer computer3 = (Computer)ac.getBean("computer3");
System.out.println("autowire=\"default\":");
computer3.run();
//autodetect
Computer computer4 = (Computer)ac.getBean("computer4");
System.out.println("autowire=\"autodetect\":");
computer4.run();
//constructor
Computer computer5 = (Computer)ac.getBean("computer5");
System.out.println("autowire=\"constructor\":");
computer5.run();
//no
Computer computer6 = (Computer)ac.getBean("computer6");
System.out.println("autowire=\"no\":");
computer6.run();
log.debug("leaving main method");
}
}
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-autowire="autodetect">
<bean id="host" name="host" class="com.redoffice.Host"></bean>
<bean id="display" name="display" class="com.redoffice.Display"></bean>
<!-- autowireóDno,constructor,byName,byType,default,autodetect????×′?? -->
<bean id="computer1" class="com.redoffice.Computer" autowire="byName"></bean>
<bean id="computer2" class="com.redoffice.Computer" autowire="byType"></bean>
<bean id="computer3" class="com.redoffice.Computer" autowire="default"></bean>
<bean id="computer4" class="com.redoffice.Computer" autowire="autodetect"></bean>
<!-- ?úComputeràà?D±?D?óDò????T2?oíóD2?μ?11?ìoˉêy£?·??ò±¨′í -->
<bean id="computer5" class="com.redoffice.Computer" autowire="constructor">
<constructor-arg index="0">
<ref bean="host"/>
</constructor-arg>
<constructor-arg index="1">
<ref bean="display"/>
</constructor-arg>
</bean>
<bean id="computer6" class="com.redoffice.Computer" autowire="no">
<constructor-arg index="0">
<ref bean="host"/>
</constructor-arg>
<constructor-arg index="1">
<ref bean="display"/>
</constructor-arg>
</bean>
</beans>
?£í?óDê2?′2???μ?μ?·??à?à???ì?£ |