1.什么是spring
spring是一站式框架
spring在ssh開發(fā)中是框架粘合劑
hibernate是數(shù)據(jù)持久層一站式框架
session.save(Object)
ibatis:半持久化框架
save(object)-->SQL程序員自定義
MVC!=三層框架
V C M
jsp servlet JavaBean
--------------- --------------
表示層 業(yè)務(wù)層數(shù)據(jù)訪問層
struts2 spring spring hiberante
struts ibatis
webwork JDO
JSF topLink
v:數(shù)據(jù)顯示,不做業(yè)務(wù)邏輯
c:接收用戶請求,調(diào)用JavaBean(業(yè)務(wù)|數(shù)據(jù))
相應(yīng)用戶請求,分發(fā)頁面
m:業(yè)務(wù)處理|數(shù)據(jù)處理
2、為什么學(xué)習(xí)spring
(1)流行
(2)開源簡單
(3)目前流行技術(shù)都支持spring
flex:富客戶端技術(shù)
html5
3、如何學(xué)習(xí)框架技術(shù)
先用,后了解原理
4、spring HelloWorld
spring 2.5.6
創(chuàng)建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:控制反轉(zhuǎn)
對象依賴關(guān)系,讓容器管理
DI:依賴注入
set方法完成調(diào)用者與被調(diào)用者之間依賴關(guān)系
調(diào)用者主動調(diào)用被調(diào)用者實例對象,調(diào)用者負(fù)責(zé)創(chuàng)建被調(diào)用者實例對象,耦合度比較高,需要解耦。被調(diào)用對象實例讓spring容器創(chuàng)建.
6.spring的注入方式
項目結(jié)構(gòu)圖(兩種注入方式通用)
(a)構(gòu)造方法注入
提供一個有參構(gòu)造器
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"構(gòu)造器中的第個參數(shù)
2.<ref bean="">依賴那個實現(xiàn)類
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()); } }
運(yùn)行結(jié)果:
ibatis:查找成功
ibatis:刪除成功
如果在applicationContext.xml中將<constructor-arg index="0"><ref bean="userIbatisDaoImpl"/></constructor-arg>改為
<constructor-arg index="0"><ref bean="userHibernateDaoImpl"/></constructor-arg>
運(yùn)行結(jié)果:
hibernate:查找成功
hibernate:刪除成功
(b)set注入(屬性注入||接口注入)
(1)提供一個私有屬性
(2)公有set方法,把屬性注入(DI)到spring容器
(3)在spring配置文件中實現(xiàn)依賴關(guān)系管理
接口聲明 ---> new 實現(xiàn)類實例
<property name="屬性名(接口)">
<ref bean="實現(xiàn)類">
</property>
簡寫:
<property name="屬性名" ref="實現(xiàn)類"/>
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()); } }
運(yùn)行結(jié)果:
ibatis:查找成功
ibatis:刪除成功
如果在applicationContext.xml中將<property name="iuserDao"><ref bean="userIbatisDaoImpl"/></property>改為
<property name="iuserDao"><ref bean="userHibernateDaoImpl"/></property>
則結(jié)果為
hibernate:查找成功
hibernate:刪除成功
7.junit測試:
在spring2.5.6建議使用junit4.4
(1)在項目中添加junit4.4類庫
(2)編寫類文件名字以類名+Test
@Before
在所有方法之前執(zhí)行(對象實例化||讀取配置文件)
@Test
方法是測試方法
@Ignore
測試忽略這個方法
8.log4j
日志管理配置文件:*.xml或者*.properties
習(xí)慣 *.properties
使用:
(1)添加log4j類庫
日志管理基本類庫
log4j-1.2.15.jar
//擴(kuò)展類庫:可以使用日志和程序解耦
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)創(chuàng)建日志
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 { //創(chuàng)建日志對象 private static Log log = LogFactory.getLog(BookTest.class); Book book; @Before public void setUp() { // 在所有方法最先執(zhí)行 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()); } }
運(yùn)行結(jié)果:
2012-01-18 19:38:03,156 [org.springframework.context.support.ClassPathXmlApplicationContext]-[INFO] Refreshing
org.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 in
org.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] 九陽神功
結(jié)論:
1. 級別有低--->高
debug-->info--warn-->error-->fatal
debug:測試程序(在框架中使用比較頻繁)
info:開發(fā)框架使用info(調(diào)試程序)
warn:一些錯誤,不會致命(實際項目)
error:try{}catch(){ log.error()}
fatal:內(nèi)存溢出
2.log4j.properties使用建議
開發(fā)項目:級別info
項目運(yùn)行上線:級別error
3.log4j.properties內(nèi)容說明
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 :輸出并換行
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
