Spring之autowire(自动装配)与lazy-init
来源:互联网 发布:linux蓝牙驱动移植 编辑:程序博客网 时间:2024/06/05 16:32
(一)在Spring的配置文件中可以通过autowire属性来实现自动装配,就不需要设置 setter方法:<property name="daoId" value="2"></property>
autowire属性的常用取值:byName , byType ,default 。
(二)autowire属性可以写在bean标签或者beans标签
在beans标签 设置:default-autowire=“byType” 所有的bean都不要配置注入了。各个bean通过设置autowire=“default”,表示使用beans标签的default-autowire设置的注入方式。
实例:
<?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"
default-autowire="byType" >
<bean name="userDAO2" class="com.bjsxt.dao.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean id="userService" class="com.bjsxt.service.UserService" scope="prototype" autowire="byType">
</bean>
</beans>
(三)一些bean可以设置lazy-init="true"即:用到它时在初始化。可以提高运行效率。
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/><bean name="not.lazy" class="com.foo.AnotherBean"/>也可以设置所有bean都懒加载:在beans标签内设置:
<beans default-lazy-init="true"> <!-- no beans will be pre-instantiated... --></beans>
(四)init-method属性与destroy-Method属性:表示bean在初始化之前首先调用init-method属性指定的方法,容器关闭的时候会首先调用destroy-method属性指定的方法。
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
public class ExampleBean { public void init() { // do some initialization work }}通过实现
org.springframework.beans.factory.InitializingBean 接口也可以实现上述功能
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements InitializingBean { public void afterPropertiesSet() { // do some initialization work }}
对于destroy-method属性实例:
<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
public class ExampleBean { public void cleanup() { // do some destruction work (like releasing pooled connections) }}
通过实现org.springframework.beans.factory.DisposableBean
接口也能实现容器在销毁时调用destroy方法。
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
public class AnotherExampleBean implements DisposableBean { public void destroy() { // do some destruction work (like releasing pooled connections) }}
- Spring之autowire(自动装配)与lazy-init
- spring的default-lazy-init参数和spring的自动装配(default-autowire="byName")
- spring之自动装配autowire
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring Autowire自动装配
- Spring 自动装配AutoWire
- Spring 自动装配AutoWire
- Count Numbers with Unique Digits问题及解法
- Oracle 11G RAC数据库基本测试和使用
- Metasploit功能程序------payloads、encoder、Nasm shell
- iOS开发之pdf文档的加载与浏览的4种方式
- Mac mvn : command not found
- Spring之autowire(自动装配)与lazy-init
- 3张图了解,关系数据库服务RDS的基本原理
- 关于多线程文件读写的一些博文
- [绍棠] ios 应用内支付(In-App Purchase,沙盒测试,后台验证)
- 图片自适应label大小
- 【mysql】nodejs 下对 mysql 查询条件的一个封装
- ABBYY PDF Transformer+到底有哪些专业术语
- ==与equals的区别
- Differences among parseInt(), intValue(), valueOf()