实例化Bean
- 方法一:构造方法实例化Bean
public class Service {
private Dao dao;
public Service(){
System.out.println("d das");
}
public void show(){
System.out.println("巴拉巴拉");
dao.show();
}
public void setDao(Dao dao){
this.dao=dao;
}
}
配置文件(property中的name是当前id类中的成员变量,ref是传进去的成员变量的参数,这里指的是id为Dao的类)
<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<bean id="Dao" class="Dao"></bean>
<bean id="Service" class="Service">
<property name="dao" ref="Dao"></property>
</bean>
</beans>
测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DayTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Day02.xml");
Service service=(Service) context.getBean("Service");
service.show();
}
}
结果
d das
巴拉巴拉
用空参构造方法创建,当然会默认有一个空参构造方法,如果你写了一个有参,则必须再添加一个空参构造方法
- 静态工厂
创建一个包含静态方法的工厂类(简称静态工厂类)
public class Factory {
public static face GetBean(){
return new Dao();
}
}
配置文件
<?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.xsd">
<bean id="dao" class="Factory" factory-method="GetBean"></bean>
</beans>
测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DayTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Day03.xml");
face f=(face) context.getBean("dao");
f.show();
}
}
原理:创建一个静态工厂,在测试类中通过new新的工厂类,又因为工厂类中有静态方法,所以在工厂类被加载的时候,静态方法也会默认加载,所以new了工厂之后,会把静态方法的返回值返回,于是就可以直接f.show()
注:静态方法的返回值类型也可以是一个具体类,我这里用的是接口罢了,为了降低耦合度
- 实例工厂
创建工厂类
public class Factory02 {
public face GetBean(){
return new Dao();
}
}
配置文件(先把工厂类bean出来,在把这个bean放在最终想要调用的bean的factory-bean上,再用factory-method标明工厂中的具体方法)
<?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.xsd">
<bean id="factory02" class="Factory02"></bean>
<bean id="dao" factory-method="GetBean" factory-bean="factory02"></bean>
</beans>
测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DayTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Day04.xml");
face f=(face) context.getBean("dao");
f.show();
}
}
原理:通过将工厂类的bean扔进最终需要的bean中,再通过测试类加载配置文件,获得 id 为dao的bean来以此实现实例化
注:同样的和静态工厂一样可以把返回值换成具体的类,理由都一样
- FactoryBean(类似实例化工厂的扩展)
创建实现FactoryBean接口的类
import org.springframework.beans.factory.FactoryBean;
public class Factory03 implements FactoryBean {
@Override
public face getObject() throws Exception {
return new Dao();
}
@Override
public Class<?> getObjectType() {
return face.class;
}
}
配置文件(解除了上面那个方法中配置文件的繁琐性,省去了不必要的代码,只用将实现FctoryBean的类放入容器即可,再在测试类中调用即可)
<?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.xsd">
<bean id="dao" class="Factory03"></bean>
</beans>
测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DayTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Day05.xml");
face f=(face) context.getBean("dao");
f.show();
}
}
注:实际项目中运用较多,比之上面方法