本文的目的是介绍EnableWebMvc注释的含义的详细情况,特别关注enablewebmvc注解作用的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解EnableWe
本文的目的是介绍EnableWebMvc注释的含义的详细情况,特别关注enablewebmvc注解作用的相关信息。我们将通过专业的研究、有关数据的分析等多种方式,为您呈现一个全面的了解EnableWebMvc注释的含义的机会,同时也不会遗漏关于@EnableWebMvc 注解会让 Swagger 无效访问的问题、@EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别、@ENABLEWEBSECURITY和@ENABLEWEBMVCSECURITY有什么区别?、Angular ng-reflect-ng-if 注释的含义说明的知识。
本文目录一览:- EnableWebMvc注释的含义(enablewebmvc注解作用)
- @EnableWebMvc 注解会让 Swagger 无效访问的问题
- @EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别
- @ENABLEWEBSECURITY和@ENABLEWEBMVCSECURITY有什么区别?
- Angular ng-reflect-ng-if 注释的含义说明
EnableWebMvc注释的含义(enablewebmvc注解作用)
我阅读了有关的Javadoc @EnableWebMvc
。
但是我不明白这个注释是什么意思?
你能清楚说明一下吗?
答案1
小编典典当你使用Java代码(而不是XML)配置Spring应用程序时,@EnableWebMvc
用于启用Spring MVC。如果你还不熟悉Spring对Java配置的支持,那么这是一个不错的起点。
@EnableWebMvc
等效<mvc:annotation-driven />
于XML。它支持对带@Controller
注释的类的支持,该类@RequestMapping
用于将传入的请求映射到某个方法。你可以在参考文档中阅读有关其默认配置以及如何自定义配置的详细信息。
@EnableWebMvc 注解会让 Swagger 无效访问的问题
在工作中,通过 Swagger2 对项目的 controller 进行配置,以便于用户测试 restful 服务接口提高开发效率。
但是今天却出现了一个让我匪夷所思的问题就是在配置类里面加上 @EnableWebMvc 注解后(开启 web 配置支持)启动项目
发现访问 Swagger 的 ui 界面 404
这个我就奇怪了,然后我尝试的把 @EnableWebMvc 注解放到启动类上
发现 Swagger-ui.html 又可以访问到了,我记的 @SpringBootApplication 是默认开启自动配置 webMvc 的呀,那为什么会这样子呢,
通过查找资料发现 @EnableWebMvc 这个注解不建议直接配置到 @Configuration 上
@EnableWebMvc 注解会全面接管 SpringMVC,所有的 SpringMVC 的自动配置都失效了,通常不这么干。
查看 @EnableWebMvc 源码发现,是继承的 WebMvcConfigurationSupport
然后我去查了 SpringBoot 对 MVC 的自动配置 WebMvcAutoConfiguration 发现
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
发现@EnableWebMvc加入之后会会取消SpringBoot的自动配置,自会提供最基础的功能
所以根据总结不难发现,springboot 对 MVC 的自动配置已经帮助我们配置了应用开发中大部分的功能,我们几乎不用去关闭修改,当然对 Swagger 的配置 SpringBoot 也帮助我们
自动配置完成。
所以当我们取消 SpringBoot 自动配置的时候加上了 @EnableWebMvc 之后,就需要自己配置 Swagger
@EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别
@EnableWebMvc是什么
直接看源码,@EnableWebMvc实际上引入一个DelegatingWebMvcConfiguration。
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}
DelegatingWebMvcConfiguration继承了WebMvcConfigurationSupport
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
...
所以@EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport
@EnableWebMvc和@EnableAutoConfiguration的关系
@EnableAutoConfiguration是springboot项目的启动类注解@SpringBootApplication的子元素,主要功能为自动配置。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
...
}
@EnableAutoConfiguration实际是导入了EnableAutoConfigurationImportSelector和Registrar两个类
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
...
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import({Registrar.class})
public @interface AutoConfigurationPackage {
}
这两个类的具体原理有些复杂,不太清除,主要内容是通过SpringFactoriesLoader.loadFactoryNames()导入jar下面的配置文件META-INF/spring.factories
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
return configurations;
}
配置文件中的内容如下
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
...
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\
...
其中有WebMvcAutoConfiguration,WebMvcAutoConfiguration源码如下
@Configuration
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {
...
}
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})意思是如果存在它修饰的类的bean
,则不需要再创建这个bean。
由此可得出结论:
如果有配置文件继承了DelegatingWebMvcConfiguration,
或者WebMvcConfigurationSupport,或者配置文件有@EnableWebMvc,那么 @EnableAutoConfiguration 中的
WebMvcAutoConfiguration 将不会被自动配置,而是使用WebMvcConfigurationSupport的配置。
@EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter使用
WebMvcConfigurationAdapter已经废弃,最好用implements WebMvcConfigurer代替
@Configuration
public class MyConfig implements WebMvcConfigurer {
}
如果使用继承,WebMvcConfigurationSupport,DelegatingWebMvcConfiguration,或者使用@EnableWebMvc,
需要注意会覆盖application.properties中关于WebMvcAutoConfiguration的设置,需要在自定义配置中实现,如
springboot2.0、spring5.0 拦截器配置WebMvcConfigurerAdapter过时使用WebMvcConfigurationSupport来代替 新坑
示例如下
Configuration
@EnableWebMvc
public class MyConfig implements WebMvcConfigurer {
}
@Configuration
public class MyConfig extends WebMvcConfigurationSupport {
}
@Configuration
public class MyConfig extends DelegatingWebMvcConfiguration {
}
上面代码中需要在类中实现关于WebMvcAutoConfiguration的配置,而不是在application.properties中。
总结
implements WebMvcConfigurer : 不会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
@EnableWebMvc + implements WebMvcConfigurer : 会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
extends WebMvcConfigurationSupport :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
extends DelegatingWebMvcConfiguration :会覆盖@EnableAutoConfiguration关于WebMvcAutoConfiguration的配置
@ENABLEWEBSECURITY和@ENABLEWEBMVCSECURITY有什么区别?
<div id="cnblogs_post_body"><div> <a href="http://www.dovov.com/enablewebsecurityenablewebmvcsecurity.html"title="@EnableWebSecurity和@EnableWebMvcSecurity有什么区别?"><h1>@EnableWebSecurity和@EnableWebMvcSecurity有什么区别?</h1></a>
<h2> @EnableWebSecurity </h2> <p> JavaDoc文档: </p><div><div><!-- /70071061/whjz-jy/336x280-01-dovov-jy-whjz --> <div id="div-gpt-ad-1556180391372-0">
</div></div><div><!-- /70071061/whjz-jy/zsy-02-dovov-whjz-jy --> <div id="div-gpt-ad-1558695785800-0">
</div></div></div> <blockquote> <p>将此注释添加到<code>@Configuration</code>类中,以使<code>Spring Security</code>configuration在任何<code>WebSecurityConfigurer</code>定义,或者更可能通过扩展<code>WebSecurityConfigurerAdapter</code>基类和重写单个方法。 </p><ul><li><ahref="http://www.dovov.com/spring-mvcbindingresult.html" rel="bookmark" title="Spring MVC中的BindingResult接口有什么用?">Spring MVC中的BindingResult接口有什么用?</a></li><li><ahref="http://www.dovov.com/typesbean.html" rel="bookmark" title="没有匹配的types的bean ...发现依赖">没有匹配的types的bean ...发现依赖</a></li><li><ahref="http://www.dovov.com/spring-mvcredirectredirecthttp-https.html" rel="bookmark" title="Spring MVC“redirect:”前缀总是redirect到http - 我如何使它保持在https上?">Spring MVC“redirect:”前缀总是redirect到http - 我如何使它保持在https上?</a></li><li><ahref="http://www.dovov.com/springparameter-passing.html" rel="bookmark" title="如何从Spring获取当前用户区域设置而不将其作为parameter passing给函数?">如何从Spring获取当前用户区域设置而不将其作为parameter passing给函数?</a></li><li><ahref="http://www.dovov.com/spring-3-x-json-status-406acceptheaders.html" rel="bookmark" title="Spring 3.x JSON status 406“特征根据请求不可接受”accept“headers()”">Spring 3.x JSON status 406“特征根据请求不可接受”accept“headers()”</a></li></ul> </blockquote> <h2> @EnableWebMvcSecurity </h2> <p> JavaDoc文档: </p> <blockquote> <p> 将此注释添加到<code>@Configuration</code>类,以使<code>Spring Security</code>configuration与<code>Spring MVC</code>集成。 </p><!-- /70071061/whjz-jy/zsy-01-dovov-whjz-jy --> <div id="div-gpt-ad-1558695632092-0">
</div> </blockquote> <ul> <li> “ <em>与Spring MVC集成</em> ”究竟意味着什么? 我能得到什么额外的行为? </li> <li> 我find了指南和答案 ,这表明这个注释添加了<code>CSRF Tokens</code>到<code>Spring MVC</code> Forms,这是它增加的唯一的东西吗? </li> </ul> <p></p> <div> <div> <!-- /70071061/whjz-jy/300x250-01-dovov-jy-whjz --> <div id="div-gpt-ad-1556180088247-0">
</div> </div> <div> <ul><li><ahref="http://www.dovov.com/springmvcjson-406.html" rel="bookmark" title="springMVC不返回JSON的内容 - 错误406">springMVC不返回JSON的内容 - 错误406</a></li><li><ahref="http://www.dovov.com/modelmodelmapmodelandview.html" rel="bookmark" title="Model,ModelMap和ModelAndView之间有什么区别?">Model,ModelMap和ModelAndView之间有什么区别?</a></li><li><ahref="http://www.dovov.com/spring-bean-6.html" rel="bookmark" title="Spring bean后期处理器的工作原理">Spring bean后期处理器的工作原理</a></li><li><ahref="http://www.dovov.com/spring-5.html" rel="bookmark" title="如何在Spring中获得会话对象?">如何在Spring中获得会话对象?</a></li><li><ahref="http://www.dovov.com/responsebodyhttpmessageconverterjson.html" rel="bookmark" title="用@ResponseBody自定义HttpMessageConverter来做Json的事情">用@ResponseBody自定义HttpMessageConverter来做Json的事情</a></li><li><ahref="http://www.dovov.com/spring-mvc-spring.html" rel="bookmark" title="Spring MVC - 如何在Spring控制器中获取地图中的所有请求参数?">Spring MVC - 如何在Spring控制器中获取地图中的所有请求参数?</a></li><li><ahref="http://www.dovov.com/spring-mvcspring-boot.html" rel="bookmark" title="Spring MVC或Spring Boot">Spring MVC或Spring Boot</a></li><li><ahref="http://www.dovov.com/exception-100.html" rel="bookmark" title="处理程序exception没有适配器">处理程序exception没有适配器</a></li></ul> </div> </div>
<div>
<!-- You can start editing here. -->
<h3 id="comments"> 2 Solutions collect form web for “@EnableWebSecurity和@EnableWebMvcSecurity有什么区别?” </h3>
<div>
<p> 如果您查看这些类, <code>@EnableWebMvcSecurity</code>实际上在<code>@EnableWebSecurity</code>中添加了<code>@EnableWebSecurity</code>注释。 因此, <code>@EnableWebMvcSecurity</code>执行<code>@EnableWebMvcSecurity</code>所做的一切, <code>@EnableWebSecurity</code>一点。 </p>
<p> 你还问什么? </p> <p> 如果您查看<code>WebMvcSecurityConfiguration</code> ,您将看到它添加了一个<code>AuthenticationPrincipalArgumentResolver</code>以便您可以通过向控制器方法参数添加批注来访问authentication主体。 即: </p> <pre> <code><span>public</span> <span>String</span> <span>show</span>(<span>@AuthenticationPrincipal</span> CustomUser customUser) { <span>// do something with CustomUser return "view"; }</span></code> </pre> <p> 它还与Spring Web MVC集成,为表单添加一个CSRF令牌。 </p> </div> <!-- #comment-## --> <div> <blockquote> <p> 从Spring Security 4.0开始,不推荐使用<code>@EnableWebMvcSecurity</code> 。 replace是<code>@EnableWebSecurity</code>将决定添加基于类path的Spring MVCfunction。 </p> <p> <em>为了使Spring Security与Spring MVC集成,将</em> <code>@EnableWebSecurity</code> <em>注释</em> <em>添加</em> <em>到您的configuration中。</em> </p> </blockquote>
</div> <!-- #comment-## -->
<div>
<div></div>
<div></div>
</div>
<!-- /70071061/whjz-jy/728x90-01-dovov-jy-whjz -->
<div id="div-gpt-ad-1556180435386-0">
</div> <ul>
</ul> </div> 原文地址:http://www.dovov.com/enablewebsecurityenablewebmvcsecurity.html </div> </div>
Angular ng-reflect-ng-if 注释的含义说明
在 Angular 应用中,我们经常会看到在浏览器中渲染的 HTML 结构里包含了一些特殊的注释,这些注释对于 Angular 框架本身具有特定的功能,但对于最终用户是不可见的。现在,我们来详细探讨你提到的代码段。
解析代码含义
你提供的 HTML 代码是:
<div role="tabpanel" id="1-CONF_HOME_THEATER_ML@_GEN-group"><!--bindings={
"ng-reflect-ng-if": "false"
}-->
这段代码包含几个部分:一个 div
元素和一个 Angular 特有的注释。div
元素有两个属性:role
和 id
。这里 role="tabpanel"
表示该元素在页面中扮演一个选项卡面板的角色,这是无障碍辅助技术(如屏幕阅读器)的一个标准实践。而 id
属性则是该元素的唯一标识符,用于标识不同的面板,可能还涉及到 CSS 样式或 JavaScript 逻辑。
注释 <!--bindings={ "ng-reflect-ng-if": "false" }-->
是 Angular 的内部机制用来跟踪和记录模板中数据绑定的状态。这里主要涉及的是 *ngIf
指令的反映(reflect)。Angular 在开发模式下会添加这种注释来帮助开发者理解数据绑定的状态,但在生产模式中通常会被移除,以减少生成的 HTML 代码的体积。
ng-reflect-ng-if
这个注释中的 ng-reflect-ng-if="false"
表示 *ngIf
指令当前的状态为 false
,也就是说,基于 *ngIf
所绑定的表达式的值,这个 div
元素不会被显示在 DOM 中。*ngIf
是一个结构型指令,用来根据条件来添加或移除 HTML 元素。当表达式结果为 true
时,元素会被渲染到 DOM 中;当表达式为 false
时,元素会从 DOM 中移除。
示例说明
假设我们有一个 Angular 组件,其模板中包含如下代码:
<div *ngIf="showTabPanel" role="tabpanel" id="home-theater">
内容区域
</div>
这里,*ngIf
指令绑定了一个名为 showTabPanel
的组件属性。如果 showTabPanel
的值为 true
,则 div
元素将显示在页面上;如果值为 false
,则不显示。在你的例子中,相应的注释 ng-reflect-ng-if="false"
就是反映了在某一时刻,showTabPanel
的值为 false
。
代码的作用
这种注释对于开发者调试应用非常有帮助。它允许开发者在不打开开发者工具的情况下快速查看某个元素是否应该被渲染以及为什么它没有被渲染(或者被渲染了)。这是理解和调试 Angular 应用中数据流和视图状态的一个重要手段。
总结
Angular 的注释虽然在生产环境中不可见,但在开发过程中它们提供了对框架行为深入了解的窗口。通过 ng-reflect-*
类型的注释,开发者可以更容易地追踪和调试复杂的数据状态和视图更新问题,尤其是在处理大型应用和复杂交互时。这些机制虽然在最终用户界面中
是隐藏的,但对于保持 Angular 应用的健康和性能至关重要。
我们今天的关于EnableWebMvc注释的含义和enablewebmvc注解作用的分享就到这里,谢谢您的阅读,如果想了解更多关于@EnableWebMvc 注解会让 Swagger 无效访问的问题、@EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别、@ENABLEWEBSECURITY和@ENABLEWEBMVCSECURITY有什么区别?、Angular ng-reflect-ng-if 注释的含义说明的相关信息,可以在本站进行搜索。
本文标签: