Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2794836
  • 博文数量: 471
  • 博客积分: 7081
  • 博客等级: 少将
  • 技术积分: 5369
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-04 21:55
文章分类

全部博文(471)

文章存档

2014年(90)

2013年(69)

2012年(312)

分类: 系统运维

2012-03-11 13:58:55

面的例子我们都是使用XML的bean定义来配置组件。在一个稍大的项目中,通常会有上百个组件,如果这些这组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找及维护起来也不太方便。spring2.5为我们引入了组件自动扫描机制,他可以在类路径底下寻找标注了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件是一样的。要使用自动扫描机制,我们需要打开以下配置信息:
       xmlns:xsi=""
       xmlns:context=""
       xsi:schemaLocation="
           /spring-beans-2.5.xsd
           
           /spring-context-2.5.xsd">
         
其中base-package为需要扫描的包(含子包)。

@Service用于标注业务层组件、 @Controller用于标注控制层组件(如struts中的action)、@Repository用于标注数据访问组件,即DAO组件。而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

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

这个配置隐式注册了多个对注释进行解析处理的处理器也包括了它<context:annotation-config/>

自动扫描,

1、不用在配置文件那里写,但是最好在要用的bean上,加上@Service(“personService”)虽然默认是personService首字母变小写即为beanid

2、记住那些依赖Bean在哪里注入,也还是要加上Resource等标记

3、切面也是一个bean ,后面的切面编程,也是可以纳入自动管理的或者基于xml方式

beans文件配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns=""
  3.        xmlns:xsi=""
  4.        xmlns:context=""
  5.        xsi:schemaLocation="
  6.            /spring-beans-2.5.xsd
  7.             /spring-context-2.5.xsd">
  8.          
  9.           <context:component-scan base-package="cn.itcast"/>
  10. </beans>
  1. package cn.itcast.dao.impl;

  2. import org.springframework.stereotype.Repository;
  3. import cn.itcast.dao.PersonDao;

  4. @Repository("personDao")
  5. public class PersonDaoBean implements PersonDao {
  6.     public void add(){
  7.         System.out.println("执行PersonDaoBean中的add()方法");
  8.     }
  9. }



  10. package cn.itcast.service.impl;

  11. import javax.annotation.PostConstruct;
  12. import javax.annotation.PreDestroy;
  13. import javax.annotation.Resource;

  14. import org.springframework.stereotype.Service;

  15. import cn.itcast.dao.PersonDao;
  16. import cn.itcast.service.PersonService;

  17. @Service("personService")
  18. public class PersonServiceBean implements PersonService {
  19.     //@Autowired(required=false) @Qualifier("personDaoxxxx")
  20.     
  21.     @Resource(name="personDao")
  22.     private PersonDao personDao;
  23.    
  24.     public void save(){
  25.         personDao.add();
  26.     }
  27. }


  28. package junit.test;

  29. import org.junit.BeforeClass;
  30. import org.junit.Test;
  31. import org.springframework.context.support.AbstractApplicationContext;
  32. import org.springframework.context.support.ClassPathXmlApplicationContext;

  33. import cn.itcast.service.PersonService;

  34. public class SpringTest {

  35.     @BeforeClass
  36.     public static void setUpBeforeClass() throws Exception {
  37.     }

  38.     @Test public void instanceSpring(){
  39.         AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  40.         PersonService personService = (PersonService)ctx.getBean("personService");
  41.         personService.save();
  42.         ctx.close();
  43.         
  44.     }
  45. }

    输出:
  46. log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
  47. log4j:WARN Please initialize the log4j system properly.
  48. 执行PersonDaoBean中的add()方法







阅读(951) | 评论(0) | 转发(0) |
0

上一篇:依赖注入DI自动装配

下一篇:spring AOP编程

给主人留下些什么吧!~~