首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring<1>bean实例化

2012-09-28 
spring1----bean实例化PersonService.javapackage cn.anycall.servicepublic interface PersonService

spring<1>----bean实例化
PersonService.java

package cn.anycall.service;public interface PersonService {public abstract void save();}

PersonServiceBean.java
package cn.anycall.service.impl;import cn.anycall.service.PersonService;public class PersonServiceBean implements PersonService {public void save(){System.out.println("我是save()");}}


PersonServiceBeanFactory.java
package cn.anycall.service.impl;public class PersonServiceBeanFactory {public static PersonServiceBean createPersonServiceBean(){return new PersonServiceBean();}public PersonServiceBean createPersonServiceBean2(){return new PersonServiceBean();}}


beans.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 name="personService" factory-method="createPersonServiceBean"></bean>    <!-- 使用实例工厂方法实例化 -->  <bean name="personServiceFactory" factory-bean="personServiceFactory" factory-method="createPersonServiceBean2"></bean></beans>


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 cn.anycall.service.PersonService;public class SpringTest {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Test public void instanceSprint(){ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});PersonService persionService = (PersonService)ctx.getBean("personService");persionService.save(); persionService = (PersonService)ctx.getBean("personService2");persionService.save(); persionService = (PersonService)ctx.getBean("personService3");persionService.save();}}

热点排行