<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
                        http://www.springframework.org/schema/task
                        http://www.springframework.org/schema/task/spring-task-3.1.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

	<context:annotation-config />

	<context:component-scan base-package="cn.com"  />

	<tx:annotation-driven transaction-manager="transactionManager" />

	<mvc:annotation-driven />

	<aop:aspectj-autoproxy proxy-target-class="true" />

	<!-- annotation的方法映射适配器 -->
	<bean id="handlerAdapter"
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
	<!-- annotation默认的方法映射适配器 -->
<!-- 	<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> -->
<!-- 	<property name="order" value="1" /> -->
<!-- 	</bean> -->

	<!-- 静态文件 -->
	<mvc:resources location="/assets/" mapping="/assets/**"></mvc:resources>

	<!-- 跳转文件的前后缀 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

	<!-- ================mvc ================= -->


	<!-- ==================spring context=============== -->
	<!-- 引入配置文件 -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>WEB-INF/conf/jdbc.properties</value>
				<!-- <value>WEB-INF/conf/conf.properties</value> -->
			</list>
		</property>
	</bean>

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${driver}" />
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="${initialSize}"></property>
		<!-- 连接池最大数量 -->
		<property name="maxActive" value="${maxActive}"></property>
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="${maxIdle}"></property>
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="${minIdle}"></property>
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="${maxWait}"></property>
	</bean>

	<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 自动扫描mapping.xml文件 -->
		<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
	</bean>

	<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
		<property name="basePackage" value="cn.com.**.dao" />
		<property name="properties">
			<value>mappers=cn.com.common.ssm.engine.mapper.BaseDao</value>
		</property>
	</bean>


<!-- 	(事务管理)transaction manager, use JtaTransactionManager for global tx -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- shiro -->

	<bean id="myRealm" class="cn.com.security.shiro.CustomRealm" />

	<!-- 安全管理器 -->
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="myRealm" />
	</bean>

	<!-- Shiro过滤器 -->
	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<!-- Shiro的核心安全接口,这个属性是必须的 -->
		<property name="securityManager" ref="securityManager" />
		<!-- 身份认证失败,则跳转到登录页面的配置 -->
		<property name="loginUrl" value="login.do" />
		<!-- 权限认证失败,则跳转到指定页面 -->
		<property name="unauthorizedUrl" value="/unauthor.jsp" />
		<!-- Shiro连接约束配置,即过滤链的定义 -->
		<property name="filterChainDefinitions">
			<value>
				/login.do=anon
				/**/*.do=authc
			</value>
		</property>
	</bean>

	<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
	<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

	<!-- 开启Shiro注解 -->
<!-- 	<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor" /> -->
<!-- 	<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> -->
<!-- 		<property name="securityManager" ref="securityManager" /> -->
<!-- 	</bean> -->

</beans>





Spring AOP通过代理模式实现,目前支持两种代理:JDK动态代理、CGLIB代理来创建AOP代理,Spring建议优先使用JDK动态代理。

JDK动态代理:使用java.lang.reflect.Proxy动态代理实现,即提取目标对象的接口,然后对接口创建AOP代理。

CGLIB代理:CGLIB代理不仅能进行接口代理,也能进行类代理,CGLIB代理需要注意以下问题:

不能通知final方法,因为final方法不能被覆盖(CGLIB通过生成子类来创建代理)。

会产生两次构造器调用,第一次是目标类的构造器调用,第二次是CGLIB生成的代理类的构造器调用。如果需要CGLIB代理方法,请确保两次构造器调用不影响应用。

Spring AOP默认首先使用JDK动态代理来代理目标对象,如果目标对象没有实现任何接口将使用CGLIB代理,如果需要强制使用CGLIB代理,请使用如下方式指定:

对于Schema风格配置切面使用如下方式来指定使用CGLIB代理:

java代码:


1



2


<aop:config proxy-target- class = "true" >



</aop:config>



而如果使用@AspectJ风格使用如下方式来指定使用CGLIB代理:

java代码:


1


<aop:aspectj-autoproxy proxy-target- class = "true" />







1、两种动态代理方式:

java动态代理是利用反射机制生成一个实现代理接口的匿名类,在调用具体方法前调用InvokeHandler来处理。

cglib动态代理是利用asm开源包,对代理对象类的class文件加载进来,通过修改其字节码生成子类来处理。

java动态代理要求目标类必须实现了接口,cglib动态代理则要求目标类必须有默认的构造方法。


2、Spring下使用动态代理对Controller切面

因为controller没有实现接口,因此必须使用cglib进行动态代理。

<!-- 启动对@AspectJ注解的支持 -->  
    <aop:aspectj-autoproxy <span style="color:#FF0000;">proxy-target-class="true"</span>/>

proxy-target-class为true则spring会强制使用cglib代理,否则使用java动态代理。


要实现对controller进行代理,需要将aspectj 和springmvc配置放到一个配置文件中,如下:

<!-- 启动扫描所有的controller,service -->
    <context:component-scan base-package="com.ec.core.controller"/>
    
    <bean id="methodLog" class="com.ec.base.log.MethodLogAspectJ"/>
    
    <!-- 启动对@AspectJ注解的支持 -->  
    <aop:aspectj-autoproxy proxy-target-class="true"/>











spring mvc的配置文件解析





springmvc 不带后缀 springmvc配置文件详解_mvc



  1. 使用@Controller定义一个控制器   
  2. 使用@RequestMapping映射请求   
  3. 使用@RequestParam绑定请求参数到方法参数   
  4. 使用@ModelAttribute提供一个从模型到数据的链接   
  5. 使用@SessionAttributes指定存储在会话中的属性  

 


springmvc 不带后缀 springmvc配置文件详解_mvc



    1. <context:annotation-config/>  
    2. 他的作用是隐式地向 Spring 容器注册  
    3. AutowiredAnnotationBeanPostProcessor、  
    4. CommonAnnotationBeanPostProcessor、  
    5. PersistenceAnnotationBeanPostProcessor、  
    6. RequiredAnnotationBeanPostProcessor   
    7. 这 4
    8. 例如:  
    9. 如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。  
    10. 如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。  
    11. 如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下:  
    12. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>   
    13. 如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:  
    14. <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>   
    15.    
    16.    
    17. 记得,使用注解一般都会配置扫描包路径选项  
    18. <context:component-scan base-package=”XX.XX”/>



    springmvc 不带后缀 springmvc配置文件详解_mvc



      1. <servlet>  
      2.         <servlet-name>dispatcherServlet</servlet-name>  
      3. class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
      4.         <init-param>  
      5.             <param-name>contextConfigLocation</param-name>  
      6.             <param-value>/WEB-INF/dispatcherServlet-servlet.xml</param-value>  
      7.         </init-param>  
      8. 1</load-on-startup>  
      9.     </servlet>  
      10.     <servlet-mapping>  
      11.         <servlet-name>dispatcherServlet</servlet-name>  
      12. do</url-pattern>  
      13.     </servlet-mapping>  
      14. 这个配置常常见于web.xml文件中  
      15. <load-on-startup>1</load-on-startup>是启动顺序,让这个Servlet随Servletp容器一起启动。  
      16. do</url-pattern> 会拦截*.do结尾的请求。  
      17.   
      18.   
      19.   
      20. <servlet-name>dispatcherServlet</servlet-name>这个Servlet的名字是dispatcherServlet,可以有多个DispatcherServlet,是通过名字来区分的。每一个DispatcherServlet有自己的WebApplicationContext上下文对象。同时保存的ServletContext中和Request对象中,关于key,以后说明。  
      21.    
      22. 在DispatcherServlet的初始化过程中,框架会在web应用的 WEB-INF文件夹下寻找名为[dispatcherServlet]-servlet.xml 的配置文件,生成文件中定义的bean。  
      23.   
      24.   
      25.     <init-param>  
      26.             <param-name>contextConfigLocation</param-name>  
      27.             <param-value>/WEB-INF/dispatcherServlet-servlet.xml</param-value>  
      28.         </init-param>  
      29.   
      30. 指明了配置文件的文件名,不使用默认配置文件名,而使用springMVC.xml配置文件。  
      31. 其中<param-value>**.xml</param-value> 这里可以使用多种写法  
      32. 1、不写,使用默认值:/WEB-INF/<servlet-name>-servlet.xml  
      33. 2、<param-value>/WEB-INF/classes/springMVC.xml</param-value>  
      34. 3、<param-value>classpath*:springMVC-mvc.xml</param-value>  
      35. 4、多个值用逗号分隔  
      36.   
      37.   
      38.   
      39.   
      40. springMVC-mvc.xml 配置文件片段讲解  
      41.   
      42. <context:annotation-config/>  
      43. <!-- 自动扫描的包名 -->    
      44. <context:component-scan base-package="com.iflysse"/>  
      45.  <!-- 默认的注解映射的支持 -->   
      46. <mvc:annotation-driven/>  
      47.   
      48.   
      49.   
      50.     <!-- 视图解释类 -->    
      51. class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
      52. "prefix" value="/WEB-INF/jsp/"/>    
      53. "suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑  -->    
      54. "viewClass" value="org.springframework.web.servlet.view.JstlView"
      55.     </bean>   
      56.   
      57.   
      58.   
      59.   
      60. <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。<mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。  
      61. 并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。  
      62. 后面,我们处理响应ajax请求时,就使用到了对json的支持。  
      63. 后面,对action写JUnit单元测试时,要从spring IOC容器中取DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,来完成测试,取的时候要知道是<mvc:annotation-driven />这一句注册的这两个bean。  
      64.   
      65.   
      66.   
      67.   
      68. <!-- json 支持 -->  
      69. "mappingJacksonHttpMessageConverter"
      70. class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
      71. "objectMapper" ref="commonObjectMapper"/>  
      72. "supportedMediaTypes">  
      73.             <list>  
      74. 8</value>  
      75.             </list>  
      76.         </property>  
      77.     </bean>  
      78.       
      79.     <!-- ObjectMapper json转换 -->  
      80. "commonObjectMapper" class="cn.com.starit.util.CommonObjectMapper"/>






      applicationContext.xml 文件



      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
          xmlns:cache="http://www.springframework.org/schema/cache"
          xsi:schemaLocation="
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/jdbc
          http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
          http://www.springframework.org/schema/cache
          http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop.xsd
          http://www.springframework.org/schema/util
          http://www.springframework.org/schema/util/spring-util.xsd">
      
          <<span style="color:#ff0000;">!-- 自动扫描web包 ,将带有注解的类 纳入spring容器管理 --></span>
          <context:component-scan base-package="com.eduoinfo.finances.bank.web"></context:component-scan>
      
          <!-- 引入jdbc配置文件 -->
          <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="locations">
                  <list>
                      <value>classpath*:jdbc.properties</value>
                  </list>
              </property>
          </bean>
      
          <!-- dataSource 配置 -->
          <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
              <!-- 基本属性 url、user、password -->
              <property name="url" value="${jdbc.url}" />
              <property name="username" value="${jdbc.username}" />
              <property name="password" value="${jdbc.password}" />
      
              <!-- 配置初始化大小、最小、最大 -->
              <property name="initialSize" value="1" />
              <property name="minIdle" value="1" />
              <property name="maxActive" value="20" />
      
              <!-- 配置获取连接等待超时的时间 -->
              <property name="maxWait" value="60000" />
      
              <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
              <property name="timeBetweenEvictionRunsMillis" value="60000" />
      
              <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
              <property name="minEvictableIdleTimeMillis" value="300000" />
      
              <property name="validationQuery" value="SELECT 'x'" />
              <property name="testWhileIdle" value="true" />
              <property name="testOnBorrow" value="false" />
              <property name="testOnReturn" value="false" />
      
              <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
              <property name="poolPreparedStatements" value="false" />
              <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
      
              <!-- 配置监控统计拦截的filters -->
              <property name="filters" value="stat" />
          </bean>
      
          <!-- mybatis文件配置,扫描所有mapper文件 -->
          <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml" p:mapperLocations="classpath:com/eduoinfo/finances/bank/web/dao/*.xml" />
      
          <span style="color:#ff0000;"><!-- spring与mybatis整合配置,扫描所有dao --></span>
          <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:basePackage="com.eduoinfo.finances.bank.web.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory" />
      
          <!-- 对dataSource 数据源进行事务管理 -->
          <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />
      
          <span style="color:#ff0000;"><!-- 配置使Spring采用CGLIB代理 --></span>
          <aop:aspectj-autoproxy proxy-target-class="true" />
      
         <span style="color:#ff0000;"> <!-- 启用对事务注解的支持 --></span>
          <tx:annotation-driven transaction-manager="transactionManager" />
      
      <span style="color:#ff0000;">    <!-- Cache配置 --></span>
          <cache:annotation-driven cache-manager="cacheManager" />
          <bean id="ehCacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml" />
          <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManagerFactory" />
      
      </beans>





      1、<context:component-scan base-package="com.eduoinfo.finances.bank.web"></context:component-scan> 作用


      @Component,@Service,@Controller,@Repository) 注解的 类  纳入spring容器管理

      在类上 ,使用以下注解,实现bean 的声明

      @Component 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
       @Service 用于标注业务层组件
       @Controller 用于标注控制层组件(如srping mvc的controller,struts中的action)
       @Repository 用于标注数据访问组件,即DAO组件
       示例:
      @Controller
       @RequestMapping(value = "/test")
       public class TestController {}
      ------------------

      ------------------------------------------------------------------------------------------------

      在类的成员变量上,使用以下注解,实现属性的自动装配

      @Autowired : 按类 的 类型进行装配

      @Resource (推荐) : 1 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常

          2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常 


          4.如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;

      @Resource注解在字段上,这样就不用写setter方法了,并且这个注解是属于J2EE的,减少了与spring的耦合。 

      示例:

      @Resource
       private TestServiceImpl testServiceImpl;