filter获取spring的bean

这是我学习过程中的例子,filter直接获取spring的bean

1、spring容器是不可以注入filter的,有些人说他们俩是平级关系

2、spring处理的是servlet,filter类似于是他外面包裹的一层网。

 

 

 

spring配置:

我用的是mybatis的接口注入

<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:util="http://www.springframework.org/schema/util"                             
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"
        default-lazy-init="true">
       
        <description>spring配置</description>
       
        <!-- 加载数据库文件 -->
      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" value="classpath:config.properties" />
 </bean>

       <!-- 配置数据库dbcp池链接 -->
       <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>
       <bean id="txManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="dataSource"></property>
       </bean>
        <tx:advice id ="txAdvice"  transaction-manager="txManager">
                <tx:attributes>
                    <tx:method name="get*" read-only="true"/>
              <tx:method name="select*" read-only="true"/>
              <tx:method name="queryFor*" read-only="true"/>
              <tx:method name="*"/>
                </tx:attributes>
        </tx:advice>
        <bean id="sqlSessionFactory"  class="org.mybatis.spring.SqlSessionFactoryBean" >
          <property name="dataSource" ref="dataSource"></property>
           <!-- 自动扫描mapping.xml文件 删除正常
     <property name="mapperLocations" value="classpath:com/botech/skynet/base/persistence/*.xml" />
     -->
        </bean>
          <!-- 配置sqlSessionTemplate -->
     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
         <constructor-arg index="0" ref="sqlSessionFactory" />
     </bean>
   
        <!-- 配置mybatis的sqlMap及接口的映射 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
         <property name="basePackage" value="com.botech.skynet.base.persistence"/>
     </bean>
 
   <!-- 扫描控件,不包括controller -->
        <context:component-scan base-package="com.botech">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
      </context:component-scan>
   
</beans>

 

 

fliter源码:

package com.botech.skynet.common;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.botech.ywzx.index.service.IUserService;
import com.botech.ywzx.index.vo.User;

/**
 * Servlet Filter implementation class LoginFilter
 */
public class LoginFilter implements Filter {
 
    /**
     * Default constructor.
     */
    public LoginFilter() {
        // TODO Auto-generated constructor stub
    }

 /**
  * @see Filter#destroy()
  */
 public void destroy() {
  // TODO Auto-generated method stub
  System.out.println("销毁----------------------------->");
 }

 /**
  * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
  */
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  // TODO Auto-generated method stub
  // place your code here
          UserService userService= (IUserService) wac.getBean("userService");
         User u = userService.getUserById(1);
         System.out.println("获取springbean成功:"+u.getUserName());
  // pass the request along the filter chain
  chain.doFilter(request, response);
 }

 /**
  * @see Filter#init(FilterConfig)
  */
 private WebApplicationContext wac;
 public void init(FilterConfig fConfig) throws ServletException {
  System.out.println("初始化:------------------------------>"+fConfig);
  wac =WebApplicationContextUtils.getRequiredWebApplicationContext( fConfig.getServletContext());
//           wac = WebApplicationContextUtils.getWebApplicationContext(
//           this.getServletContext());
  // TODO Auto-generated method stub
 }

}

转载于:https://www.cnblogs.com/1001lsj/p/5066788.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值