depend-on、延迟初始化Bean、 Bean定义的继承
depend-on
案例:
StudentServiceImpl.java
public class StudentServiceImpl {
/**默认构造器*/
public StudentServiceImpl() {
System.out.println("studnetService初始化完成...");
}
}
public class TeacherServiceImpl {
/**默认构造器*/
public TeacherServiceImpl() {
System.out.println("teacherservice初始化完毕...");
}
}
dependson.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">
<!--
1、depends-on 在初始化teacherServiceImpl之前进行初始化depends-on的属性值匹配的bean
2、你也可以吧teacherServiceImpl所以依赖的bean都放在teacherService之前并且scope是singleton模式即可
备注: 只使用第一种方式即可。
-->
<bean id="studentServiceImpl" class="cn.csdn.service.StudentServiceImpl" scope=" singleton "/>
<bean id="teacherServiceImpl" class="cn.csdn.service.TeacherServiceImpl" scope="singleton" />
</beans>
运行结果:studnetService初始化完成...
teacherservice初始化完毕...
注意的问题:
1.若teacherServiceImpl的bean在前 作用域为scope=’singleton’
studentServiceImpl的bean在后 作用域也为scope=’singleton’则运行结果为:
teacherservice初始化完毕...
studnetService初始化完成...
2. 若teacherServiceImpl的bean在前 作用域为scope=’singleton’ 并且在其中添加depends-on=’ studentServiceImpl’
studentServiceImpl的bean在后 作用域也为scope=’singleton’则运行结果为:
studnetService初始化完成...
teacherservice初始化完毕...
3.若studentServiceImpl的bean在前 并且其作用域改为scope=’prototype’ 则运行结果为:teacherservice初始化完毕...
4.若studentServiceImpl的bean在前其作用域仍为scope=’prototype’而在teacherServiceImpl的bean中添加depends-on=’ studentServiceImpl’ 其作用域为scope=’singleton’ 则运行结果为:studnetService初始化完成...
teacherservice初始化完毕...
可依据上述结果相互比较来理解depend-on的使用方法
Bean定义的继承
案例:
EmpServiceImpl.java
public class EmpServiceImpl {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
HourEmpServiceImpl.java
public class HourEmpServiceImpl extends EmpServiceImpl{
private Integer hours;
public void setHours(Integer hours) {
this.hours = hours;
}
}
RegularEmpServiceImpl.java
public class RegularEmpServiceImpl extends EmpServiceImpl {
private String title;
public void setTitle(String title) {
this.title = title;
}
}
parent.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">
<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl"
scope="singleton" >
<property name="name" >
<value >dxl</value>
</property>
</bean>
<!--继承了empServiceImpl中的name属性的setName()方法-->
<bean id="hourEmpServiceImpl" class="cn.csdn.service.HourEmpServiceImpl"
scope="singleton" parent="empServiceImpl">
<property name="hours">
<value>20</value>
</property>
</bean>
<bean id="regularEmpServiceImpl" class="cn.csdn.service.RegularEmpServiceImpl"
scope="singleton" parent="empServiceImpl">
<property name="name">
<value>xiaoli</value>
</property>
<property name="title">
<value>大二学生</value>
</property>
</bean>
</beans>
HourEmpServiceImpl.java类与RegularEmpServiceImpl.java类继承了EmpServiceImpl.java类
运行hourEmpServiceImpl可以或得empServiceImpl类中 的name属性也有自己的hours属性;
运行RegularEmpServiceImpl可以或得自己的属性即name=’xiaoli’ title=’大二学生’ 继承的父类中的name属性将被自己的name属性覆盖。
延迟初始化Bean在体验spring(三)中已有补充
分享到:
相关推荐
cglib 3.0 itself depends on asm 4.0, and is therefore // further transformed by the JarJar task to depend on org.springframework.asm; this // avoids including two different copies of asm ...
android-support-multidex-instrumentation.jar.zip
USB4 1.0 ECN - Wake on USB4 Depends on Enable Wake on USB4 Wake.pdf
spring框架中bean标签parent属性的各类使用范例 总计12个范例,涵盖了class、init-method、 factory-method、lazy-init、 depends-on、autowire 多种属性的组合使用
Using depends-on ............................................................................................ 47 Lazy-initialized beans ...................................................................
水印工具,详情请关注博客:http://blog.csdn.net/ghostbear
3.3.4. 使用depends-on 3.3.5. 延迟初始化bean 3.3.6. 自动装配(autowire)协作者 3.3.6.1. 设置Bean使自动装配失效 3.3.7. 依赖检查 3.3.8. 方法注入 3.3.8.1. Lookup方法注入 3.3.8.2. 自定义方法的替代方案 3.4....
DLL查看工具--DEPENDS DLL查看工具--DEPENDS DLL查看工具--DEPENDS
资源分类:Python库 所属语言:Python 资源全名:large-image-source-mapnik-1.11.3.dev17.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
F-OH 是一个 OpenHarmony 平台上 FOSS(Free and Open Source Software,自由开源软件)的应用中心,并提供下载安装支持
ndk-depends
ubuntu16.04升级内核时如果提示如下错误,需要安装此包:...linux-image-unsigned-4.20.17-042017-generic depends on linux-base (>= 4.5ubuntu1~16.04.1); however: Version of linux-base on system is 4.0ubuntu1.
Using depends-on ............................................................................................ 47 Lazy-initialized beans ...................................................................
2019_It depends on when you search(使用“搜索量”来量化“用户注意力”)
官方版本,亲测可用
dependsOn, 用于处理表单域依赖项的jQuery插件 依赖 一个jQuery插件,用于处理表单域依赖项的处理。返回 $( 对象的名称。示例演示工具安装使用npm的npm install --save jquery-depends-on带 Bower的
hap-depends工具可以用来查看应用软件的依赖库,非常方便。
编译QtAV模块需要使用的ffmpeg依赖库。
资源分类:Python库 所属语言:Python 资源全名:colcon_cmake-0.2.12-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
opencv4.4.0编译依赖全部依赖,包括opencv_videoio_ffmpeg.dll、vgg_generated_48.i、face_landmark_model.dat等