导航:首页 > 净水问答 > spring过滤器注解

spring过滤器注解

发布时间:2021-03-16 07:41:34

A. spring常用注解有哪些

springmvc常用注解标签详解
1、@Controller
在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Model 返回给对应的View 进行展示。在SpringMVC 中提供了一个非常简便的定义Controller 的方法,你无需继承特定的类或实现特定的接口,只需使用@Controller 标记一个类是Controller ,然后使用@RequestMapping 和@RequestParam 等一些注解用以定义URL 请求和Controller 方法之间的映射,这样的Controller 就能被外界访问到。此外Controller 不会直接依赖于HttpServletRequest 和HttpServletResponse 等HttpServlet 对象,它们可以通过Controller 的方法参数灵活的获取到。
@Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping 注解。@Controller 只是定义了一个控制器类,而使用@RequestMapping 注解的方法才是真正处理请求的处理器。单单使用@Controller 标记在一个类上还不能真正意义上的说它就是SpringMVC 的一个控制器类,因为这个时候Spring 还不认识它。那么要如何做Spring 才能认识它呢?这个时候就需要我们把这个控制器类交给Spring 来管理。有两种方式:
(1)在SpringMVC 的配置文件中定义MyController 的bean 对象。
(2)在SpringMVC 的配置文件中告诉Spring 该到哪里去找标记为@Controller 的Controller 控制器。
2、@RequestMapping
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
3、@Resource和@Autowired
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入。
4、@ModelAttribute和 @SessionAttributes
代表的是:该Controller的所有方法在调用前,先执行此@ModelAttribute方法,可用于注解和方法参数中,可以把这个@ModelAttribute特性,应用在BaseController当中,所有的Controller继承BaseController,即可实现在调用Controller时,先执行@ModelAttribute方法。
@SessionAttributes即将值放到session作用域中,写在class上面。
具体示例参见下面:使用 @ModelAttribute 和 @SessionAttributes 传递和保存数据
5、@PathVariable
用于将请求URL中的模板变量映射到功能处理方法的参数上,即取出uri模板中的变量作为参数。
6、@requestParam
@requestParam主要用于在SpringMVC后台控制层获取参数,类似一种是request.getParameter("name"),它有三个常用参数:defaultValue = "0", required = false, value = "isApp";defaultValue 表示设置默认值,required 铜过boolean设置是否是必须要传入的参数,value 值表示接受的传入的参数类型。
7、@ResponseBody
作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
8、@Component
相当于通用的注解,当不知道一些类归到哪个层时使用,但是不建议。
9、@Repository
用于注解层,在Impl类上面注解。

B. 如何在FILTER过滤器中注入SPRING的BEAN

<filter> <filter-name>DelegatingFilterProxy</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetBeanName</param-name> <param-value>myFilter</param-value> //自己抄过滤器的名字 </init-param> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter -mapping> <filter-name>DelegatingFilterProxy</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

C. 在Spring中怎么拦截自定义的注解

@Aspect
@Component//加上这个
public class MyInterceptor {
@Pointcut("execution(public * com.newer.service.imp.PersonServiecBean.save*(..))")
private void anyMethod(){}

@After(value="anyMethod()")
public void doAccess(){
System.out.println("前置通知!!");
}

}
<bean id="personService" class="com.newer.service.impl.PersonServiecBean"/>
配置里面把myInterceptor去掉

如果还不行 干脆用xml的形式
<bean id="personService" class="com.newer.servic e.impl.PersonServiecBean"/>
<bean id="myInterceptor" class="com.newer.service.MyInterceptor" />

<aop:config>

<aop:aspect id="logAspect" ref="myInterceptor">
<aop:before method="before" pointcut=("execution(public * com.newer.service.imp.PersonServiecBean.save*(..))") />
</aop:aspect>

</aop:config>

D. spring在filter里面怎么获取注解方式定义的bean

WebApplicationContext wac = WebApplicationContextUtils.(getServletContext()); 有WebApplicationContext 了对象了 spring托管来的自所有对象都可以拿到了。 当然不推荐这种方式,一般是注入的方式

E. 请教怎么用Spring的注解方式把一个Bean注入到过滤器中

参考:
UsersConnectionRepository bean =
(UsersConnectionRepository)WebApplicationContextUtils.
(filterConfig.getServletContext()).
getBean("usersConnectionRepository");

下面是优化的写法:

UsersConnectionRepository bean = WebApplicationContextUtils.
(filterConfig.getServletContext()).
getBean(UsersConnectionRepository.class);

F. spring中有没有注解拦截器,只拦截注解

可以啊有关AOP@Aspect的切点注释。列
// 切点注释方法
@Aspect
public class VisitHistory {
/**
* @param JoinPoint
*/
@Before("@annotation(com.XXXX.Auth)") // 采用切点注释
public void before(JoinPoint jp) {
// jp.getTarget(); //得到目标对象
// jp.getSignature().getName();//得到方法名
// jp.getArgs(); //得到方法参数
Class s = joinPoint.getSignature().getDeclaringType();
System.out.println(s);
Auth auth = (Auth) joinPoint.getSignature().getDeclaringType().getAnnotation(Auth.class);
RequestMapping rm = (RequestMapping) joinPoint.getSignature().getDeclaringType().getAnnotation(RequestMapping.class);
if (auth.flag()) {...}
}
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
@Inherited
public @interface Auth {
/**
* @return
*/
public boolean flag() default false;

}

/* 代理方法 */
@Auth(flag = true)
@RequestMapping("/homepage")
public ModelAndView home(HttpServletRequest request) {
...
}

G. spring中有没有注解拦截器,只拦截注解

spring拦截器只拦截path, 或者说方法名称, 它并不知道注解, 但是比servletfilter丰富的事是,这里可以比较方便的做反射操作.

比如可以继承HandlerInterceptorAdapter类 或者实现HandlerInterceptor接口,放过没有指定注解的请求即可(前者要求只要override preHandle, 后者要实现postHandle,preHandle和afterCompletion)
举个Adapter的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
* 2016/08/30
* @author sleest
*/
@Component
public class MyInterceptor extends HandlerInterceptorAdapter {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
HandlerMethod hm = (HandlerMethod) handler;
Method method = hm.getMethod();

// 这里就可以处理某些注解下要做的事情或者放过

// Is Annotation On Method's Class
method.getDeclaringClass().isAnnotationPresent(RequestMapping.class);
// Is Annotation On Method
method.isAnnotationPresent(RequestMapping.class);

// Get Method Annotation, Perhaps null
RequestMapping requestMappingAt = method.getAnnotation(RequestMapping.class);
// Get Annnotation Attribute
requestMappingAt.name();
requestMappingAt.method();

// ...

return true;
}
}

H. 请教怎么用Spring的注解方式把一个Bean注入到过滤器中

@Component public class UseCarmanager implements AssignmentHandler { @Autowired public BaseDao baseDao; } 在spring的配置文件里要配置 base-scan 包含 UseCarManager的包路径。

阅读全文

与spring过滤器注解相关的资料

热点内容
ro膜可以过滤多少吨水 浏览:238
智能净水器一般多少钱 浏览:833
怎么清洗纯净水桶 浏览:773
烧烤油烟净化器的风机怎么清理 浏览:557
城镇污水处理厂污染物排放标准修编 浏览:357
汽配加工污水 浏览:383
空气净化器的滤袋怎么清洁 浏览:10
森雅r7机油滤芯怎么用 浏览:416
化妆品工厂的污水处理 浏览:298
鱼缸放盐放到过滤槽 浏览:96
什么品牌净化器除甲醛效果最好 浏览:901
树脂三乙醇胺硬度 浏览:270
灯达树脂砂轮 浏览:296
泰克马污水提升器无锡 浏览:926
水性热塑性树脂耐化性 浏览:779
史密斯净化器怎么重置滤芯 浏览:211
济源年产18万吨污水处理剂 浏览:328
厕所饮水机漏水什么情况 浏览:127
反渗透净水器如何接烧水壶 浏览:262
饮水机为什么不过滤水 浏览:416