Spring的核心是控制反转(IoC)和面向切面(AOP)。简单来讲,Spring是一个分层的"一站式" 轻量级开源框架。 spring
分层:EE开发中三层结构.(一站式:Spring框架提供了EE开发每一层的解决方案.) express
* WEB层: Spring MVC. apache
* 业务层: Spring的Bean管理. 编程
* 持久层: Spring的JdbcTemplate, orm模块用于整合其余的持久层框架. 数组
方便解耦,简化开发 app
Spring就是一个大工厂,能够将全部对象建立和依赖关系维护,交给Spring管理 框架
AOP编程的支持 dom
Spring提供面向切面编程,能够方便的实现对程序进行权限拦截、运行监控等功能 ide
声明式事务的支持 测试
只须要经过配置就能够完成对事务的管理,而无需手动编程
方便程序的测试
Spring对Junit4支持,能够经过注解方便的测试Spring程序
方便集成各类优秀框架
Spring不排斥各类优秀的开源框架,其内部提供了对各类优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
下降JavaEE API的使用难度
Spring 对JavaEE开发中很是难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大下降
docs : Spring的开发文档
libs: Spring的开发包.
schema: 约束文档.
context beans core expression
org.apache.commons.logging + log4j
在src目录下,建立名为 applicationContext的xml文件 (注意,文件图标会变成小树叶)
按如下步骤:
windowèpreferenceè搜索catalogèaddèfile systemè查找约束文件èkey typeèSchema Locationèkeyè原有内容后添加约束文件名èOKè至此,引入约束文件成功
打开applicationContext.xm文件è添加<beans></beans>节点è切换到design视图è右键èedit namespaceèaddèxsièOKè继续addèspecify new NamespaceèLocation HintèBrowserè选择上一步引入的约束文件èOKèprefix能够为空ènamespace的值可 复制Location Hint中最后一个”/”前的内容èOKè从新打开applicationContext.xml,测试,有提示!OK
最终效果:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
</beans>
public class Car {
private String name; //准备属性
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Car [name=" + name + "]";
}
}
<bean name="car" class="cn.xiaoge.domain.Car">
<property name="name" value="QQ"></property>
</bean>
public void fun1(){
//1. 先加载配置信息,其中会将配置文件中配置的bean初始化
ClassPathXmlApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
//2. 获取相应的对象
Car car = (Car) ac.getBean("car");
//3. 测试属性注入是否成功
System.out.println(car);
}
<bean name="em" class="cn.xiaoge.domain.Employee" >
<!--
利用构造方法为属性赋值
name: 构造方法的参数名
value: 构造方法中该参数的值
ref: 构造方法中参数为引用类型时的值
type: 构造方法中参数的数据类型
index: 该参数在构造方法的参数列表中的索引
-->
<constructor-arg name="name" value="xiaoxiao" type="String"></constructor-arg>
<constructor-arg name="car" ref="car" ></constructor-arg>
</bean>
<bean name="em" class="cn.xiaoge.domain.Employee">
<!-- value主要负责基本数据类型的赋值操做.会覆盖构造方法中的赋值 -->
<property name="name" value="xiaogezi" />
<!-- ref主要负责引用数据类型的赋值操做;另外,该引用类型的类必须在spring配置文件中定义 -->
<property name="car" ref="car"></property>
</bean>
首先,在约束头中添加:
xmlns:p=http://www.springframework.org/schema/p
其次,添加属性值
<bean name="car" class="cn.xiaoge.domain.Car" p:name="QQ">
</bean>
SpEL: Spring Expression Language
//注意要为bean设置id,这样才能在el表达式中用#{Beanid}
<bean id="carInfo" class="cn.itcast.spring.demo6.CarInfo">
</bean>
<!-- SpEL的方式的属性注入 -->
<bean id="car2" class="cn.itcast.spring.demo6.Car2">
<property name="name" value="#{carInfo.carName}"/>
<property name="price" value="#{carInfo.calculatorPrice()}"/>
</bean>
<bean id="employee" class="cn.itcast.spring.demo6.Employee">
<property name="name" value="abc "/>
<property name="car2" value="#{car2}"/>
</bean>
<!-- 集合注入 -->
<bean name="mylist" class="cn.xiaoge.domain.MyList">
<!-- List集合注入 -->
<property name="list" >
<list>
<value>123</value>
<ref bean="car"/>
</list>
</property>
<!-- set集合注入 -->
<property name="set">
<set>
<value>xiaogezi</value>
<ref bean="car"/>
</set>
</property>
<!-- 为数组注入 -->
<property name="arr">
<array>
<value>123</value>
<ref bean="car"/>
</array>
</property>
<!-- 为map注入 -->
<property name="map">
<map>
<entry key="name" value="xiaoge"></entry>
<entry key="car" value-ref="car"></entry>
</map>
</property>
<!-- 为Properties 注入 -->
<property name="prop">
<props>
<prop key="username">123</prop>
<prop key="password">1234</prop>
</props>
</property>
</bean>