Spring注解

mac2022-06-30  22

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-pattern>/</url-pattern>     </servlet-mapping>

  进入正题:

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

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

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

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

@Override protected void customizeRegistration(Dynamic registration) { registration.setInitParameter("dispatchOptionsRequest", "true"); registration.setAsyncSupported(true); } @Override public 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,重写方法即可

@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception { return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } @Override public 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

最新回复(0)