Spring学习笔记 2014-7-9

Spring需要applicationContext.xml来管理各个Bean,其基本格式:

<?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:tx="http://www.springframework.org/schema/tx" 
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:jee="http://www.springframework.org/schema/jee"
        xsi:schemaLocation="
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
</beans>

当中可以插入各个Bean。加入注解后会以扫描器替代

<!-- 开启组件扫描 -->
    <context:component-scan base-package="fate"/>
    <!-- 开启AOP注解 -->
    <aop:aspectj-autoproxy/>

最新版的Spring至少需要spring-core-4.0.5.RELEASE.jar,spring-beans-4.0.5.RELEASE.jar,spring-context-4.0.5.RELEASE.jar,spring-expression-4.0.5.RELEASE.jar,commons-logging-1.1.3.jar,百度上说从Spring3开始Spring包的各个部分拆开来了,以提供编程的自由。

Spring创建Bean时默认用的是单例模式,在多线程下并发访问时可能会出现问题,这时要在<Bean>的最后加上scope="prototype"(原型模式)。

lazy-init="true" 只在需要Bean时加载。

init-method="myinit"用于指定Bean的初始化方法。

destroy-method="mydestroy"用于指定Bean的销毁方法。(只在单例模式下有效)

原文地址:https://www.cnblogs.com/raikouissen/p/3833537.html