Spring学习笔记(9)----让Spring自动扫描和管理Bean

Java代码
  1. package com.szy.spring.service;   
  2.   
  3. import org.springframework.stereotype.Service;   
  4.   
  5. import com.szy.spring.dao.PersonDao;   
  6. @Service("service")   
  7. public class UserServiceImpl implements UserService   
  8. {   
  9.     private PersonDao personDaoBean;   
  10.        
  11.     public void show()   
  12.     {   
  13.         personDaoBean.show();   
  14.     }   
  15.   
  16.     public void setPersonDaoBean(PersonDao personDaoBean)   
  17.     {   
  18.         this.personDaoBean = personDaoBean;   
  19.     }   
  20. }  

 在前面的例子中,都是使用XML的bean定义来使用组件,在大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会使配置文件显得很臃肿,查找和维护起来不方便。Spring2.5为我们引入了组件自动扫描机制,它可以在类路径下寻找标记了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入到spring容器中管理,它的作用和在xml中使用bean节点配置组件一样。要使用自动扫描机制,我们需要把配置文件如下配置:

Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  7.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  8.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  9.     <context:component-scan base-package="com.szy.spring"></context:component-scan>  
  10. </beans>  

 其中base-package为需要扫描的包(包括子包)

@Service用于标注业务层的组件,@Controller用于标注控制层组件(如struts中的action),@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。但是在目前的spring版本中,这几个注解的作用是一样的,但是在以后可能会进行区分。

 

下面把先前的例子修改一下:

首先是PersonDaoBean类,修改如下

Java代码
  1. package com.szy.spring.dao;   
  2.   
  3. import org.springframework.stereotype.Repository;   
  4.   
  5. @Repository  
  6. //告诉spring这个类要交给spring管理,   
  7. public class PersonDaoBean implements PersonDao   
  8. {   
  9.     public void show()   
  10.     {   
  11.         System.out.println("执行PersonDaoBean中的add()方法");   
  12.     }   
  13. }  

然后是UserServiceImpl类

Java代码
  1. package com.szy.spring.service;   
  2.   
  3. import org.springframework.stereotype.Service;   
  4.   
  5. import com.szy.spring.dao.PersonDao;   
  6. @Service  
  7. //把这个类交给spring管理,作为服务了。   
  8. public class UserServiceImpl implements UserService   
  9. {   
  10.     private PersonDao personDaoBean;   
  11.        
  12.     public void show()   
  13.     {   
  14.         personDaoBean.show();   
  15.     }   
  16.   
  17.     public void setPersonDaoBean(PersonDao personDaoBean)   
  18.     {   
  19.         this.personDaoBean = personDaoBean;   
  20.     }   
  21.   
  22.     public PersonDao getPersonDaoBean()   
  23.     {   
  24.         return personDaoBean;   
  25.     }   
  26. }  

 下面我们进行测试,原来的测试代码是userServiceImpl

Java代码
  1. ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");   
  2.         UserService service=(UserService)ctx.getBean("userService");   
  3.         service.show();  

其中userService是我们在配置文件中配置的bean的id。但是如今我们并没有id这个属性,在spring2.5中,默认的id是类的名称,但是开后是小写,也就是userServiceImpl,因此测试代码应修改如下:

Java代码
  1. AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");   
  2.         UserService service=(UserService)ctx.getBean("userServiceImpl");   
  3.         System.out.println(service);  

如果我们想自己命名的话,则只需在注解后加上括号,里面写入你希望的名字,如

@Service("userService")。

 

在spring中默认的是之生成一个bean实例,如果我们想每次调用都产生一个实例,则标注需如下配置

@Service @Scope("prototype")

 

在xml中我们还可以配置初始化方法和销毁方法,使用标注后只需如下标注

Java代码
  1. @PostConstruct  
  2.     public void init()   
  3.     {   
  4.         System.out.println("初始化");   
  5.     }   
  6.     @PreDestroy  
  7.     public void destory()   
  8.     {   
  9.         System.out.println("销毁");   
  10.     }  

 

 使用注解后,我们的xml文件变得十分简单,因此建议大家在以后的开发中使用注解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值