传智播客Spring2.5视频教程_Spring的三种实例化Bean的方式 1

来源:互联网 发布:好看的韩国网络剧 编辑:程序博客网 时间:2024/05/04 15:36

这节课 很短,8分钟左右。但是,丛开始我的疑问就一直没有解决:三种方法的应用场合是什么? 差别在那里? 老师只是说 通过构造方法实例化 占大多数。,我查一下,看看有什么说法?

1 构造函数

2 静态工厂

3 实例工厂

 

配置文件"/three/threebeans.xml"

           <!--1 -->
           <bean id="personService" class="three.spring.service.impl.PersonServiceBean"></bean>

 

           <!--2 -->
           <bean id="personService2" class="three.spring.service.impl.PersonServiceBeanStaticFaction"
           factory-method="getPersonService"></bean>
          
           <!--3 -->
           <bean id="personServiceBeanFaction" class="three.spring.service.impl.PersonServiceBeanFaction"></bean>
           <bean id = "personService3" factory-bean="personServiceBeanFaction" factory-method="getPersonService"></bean>

 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package three.spring.junit;


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

import two.spring.service.PersonService;


public class SpringTest {

 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }
 @Test
 public void instanceSpring()  {
  
  // 在类路径下,寻找配置文件,实例化spring容器。
  ApplicationContext ctx = new ClassPathXmlApplicationContext("/three/threebeans.xml");
  
  
  //配置文件中bean的id,推荐首字母小写
  PersonService personService = (PersonService)ctx.getBean("personService3");
  
  personService.savePerson();
 }

}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package three.spring.service;

public interface PersonService {

 //面向接口编程,降低耦合度
 public abstract void savePerson();

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package three.spring.service.impl;

import two.spring.service.PersonService;

//1 构造函数
public class PersonServiceBean implements PersonService {

  
 public void savePerson() {
  System.out.println("我是包[two.spring.service.impl]的" +"类[PersonService]的[savePerson]方法");
  
 }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package three.spring.service.impl;

//3 实例工厂
public class PersonServiceBeanFaction {
 
 public PersonServiceBean getPersonService(){
  return new PersonServiceBean();  
 }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package three.spring.service.impl;

//2 静态工厂
public class PersonServiceBeanStaticFaction {
 
 public static PersonServiceBean getPersonService(){
  return new PersonServiceBean();  
 }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

原创粉丝点击