Spring

一、Spring介绍

1、学习目的

方便解耦,简化开发:
Spring 就是一个大工厂,可以将所有对象创建和依赖关系维护,交给 Spring 管理
AOP 编程的支持: Spring 提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
声明式事务的支持: 只需要通过配置就可以完成对事务的管理,而无需手动编程
方便程序的测试: Spring 对 Junit4 支持,可以通过注解方便的测试 Spring 程序
方便集成各种优秀框架: Spring 不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、 MyBatis、Quartz 等)的直接支持降低 JavaEE API 的使用难度

2、IOC 和 DI

IOC:
Inversion of Control 控制反转. 指的是 对象的创建权反转(交给)给 Spring.
作用是实现了程序的解耦合.
DI :
Dependency Injection 依赖注入.需要有 IOC 的环境,Spring 创建这个类的过程中,Spring 将类的依 赖的属性设置进去.

3、配置文件创建:

log4j.properties
#

applicationContext.xml

<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.xsd">

<bean id = "user" class="cn.it.User"></bean>

</beans>

4、参数介绍:

id :Bean 起个名字. 在约束中采用 ID 的约束:唯一.必须以字母开始,可以使用字母、数字、连字符、
下划线、句话、冒号 id:不能出现特殊字符.
<bean id=”bookAction”>
name:Bean 起个名字. 没有采用 ID 的约束. name:出现特殊字符.如果<bean>没有 id 的话 , name 可
以当做 id 使用.
* 整合 struts1 的时候:
<bean name=”/loginAction” >

5、BeanFactory 和 ApplicationContext 的区别:

BeanFactory :是在 getBean 的时候才会生成类的实例.
ApplicationContext :在加载 applicationContext.xml(容器启动)时候就会创建

6、scope 属性:Bean 的作用范围.

  singleton :默认值,单例的.
  prototype :多例的.     在struts2中必须设为多列    
既能朝九晚五,又能浪迹天涯
原文地址:https://www.cnblogs.com/jackerzhou/p/9916438.html