为什么将ExpressionInterceptUrlRegistry调用链分成多个部分时会丢失类型信息?

问题描述 投票:0回答:1

这在Spring Boot中有效:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure (final HttpSecurity http) throws Exception
    {
        http
            .csrf    ()
            .disable ()
            .authorizeRequests ()
            .antMatchers ("/public/**") .permitAll ()
            ...

这不是:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure (final HttpSecurity http) throws Exception
    {
        ExpressionInterceptUrlRegistry registry = http
                .csrf    ()
                .disable ()
                .authorizeRequests ();

        registry.antMatchers ("/public/**") .permitAll ();

编译错误是

cannot find symbol
    symbol:   method permitAll()
    location: class java.lang.Object

[当构建器调用链这样拆分时,antMatchers返回Object而不是ExpressionInterceptUrlRegistry?为什么?

实际问题:半构建链时如何存储构建器对象,并在后续调用中继续使用它?

理论问题:为什么编译器看不到类型?

java spring spring-boot
1个回答
0
投票

理论在这里得到回答:

您的编译器或IDE应该给您警告。本质上,ExpressionInterceptUrlRegistry是内部类,而其封闭类ExpressionInterceptUrlRegistry是泛型的。内部类类型始终与其封闭类型(及其参数化)相关联。

因为您已经声明了ExpressionUrlAuthorizationConfigurer变量而没有使用其封闭类型(并且没有类型参数)来限定变量,所以该变量被视为原始ExpressionUrlAuthorizationConfigurer类型,并且所有泛型均被删除(即ExpressionInterceptUrlRegistry返回类型为ExpressionInterceptUrlRegistry,而不是antMatchers 应该已经绑定到的返回类型),正如我在上面有关原始类型的规范文章中所解释的那样。

使用参数化的封闭类型适当地限定您的类型

Object
© www.soinside.com 2019 - 2024. All rights reserved.