# java-SpringBoot自定義參數(shù)解析器實(shí)現(xiàn)對(duì)象自動(dòng)注入
解析器邏輯流程圖表
后臺(tái)解析注解的解析器
首先,我在java后臺(tái)編寫了一個(gè)解析器,代碼如下
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
42
43
44
|
import com.ruoyi.framework.interceptor.annotation.LoginUser; import com.ruoyi.project.WebMoudle.WebUser.domain.WebUser; import com.ruoyi.project.WebMoudle.WebUser.service.IWebUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.MethodParameter; import org.springframework.stereotype.Service; import org.springframework.web.bind.support.WebDataBinderFactory; import org.springframework.web.context.request.NativeWebRequest; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.method.support.ModelAndViewContainer; /** * 有@LoginUser注解的controller方法會(huì)進(jìn)入到解析器中 * 通過解析器查詢到當(dāng)前用戶,并返回給controller * * @author yangz */ @Service public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver { //用戶service @Autowired private IWebUserService webUserService; @Override public boolean supportsParameter(MethodParameter parameter) { return parameter.getParameterType().isAssignableFrom(WebUser. class ) && parameter.hasParameterAnnotation(LoginUser. class ); } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container, NativeWebRequest request, WebDataBinderFactory factory) throws Exception { //從request作用域中獲取登錄時(shí)存入的用戶ID,不明白的可以查看我的博客springBoot攔截器一文 Object object = request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY, RequestAttributes.SCOPE_REQUEST); if (object == null ) { return null ; } //獲取用戶信息 Long userId=(Long) object; WebUser user = webUserService.selectWebUserById(userId); return user; } } |
其次,我編寫一個(gè)攔截器配置類,將攔截器注入到spring容器中
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
|
import com.ruoyi.framework.interceptor.LoginUserHandlerMethodArgumentResolver; import org.springframework.context.annotation.Configuration; import org.springframework.web.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.List; @Configuration public class LoginUserConfig extends WebMvcConfigurerAdapter { /** * 此處獲取攔截器實(shí)例化對(duì)象,同理攔截器 * @return */ @Bean public LoginUserHandlerMethodArgumentResolver getLoginUserHandlerMethodArgumentResolver(){ return new LoginUserHandlerMethodArgumentResolver(); } @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers){ super .addArgumentResolvers(argumentResolvers); argumentResolvers.add(getLoginUserHandlerMethodArgumentResolver()); } } |
最后是我們的開關(guān),也就是自定義的注解LoginUser注解,當(dāng)在controller方法中參數(shù)有使用此注解,就會(huì)觸發(fā)我們的解析器進(jìn)行對(duì)象注入,那么我就得自己定義一個(gè)屬于自己的注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 注入用戶信息注解, *比較簡(jiǎn)單,沒有聲明更多的屬性 * @author lipengjun * @email [email protected] * @date 2017-03-23 20:39 */ @Target (ElementType.PARAMETER) @Retention (RetentionPolicy.RUNTIME) public @interface LoginUser { } |
然后就是一小個(gè)演示使用的方法
1
2
3
4
5
|
@RequestMapping (value = "/prepay" ) @ResponseBody public Map<String,Object> prepay( @LoginUser WebUser webUser){ //此間,從request中獲取到userId信息就會(huì)在進(jìn)入controller之前將webuser對(duì)象查出并注入到webUser參數(shù)中 } |
補(bǔ)充知識(shí):Springboot基于自定義注解的自動(dòng)裝配
1.定義java bean
1
2
3
4
5
|
@Data //lombok注解 public class User { private Integer userId; private String userName; } |
2.創(chuàng)建configuration類
1
2
3
4
5
6
7
8
9
|
public class UserConfig { @Bean public User getUser(){ User user = new User(); user.setUserId( 1 ); user.setUserName( "你好啊 哈哈哈哈" ); return user; } } |
3.定義注解
1
2
3
4
5
6
7
8
|
@Target ({ElementType.TYPE}) @Retention (RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import (UserConfig. class ) public @interface EnableAutoImport { } |
4.調(diào)用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@SpringBootApplication @EnableAutoImport //打上你自定義的注解 public class DemoApplication implements InitializingBean { //這里實(shí)現(xiàn)了InitializingBean 在初始化bean的時(shí)候都會(huì)執(zhí)行afterPropertiesSet @Autowired private User user; //注入 user類 public static void main(String[] args) { SpringApplication.run(DemoApplication. class , args); } @Override public void afterPropertiesSet() throws Exception { //在這里調(diào)用了裝配進(jìn)來的類 System.out.println(user.getUserName()); } } |
以上這篇java SpringBoot自定義注解,及自定義解析器實(shí)現(xiàn)對(duì)象自動(dòng)注入操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/nishiwodebocai21/article/details/100045494