spring 整合其他框架配置

1.spring自身:

    spring如果想开启,类的实例化就必须在ApplicationContext中使用getBean方法来获取,或者在web.xml中配置listener

(1)<context:component-scan base-package="xxx.xxx.xxx"/>
以上会自动扫描类中的注解,如果不加入这一句,则类中的注解不会生效
(2)<context:property-placeholder location="classpath:*.properties" file-encoding="UTF-8"/>加载.properties文件
(3) <aop:config></aop:config>
这个会引入springAOP的功能,可以配置切面.
子标签有<aop:before></aop:before>
<aop:after-returning></aop:after-returning>和<aop:around></aop:around>
完整例子(来源与互联网):
<!-- 配置AOP 切面 -->
    <aop:config>
        <!-- 定义切点函数 -->
        <aop:pointcut id="pointcut" expression="execution(* com.zejian.spring.springAop.dao.ProductDao.add(..))" />

        <!-- 定义其他切点函数 -->
        <aop:pointcut id="delPointcut" expression="execution(* com.zejian.spring.springAop.dao.ProductDao.delete(..))" />

        <!-- 定义通知 order 定义优先级,值越小优先级越大-->
        <aop:aspect ref="myAspectXML" order="0">
            <!-- 定义通知
            method 指定通知方法名,必须与MyAspectXML中的相同
            pointcut 指定切点函数
            -->
            <aop:before method="before" pointcut-ref="pointcut" />

            <!-- 后置通知  returning="returnVal" 定义返回值 必须与类中声明的名称一样-->
            <aop:after-returning method="afterReturn" pointcut-ref="pointcut"  returning="returnVal" />

            <!-- 环绕通知 -->
            <aop:around method="around" pointcut-ref="pointcut"  />

            <!--异常通知 throwing="throwable" 指定异常通知错误信息变量,必须与类中声明的名称一样-->
            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="throwable"/>

            <!--
                 method : 通知的方法(最终通知)
                 pointcut-ref : 通知应用到的切点方法
            -->
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>
(4)<bean></bean> spring的其他配置,将对象的实例化交给容器

2.整合spring-MVC

必须项:

1)开启注解:<mvc:annotation-driven /> 
2)配置视图解析器:<bean   class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/pages/"></property>  
        <property name="suffix" value=".jsp"></property>  
    </bean> 

注意,在整合springMVC后在spring中的扫描注解中要略过@Controller:

<context:component-scan base-package="xxx.xxx">  
        <!-- 扫描时跳过 @Controller 注解的JAVA类(控制器) -->  
        <context:exclude-filter type="annotation"  
            expression="org.springframework.stereotype.Controller" />  
    </context:component-scan> 
并且在web.xml中要将springMVC.xml和ApplicationContext.xml一起引入(来源与互联网):
<!-- 配置spring ioc容器 -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:config/applicationContext.xml</param-value>  
    </context-param>  
  
    <!-- Bootstraps the root web application context before servlet initialization -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
  
    <!-- 配置springmvc 的DispatcherServlet -->  
    <servlet>  
        <servlet-name>dispatcherServlet</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:config/spring-mvc.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
  
    <!-- Map all requests to the DispatcherServlet for handling -->  
    <servlet-mapping>  
        <servlet-name>dispatcherServlet</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>  
  
    <filter>  
        <filter-name>CharacterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>CharacterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  

    如果想加入文件上传下载:

<bean id="multipartResolver"
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
        <property name="maxUploadSize" value="5000000"/>
    </bean>
@responseBody修改为默认UTF-8:
<bean
            class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJacksonHttpMessageConverter"/>
            </list>
        </property>
    </bean>
    <bean id="mappingJacksonHttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
                <!-- 注意 这是传输json格式,无论是ajax还是外部接口传输-->
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>

3.整合MyBatis:

1)配置数据源<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close">
            <property name="driverClassName" value="${driverClassName}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
        </bean>
2)配置SessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
</bean>
3)配置MyBatis包扫描
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     <property name="basePackage" value="com.allinpay.mapper.mapper"/>
</bean>

没有更多推荐了,返回首页