Spring的三种实例化bean方式
2017-04-19 23:17
736 查看
spring提供了容器,便于替代我们管理bean对象,这容器提供了三种实例化bean的方式,下面就根据代码具体分析这三种方式
需要导入的包:spring.jar commmon-logging.jar
1,最常用的方式:构造函数实例化bean
下面根据具体的代码说明这种方式
xml配置文件
springTest.java//测试单元
PersonServiceBean.java
PersonService.java在写java程序的时候,最好将bean实现接口,将接口包放到外部包中,规范化代码
运行结果
这种实例化bean的方式使我们用的比较多的,也是最经常用的,建议使用
2,静态工厂方法实例化
要写两个bean,一个是要实例化的bean,另一个是工厂bean。容器调用工厂bean配置项factory-method中指定的方法,在方法中实例化bean
在上面代码的基础上进行更改
更改配置xml文件
创建PersonServiceBeanFactory.java
3,使用实力工厂的方法实例化bean对象
与静态工厂实例化bean的区别
一个是静态的创建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的三种实例化方式
- Spring:三种实例化bean的方式
- spring bean实例化的三种方式
- spring三种实例化bean的方式
- Spring三种实例化Bean的方式
- spring三种实例化bean的方式
- Spring之旅--Spring 实例化bean的三种方式
- [1] Spring中的Bean实例化的三种方式
- Spring中bean实例化的三种方式
- 详解Spring中bean实例化的三种方式
- Spring三种实例化Bean的方式
- spring三种实例化bean的方式
- Spring三种实例化Bean的方式
- Spring三种实例化bean的方式
- 一步一步深入spring(2)-三种方式来实例化bean
- Spring 三种实例化Bean的方式
- Spring2.5学习1.3_三种实例化bean的方式
- Spring 实例化bean的三种方式:
- Spring中实例化Bean的三种方式
- spring之bean的三种实例化方式