一当悔、構(gòu)造器實(shí)例化
構(gòu)造器實(shí)例化是指Spring容器通過Bean對應(yīng)類中默認(rèn)的無參構(gòu)造方法來實(shí)例化Bean.
通過一個簡單的例子來訓(xùn)練区丑。
1.在搭建好的項(xiàng)目中的src下創(chuàng)建一個con.spring.constructor包挠轴。在包下創(chuàng)建一個類? ? ? ? ? ? ??Bean1:??
? ?public class Bean1 {??
? ?}
2.在con.spring.constructor包下創(chuàng)建配置文件aplicationContext.xml.
<?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"
? ? xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="bean1" class="alern.spring.constructor.Bean1"></bean>
</beans>
3.創(chuàng)建測試類Test01
package alern.spring.constructor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test01 {
public static void main(String [] args){
String xmlPath = "alern/spring/constructor/aplicationContext.xml";
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext(xmlPath);
Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");
System.out.println(bean1);
}
}
二诵肛、靜態(tài)工廠方式實(shí)例化
1.?在alern.spring.staticfactory包中創(chuàng)建類Bean2.類中沒有方法
2.在alern.spring.staticfactory包下創(chuàng)建類StaticFactory跑芳,并創(chuàng)建方法返回Bean2得的實(shí)例:
? ? ?public class StaticFactory {
? ? ? public static Bean2 createBean(){
? ? ? return new Bean2();
? ? ?}
? ? ?}
3.在alern.spring.staticfactory包下創(chuàng)建Bean2.xml:
<bean id="bean2" class="alern.spring.staticfactory.StaticFactory"
factory-method="createBean"></bean>
factory-method屬性告訴容器是哪個工廠方法突照。
4.創(chuàng)建測試類Test02:
package alern.spring.staticfactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import alern.spring.constructor.Bean1;
public class Test02 {
public static void main(String [] args){
String xmlPath = "alern/spring/staticfactory/bean2.xml";
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext(xmlPath);
Bean2 bean2 = (Bean2) applicationContext.getBean("bean2");
System.out.println(bean2);
}
}
三帮非、使用實(shí)例工廠方式實(shí)例化
此種方式的工廠類中,不在使用靜態(tài)方法創(chuàng)建Bean實(shí)例,而是直接采用創(chuàng)建Bean實(shí)例的方式讹蘑。同時在配置文件中末盔,也不是由class直接指向?qū)嵗悾峭ㄟ^factory-bean指向配置的實(shí)例工廠座慰,factory-method確定工廠使用的是哪個方法陨舱。
1.在alern.spring.instancefactory包下創(chuàng)建類Bean3,也沒有方法版仔。
2.在alern.spring.instancefactory包下創(chuàng)建類Bean3facotury:
package alern.spring.instancefactory;
public class Bean3facotury {
public Bean3 myfactory(){
return new Bean3();
}
}
3.在alern.spring.instancefactory包下創(chuàng)建bean3,xml:
<?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"
? ? xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="factory" class="alern.spring.instancefactory.Bean3facotury"></bean>
<!-- 使用 factory-bean屬性指向配置的實(shí)例工廠-->
<!-- 使用factory-method屬性來確定使用工廠的哪個方法 -->
<bean id="bean3" factory-bean="factory" factory-method="myfactory"></bean>
</beans>
4.創(chuàng)建測試類Test3:
package alern.spring.instancefactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test03 {
public static void main(String [] args){
String xmlPath = "alern/spring/instancefactory/bean3.xml";
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext(xmlPath);
Bean3 bean3 = (Bean3) applicationContext.getBean("bean3");
System.out.println(bean3);
}
}
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?學(xué)習(xí)書籍:《javaEE企業(yè)級開發(fā)教程》