通过在spring配置文件中直接配置类全路径,spring会自动调用类的无参数构造方法实例化该类。
SpringBean.java
package com.sdnu.spring6.bean;
public class SpringBean {
public SpringBean() {
System.out.println("SpringBean的无参构造方法执行");
}
}
Spring配置文件
<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="sb" class="com.sdnu.spring6.bean.SpringBean"/>
beans>
测试
package com.sdnu.spring6.test;
import com.sdnu.spring6.bean.SpringBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanInstantiationTest {
@Test
public void testInstantiation1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
SpringBean sb = applicationContext.getBean("sb", SpringBean.class);
System.out.println(sb);
}
}
需要在spring的配置文件中告诉spring,调用哪个类的哪个方法获取Bean
class告诉spring调用哪个类
factory-method告诉spring调用哪个方法
Star.java
package com.sdnu.spring6.bean;
public class Star {
public Star() {
System.out.println("Star的无参构造方法被调用");
}
}
StarFactory.java
package com.sdnu.spring6.bean;
public class StarFactory {
/**
* 静态方法
* @return Start
*/
public static Star get(){
return new Star();
}
}
spring配置文件
<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="sb2" class="com.sdnu.spring6.bean.StarFactory" factory-method="get"/>
beans>
测试
@Test
public void testStarFactory(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Star sb2 = applicationContext.getBean("sb2", Star.class);
System.out.println(sb2);
}
通过factory-bean和factory-method属性共同完成。
Gun.java
package com.sdnu.spring6.bean;
public class Gun {
public Gun() {
System.out.println("Gun的无参构造方法执行");
}
}
GunFactory.java
package com.sdnu.spring6.bean;
public class GunFactory {
public Gun get(){
return new Gun();
}
}
Spring配置文件
<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="gunFactory" class="com.sdnu.spring6.bean.GunFactory"/>
<bean id="gun" factory-bean="gunFactory" factory-method="get"/>
beans>
测试
@Test
public void testInstantiation3(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Gun gun = applicationContext.getBean("gun", Gun.class);
System.out.println(gun);
}
实际上是上面方法3的一种简化。
FactoryBean在Spring中是一个接口。被称为“工厂Bean”。“工厂Bean”是一种特殊的Bean。所有的“工厂Bean”都是用来协助Spring框架来创建其他Bean对象的。
Person.java
package com.sdnu.spring6.bean;
public class Person {
public Person() {
System.out.println("Person的无参构造方法被执行");
}
}
PersonFactoryBean
package com.sdnu.spring6.bean;
import org.springframework.beans.factory.FactoryBean;
public class PersonFactoryBean implements FactoryBean<Person> {
@Override
public Person getObject() throws Exception {
return new Person();
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return FactoryBean.super.isSingleton();
}
}
spring配置文件
<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="person" class="com.sdnu.spring6.bean.PersonFactoryBean"/>
beans>
测试
@Test
public void testInstantiation4(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Person person = applicationContext.getBean("person", Person.class);
System.out.println(person);
}
Spring IoC容器的顶级对象,BeanFactory被翻译为“Bean工厂”,在Spring的IoC容器中,“Bean工厂”负责创建Bean对象。
BeanFactory是工厂。
FactoryBean:它是一个Bean,是一个能够辅助Spring实例化其它Bean对象的一个Bean。
在Spring中,Bean可以分为两类:
● 第一类:普通Bean
● 第二类:工厂Bean(记住:工厂Bean也是一种Bean,只不过这种Bean比较特殊,它可以辅助Spring实例化其它Bean对象。)