我如何更改此xml配置:

<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
      ...
      <property name="additionalDialects">
        <set>
          <bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
        </set>
      </property>
      ...
    </bean>


到到目前为止的Java代码配置,我只有一个,在这一部分中我只有问题:

<property name="additionalDialects">
            <set>
              <bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
            </set>
          </property>


这是我到目前为止在Java代码中所拥有的:

   @Bean
    public SpringSecurityDialect springSecurityDialect() {
        return new SpringSecurityDialect();
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine engine  =  new SpringTemplateEngine();
        engine.setTemplateResolver( templateResolver() );
        engine.setMessageSource( messageSource() );

        //DIALECTS
                Set<SpringSecurityDialect> ssdSet = new HashSet<SpringSecurityDialect>();
                ssdSet.add( springSecurityDialect() );
                engine.setAdditionalDialects( ssdSet ); <-- this line give me this error
        return engine;
    }


这行给我这个错误:The method setAdditionalDialects(Set<IDialect>) in the type TemplateEngine is not applicable for the arguments (Set<SpringSecurityDialect>)

最佳答案

今天我用头撞了一下。答案在于查看Spring Boot源代码:

https://github.com/spring-projects/spring-boot/blob/v1.2.5.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java

@Configuration
@ConditionalOnClass({ SpringSecurityDialect.class })
protected static class ThymeleafSecurityDialectConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public SpringSecurityDialect securityDialect() {
        return new SpringSecurityDialect();
    }

}


因此,Spring Boot(好吧,“ org.springframework.boot:spring-boot-starter-thymeleaf”)将尝试注入org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect。也许它不起作用,因为我有springsecurity4 jar。

无论如何,有一个简单的解决方法:只需执行Spring Boot尝试执行的操作即可。在@Configuration类中,只需添加bean:

@Bean
public SpringSecurityDialect securityDialect() {
    return new SpringSecurityDialect();
}

08-05 07:57
查看更多