博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springMVC的一些配置解析
阅读量:7218 次
发布时间:2019-06-29

本文共 3619 字,大约阅读时间需要 12 分钟。

<mvc:annotation-driven />

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

<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。

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

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

备注:

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

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

 

 

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

1、@controller 控制器(注入服务)2、@service 服务(注入dao)3、@repository dao(实现dao访问)4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的
 
 
  @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。  下面写这个是引入component的扫描组件  
     其中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="">

1、@controller 控制器(注入服务)2、@service 服务(注入dao)3、@repository dao(实现dao访问)4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的
 
 
  @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。  下面写这个是引入component的扫描组件  
     其中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”)来改变。 可以使用以下方式指定初始化方法和销毁方法(方法名任意):
@PostConstructpublic void init() { } @PreDestroypublic void destory() { }

  

注入方式:

把DAO实现类注入到action的service接口(注意不要是service的实现类)中,注入时不要new 这个注入的类,因为spring会自动注入,如果手动再new的话会出现错误,

然后属性加上@Autowired后不需要getter()和setter()方法,Spring也会自动注入。  

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

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

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

转载于:https://www.cnblogs.com/skyLogin/p/6605413.html

你可能感兴趣的文章
Video Target Tracking Based on Online Learning—深度学习在目标跟踪中的应用
查看>>
深度学习理论解释基础
查看>>
遗传算法
查看>>
将web网站移动化
查看>>
Application-Session-Cookie
查看>>
Perl的多进程框架(watcher-worker)
查看>>
phpMyAdmin 后台拿webshell
查看>>
Linux 关机 休眠, 关闭移动设备自动挂载 命令
查看>>
Html唤起手机APP,如果有就唤起,如果没有就跳到下载页。
查看>>
Java中File类如何扫描磁盘所有文件包括子目录及子目录文件
查看>>
VC++ 限制窗口的大小范围的方法
查看>>
结对开发-返回一个整数数组中最大子数组的和(首尾相接版)
查看>>
meanshift-聚类
查看>>
不要if else的编程
查看>>
rn.ShowDialog() == DialogResult.OK
查看>>
20160519
查看>>
SCU 3132(博弈)
查看>>
正则表达式
查看>>
delete archivelog all 无法彻底删除归档日志?
查看>>
Redis五大数据类型
查看>>