您的位置:首页 > 编程语言 > Java开发

Spring简单总结part1

2016-04-19 00:00 363 查看
摘要: Spring知识总结part1

Spring简单总结

1. Spring的概述(抄的百科)

Spring的核心是控制反转(IoC)和面向切面(AOP)。简单来说,Spring是一个分层的"一站式"轻量级开源框架
分层:EE开发中三层结构.(一站式:Spring框架提供了EE开发每一层的解决方案.)
* WEB层: Spring MVC.

* 业务层: Spring的Bean管理.

* 持久层: Spring的JdbcTemplate, orm模块用于整合其他的持久层框架.

2. Spring的优点

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

3. Spring的开发包

3.1 开发包的目录结构

docs : Spring的开发文档
libs: Spring的开发包.
schema: 约束文档.

3.2 HelloWorld必备包

context beans core expression

org.apache.commons.logging + log4j

4. HelloWorld案例

4.1 准备配置文件

在src目录下,创建名为 applicationContext的xml文件 (注意,文件图标会变成小树叶)

4.2 引入配置文件的约束文件

按以下步骤:

windowèpreferenceè搜索catalogèaddèfile systemè查找约束文件èkey typeèSchema Locationèkeyè原有内容后添加约束文件名èOKè至此,引入约束文件成功

4.3 在配置文件中添加约束

打开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>

4.4 准备实体类

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 + "]";

}

}

4.5 在配置文件中进行配置

<bean name="car" class="cn.xiaoge.domain.Car">
<property name="name" value="QQ"></property>

</bean>

4.6 在测试类中进行测试

@Test

public void fun1(){

//1. 先加载配置信息,其中会将配置文件中配置的bean初始化

ClassPathXmlApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
//2. 获取相应的对象

Car car = (Car) ac.getBean("car");
//3. 测试属性注入是否成功
System.out.println(car);

}

5. 属性注入

5.1 构造方法的属性注入

<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>

5.2 Set方法的属性注入

<bean name="em" class="cn.xiaoge.domain.Employee">

<!-- value主要负责基本数据类型的赋值操作.会覆盖构造方法中的赋值 -->

<property name="name" value="xiaogezi" />

<!-- ref主要负责引用数据类型的赋值操作;另外,该引用类型的类必须在spring配置文件中定义 -->

<property name="car" ref="car"></property>

</bean>

5.3 p名称空间的注入

首先,在约束头中添加:

xmlns:p=http://www.springframework.org/schema/p

其次,添加属性值

<bean name="car" class="cn.xiaoge.domain.Car" p:name="QQ">
</bean>

5.4 SpEL的属性注入

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>

5.5. 为各类集合注入

<!-- 集合注入 -->

<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring总结