spring实例化bean三种方式
我看了这篇博文《https://www.cnblogs.com/zhanglei93/p/6221546.html》,以及自己实践总结了关于spring实例化bean对象的3种方式。
一、通过构造
1.1通过无参构造:
在加载applicationContext.xml文件的时候会通过无参构造实例化对象。
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
<!--约束省略--> <!-- 配置service --> <bean id="userService" class="com.xx.service.UserServiceImpl" /> </beans>
需要被spring管理的类
package com.xx.service; public class UserServiceImpl implements UserService{ public void run(){
System.out.println("userService is running");
}
public UserServiceImpl(){
System.out.println("no param");
}
}
1.2通过有参构造:
在加载applicationContext.xml文件的时候会通过有参构造实例化对象。
applicationContext.xml
此处index表示构造里的参数,从0开始,value是形参的值
<?xml version="1.0" encoding="UTF-8"?>
<beans
<!--约束省略-->
<!-- 配置service -->
<bean id="userService" class="com.xx.service.UserServiceImpl">
<constructor-arg index="0" value="rose"/>
<constructor-arg index="1" value="mark"/>
</bean>
</beans>
需要被spring管理的类
rose和mark会被当做形式参数将值传递给str和str1,从而完成实例化对象
package com.xx.service; public class UserServiceImpl implements UserService{
public void run(){
System.out.println("userService is running");
}
public UserServiceImpl(String str,String str1){
System.out.println(str);
System.out.println(str1);
}
}
二、通过静态工厂
applicationContext.xml文件
factory-method是class下的静态方法,意味着执行此方法从而得到实例化的对象
<?xml version="1.0" encoding="UTF-8"?>
<beans
《!--约束省略--》 <bean id="userServiceByFactory" class="com.xx.factory.BeanFactory" factory-method="createUserService"/> </beans>
生产实例化工厂
package com.xx.factory; import com.xx.service.UserService;
import com.xx.service.UserServiceImpl; public class BeanFactory { public static UserService createUserService(){
return new UserServiceImpl();
}
}
三、通过实例(非静态)工厂
applicationContext.xml
指定哪个类,哪个方法,从而返回实例化对象
<?xml version="1.0" encoding="UTF-8"?>
<beans
<!--约束省略--> <bean id="beanFactory" class="com.xx.factory.BeanFactory"/>
<bean id="userService" factory-bean="beanFactory" factory-method="createUserService"/> </beans>
实例工厂
package com.xx.factory; import com.xx.service.UserService;
import com.xx.service.UserServiceImpl; public class BeanFactory { public UserService createUserService(){
return new UserServiceImpl();
} }
以上三种方式的测试方法:
package com.xx.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xx.service.UserService;
public class UserServiceTest {
private ApplicationContext context = null;
@Test
public void TestApp() {
String beanPath="classpath:applicationContext.xml";
context = new ClassPathXmlApplicationContext(beanPath);
UserService userService = (UserService) context.getBean("userService");
userService.run();
}
}
总结:提倡使用无参构造实例化。
非静态
spring实例化bean三种方式的更多相关文章
- Spring实例化Bean三种方法:构造器、静态工厂、实例工厂
Spring中Bean相当于java中的类,可以通过xml文件对bean进行配置和管理. 一.Bean的实例化: 构造器实例化.静态工厂实例化.实例工厂方式实例化. 目录: 构造器实例化: xml配置 ...
- Spring中bean实例化的三种方式
之前我已经有好几篇博客介绍Spring框架了,不过当时我们都是使用注解来完成注入的,具体小伙伴可以参考这几篇博客(Spring&SpringMVC框架案例).那么今天我想来说说如何通过xml配 ...
- Spring bean管理器 bean实例化的三种方式
bean实例化的三种方式实现 第一种:使用类的无参数构造方法创建(常用 重要) 第一种实例化方式最常用,实例化类时会通过调用无参构造方法创建.示例代码如下: package spring.com.Us ...
- 【Spring】的【bean】管理(XML配置文件)【Bean实例化的三种方式】
Bean实例化的三种方式 说明:通过配置文件创建对象就称为Bean实例化. 第一种:使用类的无参构造创建(重点) 实体类 package com.tyzr.ioc; public class User ...
- spring配置datasource三种方式
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...
- spring学习(03)之bean实例化的三种方式
bean实体例化的三种方式 在spring中有三中实例化bean的方式: 一.使用构造器实例化:(通常使用的一个方法,重点) 二.使用静态工厂方法实例化: 三.使用实例化工厂方法实例化 第一种.使用构 ...
- Spring依赖注入三种方式详解
在讲解Spring依赖注入之前的准备工作: 下载包含Spring的工具jar包的压缩包 解压缩下载下来的Spring压缩包文件 解压缩之后我们会看到libs文件夹下有许多jar包,而我们只需要其中的c ...
- Spring笔记03(Spring创建对象的三种方式)
1.创建对象的三种方式和bean的生命周期的验证: Animal接口代码: package cn.pb.dao; /** * 动物接口 */ public interface Animal { //吃 ...
- spring配置datasource三种方式及具体信息
1.使用org.springframework.jdbc.datasource.DriverManagerDataSource说明:DriverManagerDataSource建立连接是只要有连接就 ...
随机推荐
- linux连接mysql 出现Access denied for user 'root'@'localhost'(using password: YES)错误解决方案
linux连接mysql /usr/local/mysql/bin/mysql -uroot -p 输入密码出现Access denied for user 'root'@'localhost'(us ...
- <select>简易的二级联动
1.首先是表单页面: <tr> <td align="right"> <label class="Validform_label" ...
- windows下apache配置虚拟主机
因为有多个laravel项目需要配置根目录到public下面,所以要配置多个虚拟主机 方法一:添加端口号 第一步:进入apache的目录 Apache24\conf 找到 httpd.conf 文件, ...
- hdu_2668 Daydream O(n)求最长不重复子串
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2668 Daydream Time Limit: 2000/1000 MS (Java/Others) ...
- Exponentiation(java 大实数)
http://acm.hdu.edu.cn/showproblem.php?pid=1063 Exponentiation Time Limit: 2000/500 MS (Java/Others) ...
- SpringMVC整合Shiro权限框架
尊重原创:http://blog.csdn.net/donggua3694857/article/details/52157313 最近在学习Shiro,首先非常感谢开涛大神的<跟我学Shiro ...
- thinkPHP内置字符串截取msubstr函数用法详解
作者:陈达辉 字体:[增加 减小] 类型:转载 时间:2016-11-15 我要评论 这篇文章主要介绍了thinkPHP内置字符串截取函数用法,结合实例形式分析了thinkPHP内置的字符串截取函数功 ...
- vue 路由部署服务器子目录问题
http://blog.csdn.net/hero82748274/article/details/73436497 <Egret 引擎入门> 这两天一直在查询vue经过打包后部署服务器一 ...
- vuethink 配置
http://blog.csdn.net/hero82748274/article/details/76100938 vuethink 是一款基于PHP TP5和Vuejs 结合的后台框架,设计起来是 ...
- dedecms 图集标签{dede:productimagelist} {dede:field name='imgurls'}&nbs
1.{dede:productimagelist}{/dede:productimagelist} 2.{dede:field name='imgurls'}{/dede:field} 这两个图集标签 ...