您的位置:首页 > 编程语言 > Java开发

Spring的三种实例化bean方式

2017-04-19 23:17 736 查看
spring提供了容器,便于替代我们管理bean对象,这容器提供了三种实例化bean的方式,下面就根据代码具体分析这三种方式

需要导入的包:spring.jar commmon-logging.jar

1,最常用的方式:构造函数实例化bean

<bean id="personService"
class="yanxi.service.impl.PersonServiceBean">


下面根据具体的代码说明这种方式

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"   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="personService" class="yanxi.service.implements.PersonServiceBean"></bean>
<!-- 构造函数实例化bean -->


springTest.java//测试单元

package junit.test;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import yanxi.service.PersonService;

public class SpringTest {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@Test public void instanceSpring(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService)ctx.getBean("personService");
personService.save();
}
}


PersonServiceBean.java

package yanxi.service.implements;

import yanxi.service.PersonService;

public class PersonServiceBean implements PersonService {

public void save(){
System.out.println("我是save()方法");
}
}


PersonService.java在写java程序的时候,最好将bean实现接口,将接口包放到外部包中,规范化代码

package yanxi.service;

public interface PersonService {

public void save();

}


运行结果

我是save()方法


这种实例化bean的方式使我们用的比较多的,也是最经常用的,建议使用

2,静态工厂方法实例化

要写两个bean,一个是要实例化的bean,另一个是工厂bean。容器调用工厂bean配置项factory-method中指定的方法,在方法中实例化bean

在上面代码的基础上进行更改

更改配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="personService" class="yanxi.service.implements.PersonServiceBeanFactory"
factory-method="createPersonServiceBean"/>
</beans


创建PersonServiceBeanFactory.java

package yanxi.service.implements;
public class PersonServiceBeanFactory {
public static PersonServiceBean createPersonServiceBean(){
return new PersonServiceBean();
}
}


3,使用实力工厂的方法实例化bean对象

<?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-2.5.xsd"> <bean id="personServiceFactory" class="yanxi.service.implements.PersonServiceBeanFactory"/>
<bean id="personService" factory-bean="personServiceFactory" factory-method="createPersonServiceBean2"/>
</beans>


与静态工厂实例化bean的区别

public PersonServiceBean createPersonServiceBean2(){
return new PersonServiceBean();
}


一个是静态的创建bean方法,一个是重写之后的非静态的bean方法

推荐使用第一种方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring bean