javascript
Spring使用总结
一、基礎(chǔ)JAR包
spring-beans.jar spring-context.jar spring-core.jar spring-expression.jar
?
二、XML的配置
1、一級結(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)工廠方法實例化 --> <bean id="serviceLocator" class="com.foo.DefaultServiceLocator"></bean> <bean id="exampleBean" factory-bean="serviceLocator" factory-method="createInstance"/> <!-- 實例工廠方法實例化 --> <bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao"></bean> <!-- depends-on 初始化/銷毀時的依賴 --> <bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/> <beans default-lazy-init="true"></beans> <!-- depends-on 初始化/銷毀時的依賴 --> <bean id="" class="" autowire="byType"> <!-- 自動裝配 常用byName、byType, autowire-candidate="false" bean排除在自動裝配之外 --> <bean id="ernie" class="com.***." dependency-check="none"> <!-- 默認值,不進行任何檢查 --><bean id="ernie" class="com.***." dependency-check="simple"> <!-- 簡單類型屬性以及集合類型檢查 --><bean id="ernie" class="com.***." dependency-check="object"> <!-- 引用類型檢查 --><bean id="ernie" class="com.***." dependency-check="all"> <!-- 全部檢查 --> <!-- 作用域 默認singleton(單實例),prototype每次請求一個實例,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實例 提倡 --> <idref bean="theTargetBean" /> <!-- 同value 校驗bean [theTargetBean] 是否存在,不同于ref,ref為引和實例 --> <list/>、<set/>、<map/> <props/> <!-- 集合 對應(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; //自動注解,有且僅有一個bean(當(dāng)添加required = false時 沒有bean也不會報錯 )) @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"/> <!-- 檢測這些類并注冊--><!-- @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實現(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 常用實現(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"/> <!-- 簡化 spring2.5 支持,多個以逗號(,)分開 --> 屬性占位符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ù)庫連接池 <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ā)起一個頁面請求時打開Hibernate的Session?
三、實例化容器
1、java實例化容器
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使用总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hdu 3629 Convex
- 下一篇: 570D Codeforces Rou