一、基础JAR包

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

二、XML的配置

1、一级结构

  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>

元数据的基本结构

  1. <import resource="/resources/themeSource.xml"/> <!-- 导入bean -->
  1. <alias name="studentsManagerService" alias="studentsManagerService2"/> <!-- 别名 -->
  1. <bean id="exampleBean" class="examples.ExampleBean"/> <!-- 构造器构造 -->
  1. <bean id="exampleBean" class="examples.ExampleBean2" factory-method="createInstance"/> <!-- 静态工厂方法实例化 -->
  1. <bean id="serviceLocator" class="com.foo.DefaultServiceLocator"></bean>
  2. <bean id="exampleBean" factory-bean="serviceLocator" factory-method="createInstance"/> <!-- 实例工厂方法实例化 -->
  1. <bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao"></bean> <!-- depends-on 初始化/销毁时的依赖 -->
  1. <bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
  2. <beans default-lazy-init="true"></beans> <!-- depends-on 初始化/销毁时的依赖 -->
  1. <bean id="" class="" autowire="byType"> <!-- 自动装配 常用byName、byType, autowire-candidate="false" bean排除在自动装配之外 -->
  1. <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"> <!-- 全部检查 -->
  1. <!-- 作用域 默认singleton(单实例),prototype每次请求一个实例,request/session/global session -->
    <bean id="obj" class="com.***." init-method="init" destroy-method="destroy" scope="prototype"/>

2、参数

  1. <constructor-arg type="int" value="7500000"/>
  2. <constructor-arg index="0" value="7500000"/>
  3. <constructor-arg><bean class="x.y.Baz"/></constructor-arg> <!-- 构造器注入/静态方法参数 不提倡 -->
    <property name="beanTwo" ref="yetAnotherBean"/> <!-- Set注入 提倡 -->
  1. <value>
  2. jdbc.driver.className=com.mysql.jdbc.Driver
  3. jdbc.url=jdbc:mysql://localhost:3306/mydb
  4. </value> <!-- java.util.Properties实例 提倡 -->
  1. <idref bean="theTargetBean" /> <!-- 同value 校验bean [theTargetBean] 是否存在,不同于ref,ref为引和实例 -->
  1. <list/><set/><map/> <props/> <!-- 集合 对应ListSetMapProperties的值-->

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

  1. @Autowired(required = false)
  2. private Student student2; //自动注解,有且仅有一个bean(当添加required = false时 没有bean也不会报错 ))
  3.  
  4. @Autowired
  5. @Qualifier("cs2")
  6. private Student student2; //指定bean cs2,Autowired/Qualifier 可用于字段,Set方法,传值方法
  1. //@Resource(name="mainCatalog") 与@Autowired类似
  2. @Autowired
  3. public void prepare(@Qualifier("mainCatalog") MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
  4. this.movieCatalog = movieCatalog;
  5. this.customerPreferenceDao = customerPreferenceDao;
  6. }
  1. <context:component-scan base-package="org.example"/> <!-- 检测这些类并注册-->
  1. <!-- @Component@Repository@Service@Controller -->
  2. @Service("myMovieLister")
  3. public class SimpleMovieLister {
  4. // ...
  5. }
  6. @Repository
  7. public class MovieFinderImpl implements MovieFinder {
  8. // ...
  9. }

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拦截后执行具体操作
  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("已经记录下操作日志@Before 方法执行前");
  26. }
  27.  
  28. @Around("recordLog()")
  29. public void around(ProceedingJoinPoint pjp) throws Throwable{
  30. this.printLog("已经记录下操作日志@Around 方法执行前");
  31. pjp.proceed();
  32. this.printLog("已经记录下操作日志@Around 方法执行后");
  33. }
  34.  
  35. @After("recordLog()")
  36. public void after() {
  37. this.printLog("已经记录下操作日志@After 方法执行后");
  38. }
  39.  
  40. private void printLog(String str){
  41. System.out.println(str);
  42. }
  43. }

aspect实现(@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 常用实现

  1. <bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  2. <property name="location">
  3. <value>classpath:com/foo/jdbc.properties</value>
  4. </property>
  5. </bean>
  6. <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  7. <property name="locations">
  8. <list>
  9. <value>classpath:/spring/include/jdbc-parms.properties</value>
  10. <value>classpath:/spring/include/base-config.properties</value>
  11. </list>
  12. </property>
  13. </bean>
  14.  
  15. <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>

数据源配置,使用应用内的DBCP数据库连接池

  1. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  2. <property name="dataSource" ref="dataSource"/>
  3. </bean>

jdbcTemplate

  1. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  2. <property name="dataSource" ref="dataSource" />
  3. <!--
  4. <property name="annotatedClasses">
  5. <list>
  6. <value>com.bjsxt.model.User</value>
  7. <value>com.bjsxt.model.Log</value>
  8. </list>
  9. </property>
  10. -->
  11. <property name="packagesToScan">
  12. <list>
  13. <value>com.bjsxt.model</value>
  14. </list>
  15. </property>
  16. <property name="hibernateProperties">
  17. <props>
  18. <prop key="hibernate.dialect">
  19. org.hibernate.dialect.MySQLDialect
  20. </prop>
  21. <prop key="hibernate.show_sql">true</prop>
  22. </props>
  23. </property>
  24. </bean>
  25. <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  26. <property name="sessionFactory" ref="sessionFactory"></property>
  27. </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)

事务注解

  1. <bean id="txManager"
  2. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  3. <property name="sessionFactory" ref="sessionFactory" />
  4. </bean>
  5.  
  6. <aop:config>
  7. <aop:pointcut id="bussinessService"
  8. expression="execution(public * com.bjsxt.service..*.*(..))" />
  9. <aop:advisor pointcut-ref="bussinessService"
  10. advice-ref="txAdvice" />
  11. </aop:config>
  12.  
  13. <tx:advice id="txAdvice" transaction-manager="txManager">
  14. <tx:attributes>
  15. <tx:method name="getUser" read-only="true" />
  16. <tx:method name="add*" propagation="REQUIRED"/>
  17. </tx:attributes>
  18. </tx:advice>

事务XML配置

  1. <filter>
  2. <filter-name>encodingFilter</filter-name>
  3. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  4. <init-param>
  5. <param-name>encoding</param-name>
  6. <param-value>GBK</param-value>
  7. </init-param>
  8. </filter>

编码设置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>

发起一个页面请求时打开Hibernate的Session

三、实例化容器

1、java实例化容器

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

2、web配置

  1. <listener>
  2. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. <!-- default: /WEB-INF/applicationContext.xml -->
  4. </listener>
  5.  
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> -->
  9. <param-value>classpath:beans.xml</param-value>
  10. </context-param>

四、spring mvc

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

Spring使用总结的更多相关文章

  1. 基于spring注解AOP的异常处理

    一.前言 项目刚刚开发的时候,并没有做好充足的准备.开发到一定程度的时候才会想到还有一些问题没有解决.就比如今天我要说的一个问题:异常的处理.写程序的时候一般都会通过try...catch...fin ...

  2. 玩转spring boot——快速开始

    开发环境: IED环境:Eclipse JDK版本:1.8 maven版本:3.3.9 一.创建一个spring boot的mcv web应用程序 打开Eclipse,新建Maven项目 选择quic ...

  3. Spring基于AOP的事务管理

                                  Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...

  4. [Spring]IoC容器之进击的注解

    先啰嗦两句: 第一次在博客园使用markdown编辑,感觉渲染样式差强人意,还是github的样式比较顺眼. 概述 Spring2.5 引入了注解. 于是,一个问题产生了:使用注解方式注入 JavaB ...

  5. 学习AOP之透过Spring的Ioc理解Advisor

    花了几天时间来学习Spring,突然明白一个问题,就是看书不能让人理解Spring,一方面要结合使用场景,另一方面要阅读源代码,这种方式理解起来事半功倍.那看书有什么用呢?主要还是扩展视野,毕竟书是别 ...

  6. 学习AOP之深入一点Spring Aop

    上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...

  7. 学习AOP之认识一下Spring AOP

    心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...

  8. 为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?

    今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术框架由struts2迁移到springMVC,我突然有了一个新的疑 ...

  9. Spring之旅(2)

    Spring简化Java的下一个理念:基于切面的声明式编程 3.应用切面 依赖注入的目的是让相互协作的组件保持松散耦合:而AOP编程允许你把遍布应用各处的功能分离出来形成可重用的组件. AOP面向切面 ...

  10. Spring之旅

    Java使得以模块化构建复杂应用系统成为可能,它为Applet而来,但为组件化而留. Spring是一个开源的框架,最早由Rod Johnson创建.Spring是为了解决企业级应用开发的复杂性而创建 ...

随机推荐

  1. WinForms 新窗体后台打开完美的解决

    最近在做浏览器开发时,想要实现 IE 6那种多窗体,又允许后台打开而不抢占视野的方式. WinForms 应用程序中想要后台打开一个新的窗体,而不(抢焦).(遮挡)目前窗体. 需要注意的是,SW_SH ...

  2. WCF与WebService之间的异同

    下面我们来详细讨论一下二者的区别.Web Service和WCF的到底有什么区别. 1,Web Service:严格来说是行业标准,也就是Web Service 规范,也称作WS-*规范,既不是框架, ...

  3. Spring生态

    1.简洁有力,干掉了j2ee容器层特别是ejb,spring在rod Johnson十几年前一个人单挑j2ee体系开始,到十年前开始大行其道至今,基本上是java开发领域的事实标准.从此大部分开发者去 ...

  4. 创建MySQL从库

    我们知道Oracle有DataGuard实时备份数据.能够做主备切换,而MySQL也有自己的一套备库方案.称之为主从复制. 搭建MySQL从库是为了实时同步主库数据,同一时候也能够分担主库的读压力.对 ...

  5. uint8_t / uint16_t / uint32_t /uint64_t 是什么数据类型(转)

    在nesc的代码中,你会看到很多你不认识的数据类型,比如uint8_t等.咋一看, 好像是个新的数据类型,不过C语言(nesc是C的扩展)里面好像没有这种数据类型啊!怎么又是u又是_t的?很多人有这样 ...

  6. PHP生成条形码

    前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下. 1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息 ...

  7. LINUX命令之ETHTOOL用法详解

    转载:http://crazyming.blog.51cto.com/1048571/738022 debian 下安装: sudo apt-get install ethtool 或者下载源码编译: ...

  8. vb.net向Excel中写入值

    根据网上例子结合自己的工具环境修改后测试可以通过 我使用的工具:Microsoft Visual Studio 2010,Excel 2007 一.在D盘新建一个temp文件夹用于存放Excel启动时 ...

  9. CCTableView的使用和注意事项

    #include "cocos-ext.h" using namespace cocos2d::extension; class TableViewTestLayer: publi ...

  10. 前端必会html知识整理

    1.浏览器内核:         1.ie:trident(三叉戟)内核         2.firefox:gecko(壁虎)内核         3.safari:webkit(浏览器核心)内核 ...