http://blog.csdn.net/yerenyuan_pku/article/details/52861403

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

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:component-scan base-package="cn.itcast" /> </beans>
  • 1
  • 2

其中<context:component-scan base-package="cn.itcast" />这个配置隐式注册了多个对注解进行解析处理的处理器,包括<context:annotation-config/>该配置注册的处理器,也就是说写了<context:component-scan base-package="cn.itcast" />配置,就不用写<context:annotation-config/>配置了,此外base-package为需要扫描的包(含子包)。 
@Service用于标注业务层组件、 @Controller用于标注控制层组件(如Struts2中的action)、@Repository用于标注数据访问组件,即DAO组件。而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。 
本文是建立在@Autowire注解与自动装配的案例基础上的。我们首先将Spring的配置文件改为:

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:component-scan base-package="cn.itcast" /> </beans>
  • 1

然后使用@Service注解标注PersonServiceBean类,如下:

@Service
public class PersonServiceBean implements PersonService {
private PersonDao personDao; public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} @Override
public void save() {
personDao.add();
}
}
  • 1

使用@Repository注解标注PersonDaoBean类,如下:

@Repository
public class PersonDaoBean implements PersonDao {
@Override
public void add() {
System.out.println("执行PersonDaoBean中的add()方法");
}
}
  • 1

最后,我们修改SpringTest类的代码为:

public class SpringTest {

    @Test
public void instanceSpring() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personServiceBean");
PersonDao personDao = (PersonDao) ctx.getBean("personDaoBean");
System.out.println(personService);
System.out.println(personDao);
ctx.close();
} }
  • 1

测试instanceSpring()方法,可看到Eclipse控制台打印: 

如果我们想使用按指定名称获取,可将PersonServiceBean类的代码修改为:

@Service("personService")
public class PersonServiceBean implements PersonService {
private PersonDao personDao; public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} @Override
public void save() {
personDao.add();
}
}
  • 1

这样,SpringTest类的代码应改为:

public class SpringTest {

    @Test
public void instanceSpring() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personService");
System.out.println(personService);
ctx.close();
} }
  • 1
  • 2

测试instanceSpring()方法,可看到Eclipse控制台打印: 

我们前面学过Spring管理的bean的作用域,我们就能知道以上Spring管理的两个bean的作用域默认是singleton。当然了,我们也可以更改Spring管理的bean的作用域,如将PersonServiceBean类的代码改为:

@Service("personService") @Scope("prototype")
public class PersonServiceBean implements PersonService {
private PersonDao personDao; public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} @Override
public void save() {
personDao.add();
}
}
  • 1

意味着Spring管理的PersonServiceBean这个bean的作用域变成prototype了,这时我们将SpringTest类的代码修改为:

public class SpringTest {

    @Test
public void instanceSpring() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService1 = (PersonService) ctx.getBean("personService");
PersonService personService2 = (PersonService) ctx.getBean("personService");
System.out.println(personService1 == personService2);
ctx.close();
} }
  • 1

测试instanceSpring()方法,可看到Eclipse控制台打印: 

prototype作用域本来就意味着每次从Spring容器获取bean都是新的对象嘛。

若是通过在classpath路径下自动扫描方这种式把组件纳入Spring容器中管理,如何指定bean的初始化方法和销毁方法呢?这时我们就需要用到两个注解:@PostConstruct和@PreDestroy。为了试验,我们将PersonServiceBean类的代码修改为:

@Service("personService")
public class PersonServiceBean implements PersonService {
private PersonDao personDao; @PostConstruct
public void init() {
System.out.println("初始化资源");
} @PreDestroy
public void destroy() {
System.out.println("销毁、关闭资源");
} public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
} @Override
public void save() {
personDao.add();
}
}
  • 1

接下来还要将SpringTest类的代码修改为:

public class SpringTest {

    @Test
public void instanceSpring() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personService");
ctx.close();
} }
  • 1

这样,测试instanceSpring()方法,Eclipse控制台会打印: 

如要查看源码,可点击让Spring自动扫描和管理Bean进行下载。

 
 

(转)让Spring自动扫描和管理Bean的更多相关文章

  1. Spring、Spring自动扫描和管理Bean

    Spring2.5为我们引入了组件自动扫描机制,它可以在类路径下寻找标记了@Component.@Service.@Controller.@Repository注解的类,并把这些类纳入到spring容 ...

  2. Spring第八发—自动装配及让Spring自动扫描和管理Bean

    依赖注入–自动装配依赖对象(了解即可) 对于自动装配,大家了解一下就可以了,实在不推荐大家使用.例子: byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到 ...

  3. Spring是如何管理Bean

    容器是什么?spring中是如何体现的?一直有疑惑,这两天看了一下Spring管理bean的Demo,对于Spring中的容器有了简单的认识. 我们知道,容器是一个空间的概念,一般理解为可盛放物体的地 ...

  4. 使用Spring和SpringMVC管理bean时要注意的一个小细节

    最近一直在做毕业设计...用到了Shiro和SpringMVC..用过shiro的朋友都知道shiro需要自己去写Realm,然后把Realm注入到SecurityManager中.而Security ...

  5. Spring中管理Bean以及解析XML

    Spring是分层的轻量级框架 以IoC(Inverse of Control 反转控制)和AOP(Aspect Oriented Programming 面向切面编程)为核心 应用Spring的好处 ...

  6. 采用Spring管理Bean和依赖注入

    1. 实例化spring容器和从容器获取Bean对象 实例化Spring容器常用的两种方式: 方法一: 在类路径下寻找配置文件来实例化容器 [推荐使用] ApplicationContext ctx ...

  7. Spring的依赖注入和管理Bean

    采用Spring管理Bean和依赖注入 1.实例化spring容器 和 从容器获取Bean对象 实例化Spring容器常用的两种方式: 方法一: 在类路径下寻找配置文件来实例化容器 [推荐使用] Ap ...

  8. Spring的自动扫描与管理

    通过在classpath自动扫描方式把组件纳入spring容器中管理 前面的例子我们都是使用XML的bean定义来配置组件.在一个稍大的项目中,通常会有上百个组件,如果这些这组件采用xml的bean定 ...

  9. 通过在classpath自动扫描方式把组件纳入spring容器中管理。

    前面的例子我们都是使用xml的bean定义来配置组件,如果组件过多很臃肿.spring2.5引入了组件自动扫描机制,在指定目录下查找标注了@Component.@Service.@Controller ...

随机推荐

  1. BZOJ_2017_[Usaco2009 Nov]硬币游戏_博弈论+DP

    BZOJ_2017_[Usaco2009 Nov]硬币游戏_博弈论+DP Description 农夫约翰的奶牛喜欢玩硬币游戏,因此他发明了一种称为“Xoinc”的两人硬币游戏. 初始时,一个有N(5 ...

  2. 字符指针unsigned char *ch_p

    指向类型为unsigned char的指针变量叫字符指针. 例如: unsigned char ch = 'a'; unsigned char *ch_p = &ch;那么指针变量ch_p就是 ...

  3. 6-4 Haar特征1

    实际上特征就是图像中某个区域的像素点,经过某种四则运算之后得到的结果.所以说图像的特征它是像素经过运算之后得到的某一个结果.这个结果可以是一个具体的值,也可以是一个向量,又或是一个多维的元素.所以说特 ...

  4. 3、HTML的body内标签1

    一.特殊符号的表示   #代指空格 < #代指,< > #代指,> ...... #这玩意有很多,记也记不完,用的时候查一下即可: 二.p和br标签 <p>< ...

  5. 039--CSS

    一.CSS定义 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. ''' selector { property: value; property: value; ... propert ...

  6. DateTime.Now.ToString("yyyy/MM/dd") 输出的结果是 2006-03-16(转)

    今天我在使用 DateTime.Now.ToString("yyyy/MM/dd") 输出的结果是 2006-03-16 而不是我想要的 2006/03/16,都快把我郁闷的不行了 ...

  7. NC文件的处理【netcdf】

    NC是气象领域数据的标准格式之一. 能够更好的存储格点数据. 下面为测试NC文件的读写. git:https://git.oschina.net/ipnunu/nctest pom.xml <p ...

  8. K

    https://codeforces.com/gym/101982/attachments K 打表暴力 /*#include<bits/stdc++.h> using namespace ...

  9. Cg(c for graphic)语言的数据类(转)

    抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人” 本章将着重介绍Cg语言中预定义的内置(bu ...

  10. c++ 头文件路径选择

    单文件引用头文件./ 当前目录 ../ 父级目录 / 根目录              多文件引用头文件多文件引用头文件 定义单独放在cpp文件里面 ,声明放在().h)里面