虽然会使用自定义注解了,但是关于如何读取自定义注解,时机这点有点模糊。
前面都是使用的框架的注解,只要在框架的config文件进行简单配置,容器就可以自动扫描我们的注解了,很是顺手。自定义的注解比如我前面看的别人的validate验证框架,我却不知道如何让它自动扫描并生效。
几个思路:
1、类似@Service、@Compent这些spring装载进容器的注解,可能是在spring容器加载时期,扫描指定包。
2、类似@Test放在方法上的注解,使用的可能是类似AOP,切面的概念。在方法执行前进行扫描,在方法上的注解可能都是这样的扫描实际吧。
3、类似@AutoWired放在成员变量上的注解属于@Service注解的下一层注解,可能是在Spring扫描到了类之后再扫瞄Class的成员,动态改变Class文件。
我们的@Excel文件导出的注解的使用时机是在具体的ExcelTool方法中了。具体看需求吧,它就不是自动扫描而是被动扫描了。方法上的注解应该也是被动扫描了。
我们的验证token的注解的使用的例子:
package cn.allcheer.acl.credit.server.config.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Created by APPLE on 2016/12/15. */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Authorization { }
package cn.allcheer.acl.credit.server.config.interceptor; import cn.allcheer.acl.credit.server.config.annotation.Authorization; import cn.allcheer.acl.credit.server.config.constants.TokenConstants; import cn.allcheer.acl.credit.server.token.TokenManager; import cn.allcheer.acl.credit.server.token.TokenModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Method; /** * Created by APPLE on 2016/12/15. */ @Component public class AuthorizationInterceptor extends HandlerInterceptorAdapter { @Autowired private TokenManager manager; public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { //如果不是映射到方法直接通过 if (!(handler instanceof HandlerMethod)) { return true; } int totalbytes = request.getContentLength(); HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); //从header中得到toke // n String authorization = request.getHeader(TokenConstants.AUTHORIZATION); //验证token TokenModel model = manager.getToken(authorization); if (manager.checkToken(model)) { //如果token验证成功,将token对应的用户id存在request中,便于之后注入 return true; } //如果验证token失败,并且方法注明了Authorization,返回401错误 if (method.getAnnotation(Authorization.class) != null) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); return false; } return true; } }
package cn.allcheer.acl.credit.server.config; import cn.allcheer.acl.credit.server.config.interceptor.AuthorizationInterceptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public class TokenConfig extends WebMvcConfigurerAdapter { @Autowired private AuthorizationInterceptor authorizationInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(authorizationInterceptor); } }
package cn.allcheer.acl.credit.server.web.controller; import cn.allcheer.acl.credit.server.config.annotation.Authorization; import cn.allcheer.acl.credit.server.dto.BaseDTO; import cn.allcheer.acl.credit.server.dto.LendSummaryDTO; import cn.allcheer.acl.credit.server.dto.LendSummaryParam; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; /** * Created by Administrator on 2017/2/24. */ @Api(value = "出借概要信息接口" ,description = "出借概要信息接口") @Controller public class LendSummaryController extends BaseController{ @RequestMapping(value = "/lend_summary_message" ,method = RequestMethod.POST) @ApiOperation(value = "出借概要信息接口" ,notes = "出借概要信息" ,response = LendSummaryDTO.class) @Authorization public ResponseEntity<BaseDTO> lendSummaryMessage(@ApiParam("签名") @RequestHeader("sign") String sign , @ApiParam("出借概要参数") @RequestBody @Validated LendSummaryParam lendSummaryParam ,){ return null; } @Override protected ResponseEntity<BaseDTO> doBusiness(Object object) { return null; } }