日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring使用总结

發(fā)布時(shí)間:2025/5/22 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring使用总结 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一、基礎(chǔ)JAR包

spring-beans.jar spring-context.jar spring-core.jar spring-expression.jar

?

二、XML的配置

1、一級(jí)結(jié)構(gòu)

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 6 <bean id="..." class="..."> 7 <!-- collaborators and configuration for this bean go here --> 8 </bean> 9 </beans> 元數(shù)據(jù)的基本結(jié)構(gòu) <import resource="/resources/themeSource.xml"/> <!-- 導(dǎo)入bean --> <alias name="studentsManagerService" alias="studentsManagerService2"/> <!-- 別名 --> <bean id="exampleBean" class="examples.ExampleBean"/> <!-- 構(gòu)造器構(gòu)造 --> <bean id="exampleBean" class="examples.ExampleBean2" factory-method="createInstance"/> <!-- 靜態(tài)工廠方法實(shí)例化 --> <bean id="serviceLocator" class="com.foo.DefaultServiceLocator"></bean> <bean id="exampleBean" factory-bean="serviceLocator" factory-method="createInstance"/> <!-- 實(shí)例工廠方法實(shí)例化 --> <bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao"></bean> <!-- depends-on 初始化/銷毀時(shí)的依賴 --> <bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/> <beans default-lazy-init="true"></beans> <!-- depends-on 初始化/銷毀時(shí)的依賴 --> <bean id="" class="" autowire="byType"> <!-- 自動(dòng)裝配 常用byName、byType, autowire-candidate="false" bean排除在自動(dòng)裝配之外 --> <bean id="ernie" class="com.***." dependency-check="none"> <!-- 默認(rèn)值,不進(jìn)行任何檢查 --><bean id="ernie" class="com.***." dependency-check="simple"> <!-- 簡(jiǎn)單類型屬性以及集合類型檢查 --><bean id="ernie" class="com.***." dependency-check="object"> <!-- 引用類型檢查 --><bean id="ernie" class="com.***." dependency-check="all"> <!-- 全部檢查 --> <!-- 作用域 默認(rèn)singleton(單實(shí)例),prototype每次請(qǐng)求一個(gè)實(shí)例,request/session/global session -->
<
bean id="obj" class="com.***." init-method="init" destroy-method="destroy" scope="prototype"/>

?

2、參數(shù)

<constructor-arg type="int" value="7500000"/> <constructor-arg index="0" value="7500000"/> <constructor-arg><bean class="x.y.Baz"/></constructor-arg> <!-- 構(gòu)造器注入/靜態(tài)方法參數(shù) 不提倡 -->
<property name="beanTwo" ref="yetAnotherBean"/> <!-- Set注入 提倡 --> <value>jdbc.driver.className=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/mydb </value> <!-- java.util.Properties實(shí)例 提倡 --> <idref bean="theTargetBean" /> <!-- 同value 校驗(yàn)bean [theTargetBean] 是否存在,不同于ref,ref為引和實(shí)例 --> <list/><set/><map/> <props/> <!-- 集合 對(duì)應(yīng)List、Set、Map及Properties的值-->

?

3、注解

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 9 10 <context:annotation-config/> 11 12 </beans> 注解(<context:annotation-config/>)

?spring注解命名空間(org.springframework.beans.factory.annotation.*) //常用:Autowired、Qualifier

?javax注解命名空間(javax.annotation.*) //常用:Resource、PostConstruct、PreDestroy

?spring組件命名空間(org.springframework.stereotype.*) //常用:Component、 Repository、Service、Controller

@Autowired(required = false) private Student student2; //自動(dòng)注解,有且僅有一個(gè)bean(當(dāng)添加required = false時(shí) 沒有bean也不會(huì)報(bào)錯(cuò) )) @Autowired @Qualifier("cs2") private Student student2; //指定bean cs2,Autowired/Qualifier 可用于字段,Set方法,傳值方法 //@Resource(name="mainCatalog") 與@Autowired類似 @Autowired public void prepare(@Qualifier("mainCatalog") MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {this.movieCatalog = movieCatalog;this.customerPreferenceDao = customerPreferenceDao; } <context:component-scan base-package="org.example"/> <!-- 檢測(cè)這些類并注冊(cè)-->
<!-- @Component、 @Repository、@Service或 @Controller --> @Service("myMovieLister") public class SimpleMovieLister {// ... } @Repository public class MovieFinderImpl implements MovieFinder {// ... }

?

?4、面向切面

添加jar包:

  spring : spring-aop.jar、spring-aspects

???? aspect : aspectjrt.jar、aspectjtools.jar

??????? item : aopalliance.jar

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-3.0.xsd 9 http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 13 <context:annotation-config /> 14 <context:component-scan base-package="com.itsoft"/> 15 <aop:aspectj-autoproxy /> 16 </beans> xml配置 1 package com.itsoft; 2 3 import org.aspectj.lang.ProceedingJoinPoint; 4 import org.aspectj.lang.annotation.After; 5 import org.aspectj.lang.annotation.Around; 6 import org.aspectj.lang.annotation.Aspect; 7 import org.aspectj.lang.annotation.Before; 8 import org.aspectj.lang.annotation.Pointcut; 9 import org.springframework.stereotype.Component; 10 11 /** 12 * 13 * @author Administrator 14 * 通過aop攔截后執(zhí)行具體操作 15 */ 16 @Aspect 17 @Component 18 public class LogIntercept { 19 20 @Pointcut("execution(public * com.itsoft.action..*.*(..))") 21 public void recordLog(){} 22 23 @Before("recordLog()") 24 public void before() { 25 this.printLog("已經(jīng)記錄下操作日志@Before 方法執(zhí)行前"); 26 } 27 28 @Around("recordLog()") 29 public void around(ProceedingJoinPoint pjp) throws Throwable{ 30 this.printLog("已經(jīng)記錄下操作日志@Around 方法執(zhí)行前"); 31 pjp.proceed(); 32 this.printLog("已經(jīng)記錄下操作日志@Around 方法執(zhí)行后"); 33 } 34 35 @After("recordLog()") 36 public void after() { 37 this.printLog("已經(jīng)記錄下操作日志@After 方法執(zhí)行后"); 38 } 39 40 private void printLog(String str){ 41 System.out.println(str); 42 } 43 } aspect實(shí)現(xiàn)(@Around @Before @After) 1 <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean> 2 <aop:config> 3 <aop:aspect id="logAspect" ref="logInterceptor"> 4 <aop:before method="before" pointcut="execution(public * com.bjsxt.service..*.add(..))" /> 5 </aop:aspect> 6 </aop:config> XML替代注解

?

5、Spring 常用實(shí)現(xiàn)

<bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location"><value>classpath:com/foo/jdbc.properties</value></property> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:/spring/include/jdbc-parms.properties</value><value>classpath:/spring/include/base-config.properties</value></list></property> </bean><context:property-placeholder location="classpath:com/foo/jdbc.properties"/> <!-- 簡(jiǎn)化 spring2.5 支持,多個(gè)以逗號(hào)(,)分開 --> 屬性占位符PropertyPlaceholderConfigurer 1 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 2 <!--Connection Info --> 3 <property name="driverClassName" value="${jdbc.driver}" /> 4 <property name="url" value="${jdbc.url}" /> 5 <property name="username" value="${jdbc.username}" /> 6 <property name="password" value="${jdbc.password}" /> 7 8 <!--Connection Pooling Info --> 9 <property name="initialSize" value="5" /> 10 <property name="maxActive" value="100" /> 11 <property name="maxIdle" value="30" /> 12 <property name="maxWait" value="10000" /> 13 <property name="poolPreparedStatements" value="true" /> 14 <property name="defaultAutoCommit" value="true" /> 15 </bean> 數(shù)據(jù)源配置,使用應(yīng)用內(nèi)的DBCP數(shù)據(jù)庫(kù)連接池 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/></bean> jdbcTemplate <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- <property name="annotatedClasses"><list><value>com.bjsxt.model.User</value><value>com.bjsxt.model.Log</value></list></property>--><property name="packagesToScan"><list><value>com.bjsxt.model</value></list></property><property name="hibernateProperties"><props><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop></props></property></bean><bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"><property name="sessionFactory" ref="sessionFactory"></property></bean> hibernateTemplate、sessionFactory 1 Xml 2 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 3 <property name="dataSource" ref="dataSource"/> 4 </bean> 5 6 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 7 <property name="sessionFactory" ref="sessionFactory" /> 8 </bean> 9 10 <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> 11 12 Java 13 @Transactional(readOnly=true) 事務(wù)注解 <bean id="txManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /></bean><aop:config><aop:pointcut id="bussinessService"expression="execution(public * com.bjsxt.service..*.*(..))" /><aop:advisor pointcut-ref="bussinessService"advice-ref="txAdvice" /></aop:config><tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="getUser" read-only="true" /><tx:method name="add*" propagation="REQUIRED"/></tx:attributes></tx:advice> 事務(wù)XML配置 <filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>GBK</param-value></init-param></filter> 編碼設(shè)置CharacterEncodingFilter 1 <filter> 2 <filter-name>openSessionInView</filter-name> 3 <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 4 <init-param> 5 <param-name>singleSession</param-name> 6 <param-value>true</param-value> 7 </init-param> 8 <init-param> 9 <param-name>sessionFactoryBeanName</param-name> 10 <param-value>sf</param-value> 11 </init-param> 12 <init-param> 13 <param-name>flushMode</param-name> 14 <param-value>AUTO</param-value> 15 </init-param> 16 </filter> 發(fā)起一個(gè)頁(yè)面請(qǐng)求時(shí)打開Hibernate的Session

?

三、實(shí)例化容器

1、java實(shí)例化容器

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/products.xml");

?2、web配置

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!-- default: /WEB-INF/applicationContext.xml --></listener><context-param><param-name>contextConfigLocation</param-name><!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> --><param-value>classpath:beans.xml</param-value></context-param>

?

四、spring mvc

http://elf8848.iteye.com/blog/875830/

轉(zhuǎn)載于:https://www.cnblogs.com/Nadim/p/4724875.html

總結(jié)

以上是生活随笔為你收集整理的Spring使用总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 尤物在线精品 | 97av.com| 国产精品久久久久久久蜜臀 | 国产精品23p | 视频在线观看电影完整版高清免费 | 国产精品九九九 | www.欧美日韩 | 久久天堂影院 | 中文字幕一区二区视频 | 国产欧美日韩二区 | 中出在线播放 | 中国毛片网站 | 国产三级国产精品国产国在线观看 | 一区二区国产电影 | 日韩高清国产一区在线 | 久久久www成人免费无遮挡大片 | 精品视频一区二区三区四区 | 午夜免费成人 | 日本加勒比一区 | 成年人网站在线观看视频 | av巨作 | 成人在线观看视频网站 | 欧美综合亚洲图片综合区 | 女人18毛片水真多 | 无码人妻丰满熟妇精品 | 国产97av | 欧美福利网 | 成人av色| 欧美资源网 | 香蕉视频网站在线 | 色老头在线视频 | 亚洲欧美韩国 | 女人18毛片水真多 | 激情 亚洲| 一区二区三区四区国产精品 | 国产在线网站 | 热久久亚洲 | 天天干天天操天天插 | 美女的诞生免费观看在线高清 | 国产成人精品a视频 | 裸体喂奶一级裸片 | av免费片 | 久久久久久亚洲精品中文字幕 | 亚洲天堂福利视频 | 久草高清视频 | 男人插入女人下面视频 | 久久一线 | 中国吞精videos露脸 | 亚洲成人久久精品 | 最好看的日本字幕mv视频大全 | 人妻少妇一区二区 | 爱爱网站视频 | 懂色av一区二区三区免费观看 | 狠狠cao日日穞夜夜穞av | 欧洲色视频 | 欧美交换国产一区内射 | 欧美性天天| 深夜成人在线观看 | 91精品国产91久久久久久黑人 | 国产精品激情 | www黄色大片 | 国产视频一区二区在线播放 | 国产女人和拘做受视频免费 | 蜜桃色999 | 97影院手机版 | 日本久久一区二区 | 日韩高清国产一区在线 | 亚洲在线视频免费观看 | 青青草视频免费播放 | 欧洲精品久久久久毛片完整版 | 国产乱淫av免费 | 久久久久久不卡 | 亚洲精品国产精品乱码视色 | 精品无码国产av一区二区三区 | 91麻豆产精品久久久久久 | 被两个男人吃奶三p爽文 | 理论片毛片 | 欧美激情性做爰免费视频 | 日本不卡视频一区二区 | 不卡视频免费在线观看 | 永久av | 曰批视频在线观看 | 女人喷潮完整视频 | 日韩欧美综合视频 | 亚洲骚| 日韩伦理av | 性色av一区二区三区免费 | 伊人22综合| 日产亚洲一区二区三区 | 色播在线视频 | 国产又黄又粗的视频 | 伊人成年综合网 | 亚洲影视一区二区 | 国产成人一区二区三区别 | 人碰人人| 又污又黄的视频 | 中文字幕在线视频精品 | 日韩啪啪网站 | 国产黄色一区二区三区 |