IOC容器实例化对象的几种方式:
- 调用无参构造
- 调用有参构造
- 调用其他对象构造
- 调用工厂类静态方法
- 调用工厂类非静态方法
下面上代码:
applicationConfig.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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 无参调用 -->
<!-- <bean id="user" class="com.iotek.first.User"></bean> -->
<!-- 带参调用 -->
<!-- <bean id="user2" class="com.iotek.first.User">
<constructor-arg value="101"></constructor-arg>
<constructor-arg value="Tom"></constructor-arg>
</bean> -->
<!-- 调用其他对象 -->
<!-- <bean id="name" class="java.lang.String">
<constructor-arg value="Jack"></constructor-arg>
</bean>
<bean id="user3" class="com.iotek.first.User">
<constructor-arg type="java.lang.String" ref="name" index="1"></constructor-arg>
<constructor-arg value="102" type="int" index="0"></constructor-arg>
</bean> -->
<!-- 调用工厂类的静态方法 -->
<!-- <bean id="UserFactory" class="com.iotek.first.UserFactory">
</bean>
<bean id="user" factory-bean="UserFactory" factory-method="getUser"></bean> -->
<!-- 调用工厂类的实例方法 -->
<bean id="UserFactory" class="com.iotek.first.UserFactory"></bean>
<bean id="user" class="com.iotek.first.UserFactory" factory-method="getStaticUser"></bean>
</beans>