1.什么是spring
spring是一站式框架
spring在ssh开发中是框架粘合剂
hibernate是数据持久层一站式框架
session.save(Object)
ibatis:半持久化框架
save(object)-->SQL程序员自定义
MVC!=三层框架
V C M
jsp servlet JavaBean
--------------- --------------
表示层 业务层 数据访问层
struts2 spring spring hiberante
struts ibatis
webwork JDO
JSF topLink
v:数据显示,不做业务逻辑
c:接收用户请求,调用JavaBean(业务|数据)
相应用户请求,分发页面
m:业务处理|数据处理
2、为什么学习spring
(1)流行
(2)开源简单
(3)目前流行技术都支持spring
flex:富客户端技术
html5
3、如何学习框架技术
先用,后了解原理
4、spring HelloWorld
spring 2.5.6
创建Java Project
(1)添加spring类库
spring-framework-2.5.6/dist/spring.jar
日志管理类库
spring-framework-2.5.6/lib/log4j-1.2.15.jar
spring-framework-2.5.6/lib/log4j/commons-logging.jar
(2)自定义类库包
windows---->preferences--->Java---->Build Path------>User Libraries----->new
然后再项目中右击Builde Path ------>Add Libraries------>User libraries
(3)在src下添加配置文件
获取目录:
spring-framework-2.5.6\samples\jpetstore\war \WEB-INF\applicationContext.xml
5.spring核心解释
IOC:控制反转
对象依赖关系,让容器管理
DI:依赖注入
set方法完成调用者与被调用者之间依赖关系
调用者主动调用被调用者实例对象,调用者负责创建被调用者实例对象,耦合度比较高,需要解耦。被调用对象实例让spring容器创建.
6.spring的注入方式
项目结构图(两种注入方式通用)
(a)构造方法注入
提供一个有参构造器
applicationContext.xml中
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- dao --> <bean id="userHibernateDaoImpl" class="com.tarena.dao.impl.UserHibernateDaoImpl"></bean> <bean id="userIbatisDaoImpl" class="com.tarena.dao.impl.UserIbatisDaoImpl"></bean> <!-- biz --> <bean id="userServImpl" class="com.tarena.biz.impl.UserServImpl"> <constructor-arg index="0"> <ref bean="userIbatisDaoImpl"/> </constructor-arg> </bean> </beans>
注释:
1.index="0"构造器中的第个参数
2.<ref bean="">依赖那个实现类
3.在UserServImpl类中添加
private IUserDao iuserDao; public UserServImpl(IUserDao iuserDao){ this.iuserDao = iuserDao; }
4.在UserAction中
public class UserAction { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); IUserServ userServ = (IUserServ)ac.getBean("userServImpl"); userServ.findAll(); userServ.delete(new User()); } }
运行结果:
ibatis:查找成功
ibatis:删除成功
如果在applicationContext.xml中将<constructor-arg index="0"><ref bean="userIbatisDaoImpl"/></constructor-arg>改为
<constructor-arg index="0"><ref bean="userHibernateDaoImpl"/></constructor-arg>
运行结果:
hibernate:查找成功
hibernate:删除成功
(b)set注入(属性注入||接口注入)
(1)提供一个私有属性
(2)公有set方法,把属性注入(DI)到spring容器
(3)在spring配置文件中实现依赖关系管理
接口声明 ---> new 实现类实例
<property name="属性名(接口)">
<ref bean="实现类">
</property>
简写:
<property name="属性名" ref="实现类"/>
applicationContext.xml中
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- dao --> <bean id="userHibernateDaoImpl" class="com.tarena.dao.impl.UserHibernateDaoImpl"></bean> <bean id="userIbatisDaoImpl" class="com.tarena.dao.impl.UserIbatisDaoImpl"></bean> <!-- biz --> <bean id="userServImpl" class="com.tarena.biz.impl.UserServImpl"> <!-- <property name="iuserDao"> <ref bean="userIbatisDaoImpl"/> </property> --> <!-- 简写 --> <property name="iuserDao" ref="userIbatisDaoImpl"></property> </bean> </beans>
在UserServImpl类中,添加
private IUserDao iuserDao; public void setIuserDao(IUserDao iuserDao) { this.iuserDao = iuserDao; }
在UserAction类中
public class UserAction { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); IUserServ userServ = (IUserServ)ac.getBean("userServImpl"); userServ.findAll(); userServ.delete(new User()); } }
运行结果:
ibatis:查找成功
ibatis:删除成功
如果在applicationContext.xml中将<property name="iuserDao"><ref bean="userIbatisDaoImpl"/></property>改为
<property name="iuserDao"><ref bean="userHibernateDaoImpl"/></property>
则结果为
hibernate:查找成功
hibernate:删除成功
7.junit测试:
在spring2.5.6建议使用junit4.4
(1)在项目中添加junit4.4类库
(2)编写类文件名字以类名+Test
@Before
在所有方法之前执行(对象实例化||读取配置文件)
@Test
方法是测试方法
@Ignore
测试忽略这个方法
8.log4j
日志管理配置文件:*.xml或者*.properties
习惯 *.properties
使用:
(1)添加log4j类库
日志管理基本类库
log4j-1.2.15.jar
//扩展类库:可以使用日志和程序解耦
commons-logging.jar
(2)在src下添加log4j.properties
log4j.properties中
log4j.rootLogger=error,A1 log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} [%c]-[%p] %m%n
(3)创建日志
private static Log log = LogFactory.getLog(BookTest.class);
(4)在程序中如何记写日志
(a)applicationContext.xml中
<!-- new Book --> <bean id="book" class="com.tarena.entity.Book"> <property name="bookId"> <value>1</value> </property> <property name="bookName" value="九阳神功"/> </bean>
(b)Book类 {bookId,bookName} get(),set()方法
(c)log4j.properties
log4j.rootLogger=info,A1 log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} [%c]-[%p] %m%n
(d)测试类:
package test; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tarena.entity.Book; public class BookTest { //创建日志对象 private static Log log = LogFactory.getLog(BookTest.class); Book book; @Before public void setUp() { // 在所有方法最先执行 ApplicationContext ac = null; ac = new ClassPathXmlApplicationContext("applicationContext.xml"); book = (Book) ac.getBean("book"); } @Test @Ignore public void testBookInt() { System.out.println(book.getBookId()); } @Test public void testBookString(){ log.debug(book.getBookName()); log.info(book.getBookName()); log.warn(book.getBookName()); log.error(book.getBookName()); log.fatal(book.getBookName()); //System.out.println(book.getBookName()); } }
运行结果:
2012-01-18 19:38:03,156 [org.springframework.context.support.ClassPathXmlApplicationContext]-[INFO] Refreshingorg.springframework.context.support.ClassPathXmlApplicationContext@146c1d4: display name [org.springframework.context.support.ClassPathXmlApplicationContext@146c1d4]; startup date [Wed Jan 18 19:38:03 CST 2012]; root of context hierarchy
2012-01-18 19:38:03,218 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] Loading XML bean definitions from class path resource [applicationContext.xml]
2012-01-18 19:38:03,343 [org.springframework.context.support.ClassPathXmlApplicationContext]-[INFO] Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@146c1d4]:org.springframework.beans.factory.support.DefaultListableBeanFactory@1c6572b
2012-01-18 19:38:03,359 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[INFO] Pre-instantiating singletons inorg.springframework.beans.factory.support.DefaultListableBeanFactory@1c6572b: defining beans [book]; root of factory hierarchy
2012-01-18 19:38:03,421 [test.BookTest]-[INFO] 九阳神功
2012-01-18 19:38:03,421 [test.BookTest]-[WARN] 九阳神功
2012-01-18 19:38:03,421 [test.BookTest]-[ERROR] 九阳神功
2012-01-18 19:38:03,421 [test.BookTest]-[FATAL] 九阳神功
结论:
1. 级别有低--->高
debug-->info--warn-->error-->fatal
debug:测试程序(在框架中使用比较频繁)
info:开发框架使用info(调试程序)
warn:一些错误,不会致命(实际项目)
error:try{}catch(){ log.error()}
fatal:内存溢出
2.log4j.properties使用建议
开发项目:级别info
项目运行上线:级别error
3.log4j.properties内容说明
log4j.rootLogger=info(输出级别),A1
//在控制台输出
log4j.appender.A1=org.apache.log4j.ConsoleAppender
//输出格式
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
//输出信息
log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n
%d{yyyy-MM-dd HH:mm:ss,SSS}=%d:日志时间
[%c]:日志类路径
[%p]:日志输出级别
%m%n :输出并换行