Spring bean管理器 bean实例化的三种方式
bean实例化的三种方式实现
第一种:使用类的无参数构造方法创建(常用 重要)
第一种实例化方式最常用,实例化类时会通过调用无参构造方法创建。示例代码如下:
package spring.com.UserService;
public class UserService {
public UserService() {
//该方法是无参方法
}
public void AddUser(){
System.out.println("Add.........................");
}
}
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" 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 id="userService" class="spring.com.UserService.UserService"></bean>
</beans>
单元测试:
package spring.com.Test; import static org.junit.Assert.*; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.com.UserService.UserService; public class Test { @org.junit.Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService s=(UserService) context.getBean("userService");
s.AddUser();
} }
结果:

方法正常执行
如果手动添加一个有参构造方法时,那么创建时会找不到无参构造方法;示例代码:
package spring.com.UserService;
public class UserService {
public UserService(String name) {
//该方法是有参方法
}
public void AddUser(){
System.out.println("Add.........................");
}
}
结果:

找不到无参构造方法,所以无法实例化。必须手动添加无参构造方法
package spring.com.UserService;
public class UserService {
public UserService() {
//该方法是无参方法
}
public UserService(String name) {
//该方法是有参方法
}
public void AddUser(){
System.out.println("Add.........................");
}
}
第二种:使用静态工厂创建(知道即可)
示例代码:
创建一个工厂类,改工厂用来创建一个类的实例
package spring.com.UserService;
public class BeanFactory {
public static UserService getBeanFactory(){
return new UserService();
}
}
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" 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 id="factory" class="spring.com.UserService.BeanFactory" factory-method="getBeanFactory"></bean><!--创建工厂创建对象-->
</beans>
单元测试:
package spring.com.Test; import static org.junit.Assert.*; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.com.UserService.BeanFactory;
import spring.com.UserService.UserService; public class Test { @org.junit.Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService s=(UserService) context.getBean("factory");
s.AddUser();
} }
结果:

第三种:使用实例工厂创建(知道即可)
示例代码:
package spring.com.UserService;
public class BeanFactory {
//普通工厂方法
public UserService getBeanFactory(){
return new UserService();
}
}
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" 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 id="bean" class="spring.com.UserService.BeanFactory"></bean>
<bean id="factory" factory-bean="bean" factory-method="getBeanFactory"></bean>
</beans>
单元测试:
package spring.com.Test; import static org.junit.Assert.*; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import spring.com.UserService.BeanFactory;
import spring.com.UserService.UserService; public class Test { @org.junit.Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService s=(UserService) context.getBean("factory");
s.AddUser();
} }
结果:

Spring bean管理器 bean实例化的三种方式的更多相关文章
- 【Spring】的【bean】管理(XML配置文件)【Bean实例化的三种方式】
Bean实例化的三种方式 说明:通过配置文件创建对象就称为Bean实例化. 第一种:使用类的无参构造创建(重点) 实体类 package com.tyzr.ioc; public class User ...
- Spring中bean实例化的三种方式
之前我已经有好几篇博客介绍Spring框架了,不过当时我们都是使用注解来完成注入的,具体小伙伴可以参考这几篇博客(Spring&SpringMVC框架案例).那么今天我想来说说如何通过xml配 ...
- Spring的依赖注入(DI)三种方式
Spring依赖注入(DI)的三种方式,分别为: 1. 接口注入 2. Setter方法注入 3. 构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...
- spring学习(03)之bean实例化的三种方式
bean实体例化的三种方式 在spring中有三中实例化bean的方式: 一.使用构造器实例化:(通常使用的一个方法,重点) 二.使用静态工厂方法实例化: 三.使用实例化工厂方法实例化 第一种.使用构 ...
- Spring实现初始化和销毁bean之前进行的操作,三种方式
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- Bean实例化的三种方式
1. 构造器实例化 spring容器通过bean对应的默认的构造函数来实例化bean. 2. 静态工厂方式实例化 首先创建一个静态工厂类,在类中定义一个静态方法创建实例. 静态工厂类及静态方法: pu ...
- Spring AOP编程(二)-AOP实现的三种方式
AOP的实现有三种方式: l aop底层将采用代理机制进行实现. l 接口 + 实现类 :spring采用 jdk 的动态代理Proxy. l 实现类: ...
- 【spring】 SpringMVC返回json数据的三种方式
配置方法一 **1.导入第三方的jackson包,jackson-mapper-asl-1.9.7.jar和jackson-core-asl-1.9.7.jar. 2.spring配置文件添加** & ...
- Spring配置文件-Bean实例化的三种方式
1.无参构造方法实例化(详见我的博客) 2.工厂静态方法实例化 创建StaticFactory类 public class StaticFactory { public static UserDao ...
随机推荐
- POJ1159解题心得
题目:http://poj.org/problem?id=1159 刚开始,从样例的特征去思考.总让我从回文数的角度去思考,想出几个方案,可都用了数据去检验,发现不行.如:ABCDDCB,BACDCA ...
- NodeJS写模块和引入模块的例子
nodejs自学.js function hello(){ console.log("hello world");} function s(){ console.log(" ...
- 华为交换机S5700 vty 0 4
最佳答案 vty 0 4 代表有5条VTY线路 由0到4 这个要看设备的版本有的设备会有更多条VTY线路比如一些企业版的设备 显示用户界面 进入TELNET 设置模式 user-interface ...
- 一分钟学会 ConstraintLayout 之从属性角度理解布局
ConstraintLayout 在 Android 开发中,我们通常是手写布局,很少会用拖动来写布局,虽然 ConstraintLayout 在 I/O 上以拖动来展现了各种功能,我估计在以后开发中 ...
- Bilateral Filtering(双边滤波) for SSAO
原网址:http://blog.csdn.net/bugrunner/article/details/7170471 1. 简介 图像平滑是一个重要的操作,而且有多种成熟的算法.这里主要简单介绍一下B ...
- 关于data-属性
关于data-属性 现有需求如下,也就是类似做一个tab页的切换如下图: 因为这里要记录一下jquery里的“data-属性”的用法,所以忽略类似的组件. 往HTML标签上添加任意以 "da ...
- nginx实现多个域名共享80端口
server { listen 80; server_name server8085.duchong.cn; location / { proxy_pass http://127.0.0.1:8085 ...
- Shell32.ShellClass服务器操作系统无法获取 音频文件时长问题
前言: 上传音频文件,自动写入此音频文件的时长,这里用 COM组件Microsoft Shell Controls And Automation来实现. 首先 1.引用:Microsoft Shell ...
- 从socket开始讲IOS网络编程
home list tags talk user rss Mac&iOS Socket 大纲 一.Socket简介 二.BSD Socket编程准备 1.地址 2.端口 3.网络字节序 4.半 ...
- zt对于C#中的FileUpload解决文件上传大小限制的问题设置
对于C#中的FileUpload解决文件上传大小限制的问题设置 您可能没意识到,但对于可以使用该技术上载的文件的大小存在限制.默认情况下,使用 FileUpload 控件上载到服务器的文件最大为 4M ...