关于Spirng IOC的注解说明

来源:互联网 发布:在js中引入html页面 编辑:程序博客网 时间:2024/06/18 16:41
@component 的作用:
相当于配置文件中applicationContext.xml中的一句:<bean id="XXX" class="XXXXX"></bean>
实例如下:
1、
package com.henu.app  //包名
@component(name="myComponent")
public class MyComponent
{
//其他省略
}
相当于-> 
2、<bean id="myComponent" class="com.henu.app.MyComponent"></bean>
当使用第一种时,必须在配置文件中加入
<context:component-scan base-package=”com.henu.app”>
1、@controller 控制器(注入服务)2、@service 服务(注入dao)3、@repository dao(实现dao访问)4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
  @Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。 
注意:在使用@component 前,必须配置spring 的配置文件:applicationContext.xml ,导入扫描@component 所在包的名字
下面写这个是引入component的扫描组件 
<context:component-scan base-package=”扫描的包名”>

其中base-package为需要扫描的包(含所有子包) 
       1、@Service用于标注业务层组件 
       2、@Controller用于标注控制层组件(如struts中的action) 
       3、@Repository用于标注数据访问组件,即DAO组件. 
       4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。