Spring实例化Bean三种方法:构造器、静态工厂、实例工厂
Spring中Bean相当于java中的类,可以通过xml文件对bean进行配置和管理。
一、Bean的实例化:
构造器实例化、静态工厂实例化、实例工厂方式实例化。
目录:
构造器实例化:
xml配置文件:
id唯一,calss指定Bean具体实现类,必须是完整的类名,可以在Bean1.java文件中右击“public class Bean1”中的Bean1,选中Copy Qualifiel Name得到。注意这里用"."分隔。
测试函数:
测试函数中首先定义xml配置文件的路径,可以在目录视图中右键选中Copy Qualifiel Name得到,注意这里从com开始,因为是路径所以用/隔开。
然后加载配置文件对Bean进行实例化,在通过getBean函数获得指定id的实例对象,注意类型转换。
完整代码:
package com.liu.instance.contructor; public class Bean1 { }
Bean1.java
<?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-4.3.xsd">
<bean id="bean1" class="com.liu.instance.contructor.Bean1"></bean>
</beans>
beans1.xml
package com.liu.instance.contructor; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class InstanceTest { public static void main(String[] args) {
//定义配置文件路径
String xmlPath = "com/liu/instance/contructor/beans1.xml";
//ApplicationContext 加载配置文件时对Bean进行实例化。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
//配置文件的id确定是哪个bean,在通过class找到java文件创建对象。
Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");
System.out.println(bean1); }
}
InstanceTest.java
运行截图:
静态工厂实例化:
xml配置文件:
id唯一,class为工厂方法类,factory-method值为方法名,确定使用了工厂中的哪个方法。
静态工厂类:
静态方法返回一个Bean2对象。
package com.liu.instance.static_factory;
/*
* lsq
* 2019-9-10
* Spring静态工厂实例化被实例化的类Bean2
*/
public class Bean2 { }
Bean2.java
package com.liu.instance.static_factory;
/*
* lsq
* 2019-9-10
* Spring静态工厂实例化
*/ public class MyBean2Factory { //创建Bean2对象
public static Bean2 createBean2(){
return new Bean2();
}
}
MyBean2Factory.java
package com.liu.instance.static_factory; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /*
* lsq
* 2019-9-10
* Spring静态工厂实例化对象测试类
*/
public class InstanceTest { public static void main(String[] agrs){
//定义配置文件路径
String xmlPath = "com/liu/instance/static_factory/beans2.xml";
//实例化对象
ApplicationContext ApplicationContext = new ClassPathXmlApplicationContext(xmlPath);
//调用函数
//getBean函数传入id属性值获取对象
System.out.println(ApplicationContext.getBean("bean2"));
}
}
InstanceTest.java
<?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-4.3.xsd"> <bean id="bean2" class="com.liu.instance.static_factory.MyBean2Factory"
factory-method="createBean2">
</bean>
</beans>
beans2.xml
运行截图:
实例工厂实例化:
xml配置文件:
两个bean第一个为静态工厂,class为静态工厂类。第二个为bean3,factory-method属性配置实例工厂,factory-method确定使用工厂中哪个方法。
工厂类:
返回一个Bean3对象。
完整代码:
package com.liu.instance.factory;
/*
* lsq
* 2019-9-10
* Spring工厂实例化对象
*/
public class Bean3 { }
Bean3.java
<?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-4.3.xsd"> <bean id="bean2" class="com.liu.instance.static_factory.MyBean2Factory"
factory-method="createBean2">
</bean>
</beans>
bean3.xml
package com.liu.instance.factory;
/*
* lsq
* 2019-9-10
* Spring工厂实例化对象
*/
public class MyBean3Factory { public MyBean3Factory(){
System.out.println("Bean3工厂实例化中。。。");
}
public Bean3 createBean3(){
return new Bean3();
}
}
MyBean3Factory.java
package com.liu.instance.factory; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /*
* lsq
* 2019-9-10
* Spring工厂实例化测试类
*/
public class InstanceTest3 { public static void main(String[]args){
//指定配置文件路径
String xmlPath= "com/liu/instance/factory/bean3.xml";
//ApplicationContext加载配置文件时,对Bean进行实例化
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); System.out.println(applicationContext.getBean("bean3"));
}
}
InstanceTest3.java
运行截图:
三种方法区别:
Spring实例化Bean三种方法:构造器、静态工厂、实例工厂的更多相关文章
- spring实例化bean三种方式
我看了这篇博文<https://www.cnblogs.com/zhanglei93/p/6221546.html>,以及自己实践总结了关于spring实例化bean对象的3种方式. 一. ...
- Bean实例化(三种方法)
(一)构造器实例化Bean 1. Bean1.java package com.inspur.ioc; public class Bean1 { } 2.Beans1.xml <?xml ver ...
- python 四种方法修改类变量,实例对象调用类方法改变类属性的值,类对象调用类方法改变类属性的值,调用实例方法改变类属性的值,直接修改类属性的值
三种方法修改类变量,实例对象调用类方法改变类属性的值,类对象调用类方法改变类属性的值,调用实例方法改变类属性的值,类名就是类对象,city就是类变量, #coding=utf-8 class empl ...
- Spring中bean实例化的三种方式
之前我已经有好几篇博客介绍Spring框架了,不过当时我们都是使用注解来完成注入的,具体小伙伴可以参考这几篇博客(Spring&SpringMVC框架案例).那么今天我想来说说如何通过xml配 ...
- Spring bean管理器 bean实例化的三种方式
bean实例化的三种方式实现 第一种:使用类的无参数构造方法创建(常用 重要) 第一种实例化方式最常用,实例化类时会通过调用无参构造方法创建.示例代码如下: package spring.com.Us ...
- spring注入bean的三种方法
在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...
- 【Spring】的【bean】管理(XML配置文件)【Bean实例化的三种方式】
Bean实例化的三种方式 说明:通过配置文件创建对象就称为Bean实例化. 第一种:使用类的无参构造创建(重点) 实体类 package com.tyzr.ioc; public class User ...
- Spring 实例化bean的三种方式
第一种方法:直接配置Bean <bena id="所需要实例化的一个实例名称" class="包名.类名"/> 例如: 配置文件中的bean.XML ...
- Spring bean三种创建方式
spring共提供了三种实例化bean的方式:构造器实例化(全类名,反射).工厂方法(静态工厂实例化 动态工厂实例化)和FactoryBean ,下面一一详解: 1.构造器实例化 City.jav ...
随机推荐
- MySQL实现计算两点之间的距离
DELIMITER $$ CREATE FUNCTION `calculateLineDistance`(startLng double, startLat double, endLng double ...
- House Lawn Kattis - houselawn
Problem You have just bought a new house, and it has a huge, beautiful lawn. A lawn that needs cutti ...
- 学密码学一定得学程序(SDUT 2463)
Problem Description 曾经,ZYJ同学非常喜欢密码学.有一天,他发现了一个很长很长的字符串S1.他很好奇那代表着什么,于是神奇的WL给了他另一个字符串S2.但是很不幸的是,WL忘记跟 ...
- 【原创】go语言学习(一)
一.go发展历史 1.1诞生历史 1.诞生于2006年1月下午15点4分5秒 2.2009发布并正式开园 3.2012年第一个正式版本Go1.0发布 4.截止2019年10月8日,Go1.13.1 1 ...
- iOS测试-如何指标量化app耗电量和性能XCTest Metrics
对于app端的专项测试,Android端我们可以用adb或者一些三方工具进行(例如itest)进行实时的性能监控,iOS端的话也可以用用一些三方的工具,但是需要嵌入到我们的项目当中,今天来介绍下Xco ...
- skb head/data/tail/end/介绍
2017年04月26日 18:21:12 abcLinux 阅读数 799 This first diagram illustrates the layoutof the SKB data are ...
- Notepad++编辑.sh文件
使用记事本创建创建test.txt文件,修改后缀名为sh后,再文件里写以下内容: #!/bin/bash echo "hello world" 这样的文件再linux里是无法执行的 ...
- 重读APUE(9)-SIG_ERR、SIG_DFL、SIG_IGN定义无参数
下面这几个函数定义,每次看到都会纠结一阵子,奇怪的是为什么没有参数? #define SIG_ERR (void (*)())-1 #define SIG_DFL (void (*)())0 #def ...
- Web开发中 MTV模式与MVC模式的区别 联系 概念
MTV 与 MVC模式的区别 联系 概念: MTV: 所谓MTV指的就是: M:model (模型),指的是ORM模型. T:template (模板),一般Python都是使用模板渲染的方式来把HT ...
- 无法下载golang.org-x-net解决方法
由于go的很多包都依赖了google官方的包,而google官方的包都在google服务器上,因为某些原因无法直接访问,在搜索了很多解决方案后,找到了最简单的一个方法: 1. 找到对应包在github ...