1.通过类的构造方法构建对象
2.通过静态工厂方法构建对象
3.通过动态工厂方法构建对象
Spring中的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd" >
<!-- 直接通过类的构建方法构建对象 class为包名+类名-->
<bean id="date" class="java.util.Date"/>
<!-- 通过类的静态工厂方法创建对象 -->
<!--用于此类不能被直接实例化时,一般为抽象类-->
<!--factory-method中是此类的静态方法,通过此静态方法返回的对象获得一个对象-->
<bean id="calendar"
class="java.util.Calendar"
factory-method="getInstance"
/>
<!-- 通过Calendar对象的getTime方法获取date对象 动态工厂方法 动态 -->
<bean id="time"
factory-bean="calendar"
factory-method="getTime"/>
<!--注意:动态工厂方法比静态工厂方法多个factory-bean标签,这个标签指的是需要的那个bean对象的id值,用这个bean创建按另一个bean的对象-->
</beans>