Avi*_*mar 5 java spring spring-mvc
我想在类中保存区域(经过身份验证的用户的属性).区域列表从数据库中获取并存储为Spring Controller类中每个用户的属性,假设为"A".
现在我想要在Spring拦截器"B"中获取这个区域属性,并希望验证用户的区域,我在每个http请求中作为参数验证存储在region属性中的参数.
我被建议使用服务或组件来存储区域列表,以便拦截器B和控制器类A都可以使用该服务或组件.
在这种情况下,任何人都可以告诉我如何使用服务或组件.
你的问题并不完全清楚,但我会尽我所能.
我想你是说你想在Spring MVC HandlerInterceptor和Controller中使用相同的类?
您应该使用依赖注入:
用@Service注释服务类(这让Spring找到你的服务)
@Service
public class MyRegionService...
Run Code Online (Sandbox Code Playgroud)
将组件扫描添加到应用程序上下文配置(这可以找到服务)
<context:component-scan base-package="com.example.yourapp"/>
Run Code Online (Sandbox Code Playgroud)
在任何需要的地方添加服务作为类成员,并使用@Autowired注释它(这会注入服务)
@Controller
public class MyController {
@Autowired
MyRegionService myRegionService;
}
Run Code Online (Sandbox Code Playgroud)
和
public class MyHandlerInterceptor extends HandlerInterceptorAdapter {
@Autowired
MyRegionService myRegionService;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6819 次 |
| 最近记录: |