zoukankan      html  css  js  c++  java
  • springMVC的一些配置解析

    <mvc:annotation-driven />

    <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解POJO类方法的映射-->

    是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案。

    <mvc:annotation-driven /> 会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。

    并提供了:数据绑定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,读写XML的支持(JAXB),读写JSON的支持(Jackson)。

    当我们需要controller返回一个map的json对象时,可以设定<mvc:annotation-driven />,

    同时设定<mvc:message-converters> 标签,设定字符集和json处理类,例如:

    <mvc:annotation-driven>
    <mvc:message-converters>
    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
    <property name="supportedMediaTypes">
    <list>
    <value>text/plain;charset=UTF-8</value>
    </list>
    </property>
    </bean>
    </mvc:message-converters>
    </mvc:annotation-driven>

    <context:annotation-config/>

    使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean:

    使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:

      类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;使用@PersistenceContext注解,就必须声明 PersistenceAnnotationBeanPostProcessor的Bean。
      这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。

    <context:annotation-config/>

    另,在我们使用注解时一般都会配置扫描包路径选项:

    <context:component-scan base-package="pack.pack"/>
    

      该配置项其实也包含了自动注入上述processor的功能,因此当使用<context:component-scan/>后,即可将<context:annotation-config/>省去。

    备注:

     xmlns:context="http://www.springframework.org/schema/context"  

    在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。

    <context:component-scan base-packge="">

    <!-- 启动包扫描功能,以便注册带有@Controller、@service、@repository、@Component等注解的类成为spring的bean -->
    1、@controller 控制器(注入服务)
    2、@service 服务(注入dao)
    3、@repository dao(实现dao访问)
    4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
    
    
    
    
      @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。 
    下面写这个是引入component的扫描组件 
    <context:component-scan base-package=”com.mmnc”>    

    其中base-package为需要扫描的包(含所有子包) 
           1、@Service用于标注业务层组件 
           2、@Controller用于标注控制层组件(如struts中的action) 
           3、@Repository用于标注数据访问组件,即DAO组件. 
           4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。    
               @Service public class UserServiceImpl implements UserService { } 
               @Repository public class UserDao { } getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“***”)              
           这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) 
               @Scope(“prototype”)来改变。可以使用以下方式指定初始化方法和销毁方法(方法名任意): @PostConstruct public void init() { } 

    <context:component-scan base-packge="">

    <!-- 启动包扫描功能,以便注册带有@Controller、@service、@repository、@Component等注解的类成为spring的bean -->
    1、@controller 控制器(注入服务)
    2、@service 服务(注入dao)
    3、@repository dao(实现dao访问)
    4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
    
    
    
    
      @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。 
    下面写这个是引入component的扫描组件 
    <context:component-scan base-package=”com.mmnc”>    

    其中base-package为需要扫描的包(含所有子包) 
           1、@Service用于标注业务层组件 
           2、@Controller用于标注控制层组件(如struts中的action) 
           3、@Repository用于标注数据访问组件,即DAO组件. 
           4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。    
               @Service public class UserServiceImpl implements UserService { } 
               @Repository public class UserDao { } getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“***”)               这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”) 
               @Scope(“prototype”)来改变。
    可以使用以下方式指定初始化方法和销毁方法(方法名任意):
    @PostConstruct
    public void init() { 
    } 
    @PreDestroy
    public void destory() { 
    } 
    

      

    注入方式:

    把DAO实现类注入到action的service接口(注意不要是service的实现类)中,注入时不要new 这个注入的类,因为spring会自动注入,如果手动再new的话会出现错误,
    然后属性加上@Autowired后不需要getter()和setter()方法,Spring也会自动注入。  

    在接口前面标上@Autowired注释使得接口可以被容器注入,如:

    @Autowired  
    @Qualifier("chinese")  
    private userService userService; 
    

      当接口存在两个实现类的时候必须使用@Qualifier指定注入哪个实现类,否则可以省略,只写@Autowired。

  • 相关阅读:
    沟通,你都做了什么
    Markdown,你只需要掌握这几个
    一周心态-情景联想
    一周心态-漫谈计划之死
    我的Git之旅(1)---git安装、github注册以及一些基本命令
    我的20132014
    Python 笔记——4 条件控制
    Python 笔记——3 数据类型
    Python 笔记——1语法分析
    纯CSS实现兼容ie6以上的圆角头像
  • 原文地址:https://www.cnblogs.com/skyLogin/p/6605413.html
Copyright © 2011-2022 走看看