//Dao类

package com.dao.bean.www;

public interface PersonServiceDao {

	public abstract void save();

}

//Bean

package com.bean.www;

import com.dao.bean.www.PersonServiceDao;

public class PersonServiceBean implements PersonServiceDao {

	@Override
public void save() {
System.out.println("method save()");
}
}

//第二第三种需要的工厂方法

package com.factory.www;

import com.bean.www.PersonServiceBean;

public class PersonServiceFactory {
public static PersonServiceBean creatBean() {
return new PersonServiceBean();
} public PersonServiceBean creatBean2() {
return new PersonServiceBean();
} }

//配置文件

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 获取bean -->
<bean id="personService" class="com.bean.www.PersonServiceBean"></bean> <!-- 静态工厂方法 -->
<bean id="beanFactory1" class="com.factory.www.PersonServiceFactory" factory-method="creatBean" ></bean> <!-- 非静态工厂方法 1.首先实例化工厂类bean -->
<bean id="beanFactory2" class="com.factory.www.PersonServiceFactory" ></bean>
<bean id="GetBean" factory-bean="beanFactory2" factory-method="creatBean2" ></bean>
</beans>

//测试类

package com.itcast.www;

import static org.junit.Assert.*;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dao.bean.www.PersonServiceDao; public class TestCaseDemo { @BeforeClass
public static void setUpBeforeClass() throws Exception {
} @Test
public void instanceSpring() { ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml"); // PersonServiceDao personService = (PersonServiceDao) ctx
// .getBean("personService");
// PersonServiceDao personService = (PersonServiceDao) ctx
// .getBean("beanFactory1");
PersonServiceDao personService = (PersonServiceDao) ctx
.getBean("GetBean");
personService.save(); } }

//************************初始化和构造函数******************************

package com.bean.www;

import com.dao.bean.www.PersonServiceDao;
/*
* 初始化方法执行在构造方法之后
* 需要在配置文件中配置初始化或者销毁方法
* 用于打开或者关闭资源等
* 单实例-lazy-init=true 获取容器后创建
* lazy-init=false ctx.getbean实现
* 直到关闭才执行destroy方法
* 关闭方法 ctx.close();
* */
public class PersonServiceBean implements PersonServiceDao { public void init(){
System.out.println("init..................");
} @Override
public void save() {
System.out.println("method save()");
} public void destroy(){
System.out.println("destroy..................");
}
}

//配置文件

<bean id="personService" class="com.bean.www.PersonServiceBean" lazy-init="false" init-method="init" destroy-method=""></bean>

java_spring_实例化bean的3种方法的更多相关文章

  1. Spring Ioc源码分析系列--容器实例化Bean的四种方法

    Spring Ioc源码分析系列--实例化Bean的几种方法 前言 前面的文章Spring Ioc源码分析系列--Bean实例化过程(二)在讲解到bean真正通过那些方式实例化出来的时候,并没有继续分 ...

  2. 实例化Bean的几种方法

    1.使用构造器实例化Bean.         当没有指定实例化方法时,Spring IoC容器能使用默认空构造器.构造器实例化包括默认空构造器和有参数构造器两种方式创建Bean. 2.使用构造器实例 ...

  3. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring实例化Bean的三种方法

    在面向对象的程序中,要想调用某个类的成员方法,就需要先实例化该类的对象.在 Spring 中,实例化 Bean 有三种方式,分别是构造器实例化.静态工厂方式实例化和实例工厂方式实例化. 构造器实例化 ...

  4. Spring实例化bean的三种方法

    1.用构造器来实例化 <bean id="hello2" class="com.hsit.hello.impl.ENhello" /> 2.使用静态 ...

  5. Spring、实例化Bean的三种方法

    1.使用类构造器进行实例化 <bean id="personIService" class="cn.server.impl.PersonServiceImpl&qu ...

  6. Spring学习之实例化bean的三种方式

    实例化bean的三种方式 构造器实例化bean Person.java public class Person { private String name; private Integer age; ...

  7. Spring实例化Bean的三种方式及Bean的类型

    1.使用类构造器实例化  [默认的类构造器] <bean id=“orderService" class="cn.itcast.OrderServiceBean"/ ...

  8. 普通Java类获取spring 容器的bean的5种方法

    方法一:在初始化时保存ApplicationContext对象方法二:通过Spring提供的工具类获取ApplicationContext对象方法三:继承自抽象类ApplicationObjectSu ...

  9. spring注入bean的三种方法

    在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...

随机推荐

  1. 导出Excel帮助类

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.D ...

  2. HDU 5007 Post Robot

    Post Robot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  3. 《深入Java虚拟机学习笔记》- 第7章 类型的生命周期/对象在JVM中的生命周期

    一.类型生命周期的开始 如图所示 初始化时机 所有Java虚拟机实现必须在每个类或接口首次主动使用时初始化: 以下几种情形符合主动使用的要求: 当创建某个类的新实例时(或者通过在字节码中执行new指令 ...

  4. 编程精粹:编写高质量的C语言代码———笔记一

    第一章 假想的编译程序 要记得对空语句进行处理,最好使用NULL使其明显可见 char * strcpy(char* pchTo, char* pchFrom) { char* pchStart = ...

  5. HDU 5618 Jam's problem again CDQ分治 BC ROUND 70

    题意:给你1e5个点(x,y,z),对于每一个点询问有多少个点(x1,y1,z1)满足x1<=x&&y1<=y&&z1<=z 分析:(官方题解奉上)很 ...

  6. Linux批量修改指定目录下的文件或文件夹权限

    在Puppet下很头大,尤其是文件拷贝,使用file的mode会导致文件或文件夹都一个权限. 暂时使用命令代替: 最近忙着明年的N多计划,待有空后继续研究.

  7. Express细节探究(1)——app.use(express.static)

    express相信是很多人用nodejs搭建服务器的首选框架,相关教程有很多,也教会了大家来如何使用.如果你想更深的了解他的细节,不妨和我一起来研究一下. 先来看一个每个人都用到的方法app.use( ...

  8. algorithm@ 大素数判定和大整数质因数分解

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<time.h> #in ...

  9. HDU-4655 Cut Pieces 数学,贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4655 先不考虑相临的有影响,那么总数就是n*prod(ai),然后减去每个相邻的对总数的贡献Σ( Mi ...

  10. SRM 599 DIV 2

    rating又掉了...变灰色了%>_<%.250pt很简单,一眼看上去是个背包,没有多想立马写了个01背包,后面发现其实就是个简单的排序...因为只是需要求数量而已.500pt被我写残了 ...