当前位置: 首页 > news >正文

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。

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

相关文章:

  • Spring4-三种方式注入值到 bean 属性
  • maven 打包时提示 软件包 xxxxxxx 不存在
  • poj1753-Flip Game BFS+位运算
  • 对 Git 分支 master 和 origin/master 的一些认识
  • 不要做干自己没时间做的事
  • @Not - Empty-Null-Blank
  • vagrant学习笔记
  • jquery widgets 弹框
  • Linux系统管理命令之权限管理
  • ReactNative开发常用的三方模块
  • Linq to entity 执行多个字段排序的方法
  • maven学习:java编译插件与去除测试插件
  • 机器的自我进化
  • 取汉子拼音首字母的VB.Net方法
  • TP90 95 99指标
  • 【391天】每日项目总结系列128(2018.03.03)
  • 【comparator, comparable】小总结
  • Android 控件背景颜色处理
  • Android交互
  • Android框架之Volley
  • angular2开源库收集
  • C# 免费离线人脸识别 2.0 Demo
  • canvas绘制圆角头像
  • es6--symbol
  • gcc介绍及安装
  • MobX
  • MySQL QA
  • Python利用正则抓取网页内容保存到本地
  • Python学习之路13-记分
  • sessionStorage和localStorage
  • tab.js分享及浏览器兼容性问题汇总
  • TCP拥塞控制
  • 阿里云应用高可用服务公测发布
  • 后端_ThinkPHP5
  • 机器学习 vs. 深度学习
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 普通函数和构造函数的区别
  • 如何利用MongoDB打造TOP榜小程序
  • 微信小程序设置上一页数据
  • 我是如何设计 Upload 上传组件的
  • Java性能优化之JVM GC(垃圾回收机制)
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • ​用户画像从0到100的构建思路
  • #Z0458. 树的中心2
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (2020)Java后端开发----(面试题和笔试题)
  • (Mac上)使用Python进行matplotlib 画图时,中文显示不出来
  • (pojstep1.3.1)1017(构造法模拟)
  • (十二)springboot实战——SSE服务推送事件案例实现
  • (四) Graphivz 颜色选择
  • (转)Linux NTP配置详解 (Network Time Protocol)
  • (转)清华学霸演讲稿:永远不要说你已经尽力了
  • .NET CORE Aws S3 使用