SQL程序員自定義MVC!=三層框架VCMjspservletJavaBean-----------------------------表示層業(yè)務(wù)層數(shù)據(jù)訪(fǎng)問(wèn)層struts2springspringhiberantestrutsibatisw" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Spring入門(mén)

系統(tǒng) 2030 0

1.什么是spring
spring是一站式框架
spring在ssh開(kāi)發(fā)中是框架粘合劑

hibernate是數(shù)據(jù)持久層一站式框架
session.save(Object)
ibatis:半持久化框架
save(object)-->SQL程序員自定義

MVC!=三層框架
V C M
jsp servlet JavaBean
--------------- --------------
表示層 業(yè)務(wù)層數(shù)據(jù)訪(fǎng)問(wèn)層
struts2 spring spring hiberante
struts ibatis
webwork JDO
JSF topLink

v:數(shù)據(jù)顯示,不做業(yè)務(wù)邏輯
c:接收用戶(hù)請(qǐng)求,調(diào)用JavaBean(業(yè)務(wù)|數(shù)據(jù))
相應(yīng)用戶(hù)請(qǐng)求,分發(fā)頁(yè)面
m:業(yè)務(wù)處理|數(shù)據(jù)處理

2、為什么學(xué)習(xí)spring
(1)流行
(2)開(kāi)源簡(jiǎn)單
(3)目前流行技術(shù)都支持spring
flex:富客戶(hù)端技術(shù)
html5

3、如何學(xué)習(xí)框架技術(shù)
先用,后了解原理

4、spring HelloWorld
spring 2.5.6
創(chuàng)建Java Project
(1)添加spring類(lèi)庫(kù)
spring-framework-2.5.6/dist/spring.jar
日志管理類(lèi)庫(kù)
spring-framework-2.5.6/lib/log4j-1.2.15.jar
spring-framework-2.5.6/lib/log4j/commons-logging.jar

(2)自定義類(lèi)庫(kù)包
windows---->preferences--->Java---->Build Path------>User Libraries----->new
然后再項(xiàng)目中右擊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)
對(duì)象依賴(lài)關(guān)系,讓容器管理
DI:依賴(lài)注入
set方法完成調(diào)用者與被調(diào)用者之間依賴(lài)關(guān)系

調(diào)用者主動(dòng)調(diào)用被調(diào)用者實(shí)例對(duì)象,調(diào)用者負(fù)責(zé)創(chuàng)建被調(diào)用者實(shí)例對(duì)象,耦合度比較高,需要解耦。被調(diào)用對(duì)象實(shí)例讓spring容器創(chuàng)建.

6.spring的注入方式

項(xiàng)目結(jié)構(gòu)圖(兩種注入方式通用)

Spring入門(mén)

(a)構(gòu)造方法注入
提供一個(gè)有參構(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>

  

注釋?zhuān)?

1.index="0"構(gòu)造器中的第個(gè)參數(shù)

2.<ref bean="">依賴(lài)那個(gè)實(shí)現(xiàn)類(lèi)

3.在UserServImpl類(lèi)中添加

    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)提供一個(gè)私有屬性
(2)公有set方法,把屬性注入(DI)到spring容器
(3)在spring配置文件中實(shí)現(xiàn)依賴(lài)關(guān)系管理
接口聲明 ---> new 實(shí)現(xiàn)類(lèi)實(shí)例
<property name="屬性名(接口)">
<ref bean="實(shí)現(xiàn)類(lèi)">
</property>

簡(jiǎn)寫(xiě):
<property name="屬性名" ref="實(shí)現(xiàn)類(lèi)"/>

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>
	 -->
	<!-- 簡(jiǎn)寫(xiě) -->
	<property name="iuserDao" ref="userIbatisDaoImpl"></property>
	</bean>
</beans>

  

在UserServImpl類(lèi)中,添加

    private IUserDao iuserDao;
	
	public void setIuserDao(IUserDao iuserDao) {
		this.iuserDao = iuserDao;
	}
  


在UserAction類(lèi)中

    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測(cè)試:
在spring2.5.6建議使用junit4.4
(1)在項(xiàng)目中添加junit4.4類(lèi)庫(kù)
(2)編寫(xiě)類(lèi)文件名字以類(lèi)名+Test
@Before
在所有方法之前執(zhí)行(對(duì)象實(shí)例化||讀取配置文件)
@Test
方法是測(cè)試方法
@Ignore
測(cè)試忽略這個(gè)方法

8.log4j
日志管理配置文件:*.xml或者*.properties
習(xí)慣 *.properties
使用:

(1)添加log4j類(lèi)庫(kù)
日志管理基本類(lèi)庫(kù)
log4j-1.2.15.jar
//擴(kuò)展類(lèi)庫(kù):可以使用日志和程序解耦
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)在程序中如何記寫(xiě)日志

(a)applicationContext.xml中

    <!-- new Book -->
<bean id="book" class="com.tarena.entity.Book">
	<property name="bookId">
		<value>1</value>
	</property>
	<property name="bookName" value="九陽(yáng)神功"/>
</bean>
  

(b)Book類(lèi) {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)測(cè)試類(lèi):

    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)建日志對(duì)象
	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] 九陽(yáng)神功
2012-01-18 19:38:03,421 [test.BookTest]-[WARN] 九陽(yáng)神功
2012-01-18 19:38:03,421 [test.BookTest]-[ERROR] 九陽(yáng)神功
2012-01-18 19:38:03,421 [test.BookTest]-[FATAL] 九陽(yáng)神功

結(jié)論:
1. 級(jí)別有低--->高
debug-->info--warn-->error-->fatal

debug:測(cè)試程序(在框架中使用比較頻繁)
info:開(kāi)發(fā)框架使用info(調(diào)試程序)
warn:一些錯(cuò)誤,不會(huì)致命(實(shí)際項(xiàng)目)
error:try{}catch(){ log.error()}
fatal:內(nèi)存溢出
2.log4j.properties使用建議

開(kāi)發(fā)項(xiàng)目:級(jí)別info
項(xiàng)目運(yùn)行上線(xiàn):級(jí)別error

3.log4j.properties內(nèi)容說(shuō)明

log4j.rootLogger=info(輸出級(jí)別),A1
//在控制臺(tái)輸出
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:日志時(shí)間
[%c]:日志類(lèi)路徑
[%p]:日志輸出級(jí)別
%m%n :輸出并換行

Spring入門(mén)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 洪江市| 合作市| 天津市| 瑞昌市| 杂多县| 巴塘县| 关岭| 宾阳县| 新龙县| 新疆| 澎湖县| 彭阳县| 玛多县| 余江县| 阳新县| 南和县| 巴彦县| 绿春县| 博乐市| 娄烦县| 安新县| 建瓯市| 沭阳县| 林芝县| 河北省| 曲周县| 阿拉尔市| 卢龙县| 昆明市| 合作市| 宁南县| 石嘴山市| 饶阳县| 沂水县| 瑞丽市| 洪洞县| 嘉兴市| 虞城县| 长葛市| 石门县| 乌鲁木齐县|