当前位置: 首页 >服务端 > Spring注解

Spring注解

1、@Bean标记方法

  标明该方法返回一个Bean,该Bean被Spring容器管理

2、@Autowired标记方法

  标明该方法使用参数自动装载。

 

令Spring MVC无XML化:省去web.xml与mvc-dispatcher-servlet.xml与applicationContext.xml(后面两个其实也是由web.xml指定的)

<!-- Spring注册容器所需要的配置 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener  //下面那个类是为了取代这个而存在的。</listener-class></listener><!-- 自定义启动 --><listener><listener-class>com.guangshan.ui.SysInitListener  //要自定义启动的类需要实现ServletContextListener方法,并重写contextInitialized方法。</listener-class></listener><!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/applicationContext.xml</param-value></context-param><servlet><servlet-name>mvc-dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/mvc-dispatcher-servlet.xml</param-value></init-param><init-param><param-name>dispatchOptionsRequest</param-name><param-value>true</param-value></init-param><load-on-startup>1</load-on-startup><async-supported>true</async-supported></servlet>

    <servlet-mapping>
      <servlet-name>mvc-dispatcher</servlet-name>
      <url-patte>/</url-patte>
    </servlet-mapping>

  进入正题:

一、若使用无配置即纯注解方式加载bean,则需要继承:

  AbstractAnnotationConfigDispatcherServletInitializer类,该类有如下未实现方法:

@Overrideprotected Class<?>[] getRootConfigClasses() {retu new Class<?>[] { Object.class };}@Overrideprotected Class<?>[] getServletConfigClasses() {retu new Class<?>[] { WebConfig.class, WebSocketConfig.class , WebSocketCfg.class};}@Overrideprotected String[] getServletMappings() {retu new String[] { "/" };}

  第一个获取根配置的class,并对该config.class进行初始化执行注入,同时可以重写一下功能,实现启动时配置拦截器等动作。

@Overrideprotected void customizeRegistration(Dynamic registration) {registration.setInitParameter("dispatchOptionsRequest", "true");registration.setAsyncSupported(true);}@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {super.onStartup(servletContext);}
servletContext接口是Servlet中最大的一个接口,呈现了web应用的Servlet视图。ServletContext实例是通过 getServletContext()方法获得的,由于HttpServlet继承Servlet的关系,GenericServlet类和HttpServlet类同时具有该方法。

  PS:spring mvc-dispatch配置中可以配置请求地址拦截器

 <mvc:interceptors>  <!-- 多个拦截器,顺序执行 -->  <mvc:interceptor> <mvc:mapping path="/entryOrJsonController/*" /><!-- 如果不配置或/*,将拦截所有的Controller --> <bean class="com.wy.interceptor.CommonInterceptor"></bean>  </mvc:interceptor>  </mvc:interceptors>  

  拦截器类要实现HandlerInterceptor,重写方法即可

@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {retu true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {}@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {}

二、在第一步中config.class的类中要有@Configuration注解,该文件便会被识别为bean,并实例化加入bean container。同时方法有@Bean注解的,返回值对象也会加入bean container中。

  标记为@Configuration的类会被自动扫描,其中标注为@Bean的方法会自动执行并返回一个Bean给Ioc容器。

  @Configuration是继承@Component的,所以一个Configuration也是一个Bean。

 

三、若想可以自动扫描包中所有@Component,则可以在任一个Config或其他可启动的Bean中加入注解:

  @ComponentScan(
basePackages="org.springframework.samples",
excludeFilters = @ComponentScan.Filter(type= FilterType.ANNOTATION, value = Configuration.class)
)

  后面的filter是防止再次注入带注解@Configuration的类

  该句作用同dispatch.xml中的 

<context:component-scan base-package="com.foo" use-default-filters="false"> 
<context:include-filter type="regex" expression="com.foo.bar.*Config"/> 引入
<context:exclude-filter type="regex" expression="com.foo.config.*"/> 排除
</context:component-scan> 

PS:<context:annotation-config> 和 <context:component-scan>的区别

<context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解。

<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean 。

即:<context:annotation-config>是bean还需要在xml中注册,但是使用自动注入Autowired等时,就可以进行自动注入了,若没有该句,autowired将无效

<context:component-scan>可以自动扫描注解并创建Bean,并带有<context:annotation-config>的功能。

  

三、这样虽然可以实现无配置文件,但是对于@Service等可以自动扫描的注解,这样并不太好。

作者:光闪
来源链接:https://www.cnblogs.com/guangshan/p/4459924.html

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/server/113292.html

标签:Spring注解
分享给朋友: