什么是 Spring
Spring 是一个开源框架,Spring 是于2003 年兴起的一个轻量级的Java 开发框架,由 RodJohnson
在其著作 Expert One-On-One J2EE Development and Design 中阐述的部分理念和原型衍生而来。它是
为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使
用者选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架。Spring 使用基本的 JavaBean
来完成以前只可能由 EJB 完成的事情。然而,Spring 的用途不仅限于服务器端的开发。从简单性、
可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。Spring 的核心是控制反转
(IoC)和面向切面(AOP)。简单来说,Spring 是一个分层的 JavaSE/EEfull-stack(一站式) 轻量级
开源框架。
EE 开发分成三层结构
* WEB 层:Spring MVC.
* 业务层:Bean 管理:(IOC)
* 持久层:Spring 的 JDBC 模板.ORM 模板用于整合其他的持久层框架.
方便解耦,简化开发
Spring 就是一个大工厂,可以将所有对象创建和依赖关系维护,交给 Spring 管理 AOP 编程的支持
Spring 提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能 声明式事务的支持
只需要通过配置就可以完成对事务的管理,而无需手动编程 方便程序的测试
Spring 对 Junit4 支持,可以通过注解方便的测试 Spring 程序 方便集成各种优秀框架
Spring 不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、 MyBatis、Quartz
等)的直接支持
降低 JavaEE API 的使用难度
Spring 对 JavaEE 开发中非常难用的一些 API(JDBC、JavaMail、远程调用等),都提供了封装, 使这些 API应用难度大大降低
IOC:Inversion of Control 控制反转. 指的是 对象的创建权反转(交给)给 Spring.
作用是实现了程序的解耦合.
官网:官网地址
下载地址:下载地址
解压:(Spring目录结构:)
docs :API和开发规范.
libs :jar包和源码.
schema :约束.
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
beans>
log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=error, stdout
log4j.logger.com.springframework=DEBUG
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
public interface UserDao{
public void sayHello();
}
public class UserDaoImpl implements UserDao {
@Override
public void sayHello() {
System.out.println("Hello Spring...");
}
}
<bean id="userDao" class="cn.test.spring.demo1.UserDaoImpl">bean>
@Test
// Spring的方式:
public void demo2(){
// 创建 Spring的工厂类:
ApplicationContext applicationContext=
new ClassPathXmlApplicationContext("applicationContext.xml");
// 通过工厂解析 XML获取 Bean的实例.
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.sayHello();
}
id 属性和 name 属性标签的配置
id :Bean起个名字. 在约束中采用 ID的约束:唯一.必须以字母开始,可以使用字母、数字、连字符、
下划线、句话、冒号 id:不能出现特殊字符.
name:Bean起个名字. 没有采用 ID的约束. name:出现特殊字符.如果没有 id的话 , name 可
以当做 id使用.
整合 struts1的时候:
scope 属性:Bean 的作用范围
globalSession:WEB项目中,应用在 Porlet环境.如果没有 Porlet环境那么 globalSession相当
于 session.
<bean id="bean1" class="cn.test.spring.demo3.Bean1">bean>
【静态工厂实例化的方式】
提供一个工厂类:
public class Bean2Factory {
public static Bean2 getBean2(){
return new Bean2();
}
}
<bean id="bean2" class="cn.test.spring.demo3.Bean2Factory" factory-method="getBean2"/>
【实例工厂实例化的方式】
提供 Bean3的实例工厂:
public class Bean3Factory {
public Bean3 getBean3(){
return new Bean3();
}
}
<bean id="bean3Factory" class="cn.test.spring.demo3.Bean3Factory">bean>
<bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3">bean>
<bean id="car" class="cn.test.spring.demo4.Car">
<constructor-arg name="name" value="保时捷"/>
<constructor-arg name="price" value="1000000"/>
bean>
【set 方法的方式注入属性】
<bean id="car2" class="cn.test.spring.demo4.Car2">
<property name="name" value="奇瑞 QQ"/>
<property name="price" value="40000"/>
bean>
<bean id="person" class="cn.test.spring.demo4.Person">
<property name="name" value="会希"/>
<property name="car2" ref="car2"/>
bean>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
第二步:使用 p名称空间.
* 普通属性:p:属性名称=””
* 对象类型属性:p:属性名称-ref=””
<bean id="car2" class="cn.test.spring.demo4.Car2" p:name="宝马 7"
p:price="1200000"/>
<bean id="person" class="cn.test.spring.demo4.Person" p:name="思聪"
p:car2-ref="car2"/>
<bean id="car2" class="cn.test.spring.demo4.Car2">
<property name="name" value="#{'奔驰'}"/>
<property name="price" value="#{800000}"/>
bean>
<bean id="person" class="cn.test.spring.demo4.Person">
<property name="name" value="#{'冠希'}"/>
<property name="car2" value="#{car2}"/>
bean>
<bean id="carInfo" class="cn.test.spring.demo4.CarInfo">bean>
<bean id="car2" class="cn.test.spring.demo4.Car2">
<property name="name" value="#{carInfo.carName}"/>
<property name="price" value="#{carInfo.calculatePrice()}"/>
bean>
<bean id="collectionBean" class="cn.test.spring.demo5.CollectionBean">
<property name="arrs">
<list>
<value>会希value>
<value>冠希value>
<value>天一value>
list>
property>
<property name="list">
<list>
<value>芙蓉value>
<value>如花value>
<value>凤姐value>
list>
property>
<property name="map">
<map>
<entry key="aaa" value="111"/>
<entry key="bbb" value="222"/>
<entry key="ccc" value="333"/>
map>
property>
<property name="properties">
<props>
<prop key="username">rootprop>
<prop key="password">123prop>
props>
property>
bean>
当集合中也是对象时
private List<Car> list1;
<bean id="car" class="cn.java.po.Car">
<property name="id" value="1">property>
<property name="color" value="yellow">property>
bean>
<property name="list1">
<list>
<ref bean="car">ref>
<ref bean="car">ref>
list>
property>
:
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");
二种:在一个配置文件中包含另一个配置文件:
<import resource="applicationContext2.xml">import>