Spring的注解特性,IOC控制反转

   首先了解依赖注入是什么,就是在实例化对象的时候并不需要每次都new对象出来,spring管理对象,在你配置been或者@service时候

Spring会自动帮你实例化对象,你自己调用就好了;

   Spring帮你管理对象有两种方式

    1. 在Sping配置文件配置bean,这样这个对象就实例化了

      class为管理的对象,id 命名  property 为这个对象注入另外一个对象或者字符串  name为要注入的对象配置bean的id  ref为要配置对象的值
      <bean id="userPoiServer" class="com.ifengstar.user.service.impl.UserPoiServiceImpl">
      <property name="userSyncInfoDao"><ref local="userPoiDao"/></property>
      <property name="navigatorDao"><ref local="navigatorDao"/></property>
      </bean>
    2. 在Spring配置文件配置,扫描包 配置(简单方便,建议使用)

      <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器    
      srping会自动扫描包下的对象 有注入@Service、@Controller的 自动实例化--> <context:component-scan base-package="com.ifengSearch.*.controller" />
      <context:component-scan base-package="com.ifengSearch.*.service.impl" />            

简单的配置 注入解析

@Service用于标注业务层组件(我们通常定义的service层就用这个)

@Controller用于标注控制层组件(如struts中的action)

@Repository用于标注数据访问组件,即DAO组件

@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

  

简单的配置文件模版

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 默认的注解映射的支持-->
<mvc:annotation-driven /> <!-- 对静态文件的访问 -->
<mvc:default-servlet-handler/> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.ifengSearch.*.controller" />
<context:component-scan base-package="com.ifengSearch.*.service.impl" />
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> <!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean> </beans>

最新文章

  1. Ubuntu 14.04 更新源
  2. transition&amp;transform,CSS中过度和变形的设置
  3. 发布b3log-solo后,访问http://localhost:8080/b3log-solo/提示错误为staticServePath Error。
  4. C#接口作用的深入理解
  5. -----------------------------SpringMVC理解-----------------------------
  6. IOS 采用https 协议访问接口
  7. SQL Server 存储过程、触发器、游标
  8. HDU4850 构造一个长度n串,它需要随机长度4子是不相同
  9. android:onKeyDown
  10. 10大支持移动“触摸操作”的JavaScript框架
  11. Android使用gradle不同配置多项目打包
  12. Spider_Man_5.1 の Mongodb_安装
  13. java abs(绝对值) , max(最大值),min(最小值) 方法的应用
  14. [Python]Python中的浅复制与深复制
  15. 实现一个类似 http-server 的静态服务 一一 ks-server
  16. debug2
  17. top-N 抽样
  18. java基础----&gt;序列化框架arvo的使用
  19. 【分库、分表】MySQL分库分表方案
  20. css 使用率 清楚无用css

热门文章

  1. [补档]暑假集训D4总结
  2. 使用Hibernate模板调用存储过程
  3. python常用模块(1):collections模块和re模块(正则表达式详解)
  4. ASP.NET-页面间的数据传递
  5. mysql-proxy读写分离
  6. 读取txt文件加DevExpress之进度条progressBarControl
  7. mysql获取当前时间,前一天,后一天
  8. Mybatis源码分析-SqlSessionTemplate
  9. SolrCloud(一)搭建Zookeeper
  10. 【社交系统研发日记五】ThinkSNS+如何计算字符显示长度?