2.springioc实例化bean的三个方法
1.构造器
也就是在上一篇讲的那个例子,调用默认的无参构造函数
2.静态工厂方法
1)创建需要执行的方法的类
public class HelloWorld {
public HelloWorld(){
System.out.println("aaaa");
}
public void hello(){
System.out.println("hello world");
}
}
2)创建静态工厂
public class HelloWorldFactory {
public static HelloWorld getInstance(){
return new HelloWorld();
}
}
3)编写applicationContext.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">
<!--
在这个配置中,spring容器要用默认的构造函数为HelloWorld创建对象
-->
<bean id="helloWorld" class="HelloWorld"></bean> <!--
采用静态工厂方法创建对象
factory-method为工厂方法
-->
<bean id="helloWorld2" class="HelloWorldFactory" factory-method="getInstance"></bean>
</beans>
4)启动容器,创建对象,调用方法
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld world = (HelloWorld)context.getBean("helloWorld2");
world.hello();
}
3.实例工厂方法(略)
2.springioc实例化bean的三个方法的更多相关文章
- Spring实例化bean的三种方法
1.用构造器来实例化 <bean id="hello2" class="com.hsit.hello.impl.ENhello" /> 2.使用静态 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring实例化Bean的三种方法
在面向对象的程序中,要想调用某个类的成员方法,就需要先实例化该类的对象.在 Spring 中,实例化 Bean 有三种方式,分别是构造器实例化.静态工厂方式实例化和实例工厂方式实例化. 构造器实例化 ...
- Spring、实例化Bean的三种方法
1.使用类构造器进行实例化 <bean id="personIService" class="cn.server.impl.PersonServiceImpl&qu ...
- Spring学习之实例化bean的三种方式
实例化bean的三种方式 构造器实例化bean Person.java public class Person { private String name; private Integer age; ...
- Spring Ioc源码分析系列--容器实例化Bean的四种方法
Spring Ioc源码分析系列--实例化Bean的几种方法 前言 前面的文章Spring Ioc源码分析系列--Bean实例化过程(二)在讲解到bean真正通过那些方式实例化出来的时候,并没有继续分 ...
- Spring:Spring-IOC实例化bean的常用三种方式
Spring容器提供了三种对bean的实例化方式: 1)构造器实例化 public class Demo { private String name; //getter和setter方法略 } < ...
- Spring实例化Bean的三种方式及Bean的类型
1.使用类构造器实例化 [默认的类构造器] <bean id=“orderService" class="cn.itcast.OrderServiceBean"/ ...
- Spring 实例化bean的三种方式:
方法一:使用构造器实例化bean java代码: package com.model; public class User { private String username; public User ...
- spring注入bean的三种方法
在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...
随机推荐
- 42.oracle物化视图
写在前面 先大概列一下数据库表设计的常规流程.方案.要遵循的规则 根据业务切分设计表 逻辑分层(数据库分层) 数据库结构设计与拆分:水平拆分(mysql分片)oracle分区物化视图中间表设计方案 优 ...
- mysql导入大批量数据出现MySQL server has gone away的解决方法
因工作需要,需要导入一个200M左右的sql到user库 执行命令 mysql> use user Database changed mysql> source /tmp/user.sql ...
- linux 编译PHP memcache扩展
在Linux下编译memcache:memcache官网:http://memcached.org/前期准备:如果是虚拟机 保证虚拟机 联网安装依赖包yum -y install gcc make l ...
- c语言中变量/函数命名以单下划线(_)和双下划线(__) 开头的意义
以单下划线(_)表明是标准库的变量 双下划线(__) 开头表明是编译器的变量 建议自己在命名的时候不要用下划线开头,避免与标准库中的命名冲突 命名方法有好多,何必为自己找不自在呢.
- 使用mint-ui的 InfiniteScroll 做数据分页请求
1.首先 npm install mint-ui 2.在main.js引用 import { InfiniteScroll } from 'mint-ui'; Vue.use(InfiniteScro ...
- 响应式Web设计-一种优雅的掌上展现
入门 flat - style (too many ad.) writeshell
- WebGIS简单实现一个区域炫酷的3D立体地图效果
1.别人的效果 作为一个GIS专业的,做一个高大上的GIS系统一直是我的梦想,虽然至今为止还没有做出一个理想中的系统,但是偶尔看看别人做的,学习下别人的技术还是很有必要的.眼睛是最容易误导我们的,有时 ...
- pyserial timeout=1 || timeout=0.01
昨天在做串口通信时候发现,串口参数(timeout=1 || timeout=0.01)对通信的读数据竟然影响很大,代码如下: self.ser = serial.Serial(port=serial ...
- spring boot快速入门 9: 单元测试
进行单元测试: service第一种方式: 第一步:在指定service中创建一个方法进行测试 /** * 通过ID查询一个女生的信息 * @param id * @return */ public ...
- phpqrcode实现二维码(含图片)
---恢复内容开始--- 1,http://phpqrcode.sourceforge.net/ 下载 2,解压以后只需要一个文件 3,原生php测试: <?php include 'phpqr ...