前面的例子都是使用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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<!-- 配置自动扫描bean 自动寻找标注了@Component、@Service、@Controller、@Repository注解的类 -->
<!-- base-package="com.zyy.service" 扫描com.zyy.service下所有的类(包括子包)-->
<!-- 将它们都纳入Spring容器管理,相当于添加bean标签 -->
<!-- @Service 用与标注业务层组件 -->
<!-- @Controller 用于标注控制层组件(如struts中的action) -->
<!-- @Repository 用于标注数据访问组件,即DAO组件 -->
<!-- @Component 泛指组件,当一个组件不好归类的时候,可以使用这个注解进行标注 -->
<!-- 也包含了 <context:annotation-config></context:annotation-config> 的功能 -->
<context:component-scan base-package="com.zyy.service"></context:component-scan>
</beans>
其中base-package为需要扫描的包(含子包)。
@Service用于标注业务层组件、@Controller用于标注控制层组件(如struts中的action)
@Repository用于标注数据访问组件,即DAO组件。而@Component泛指组件,当组件不好归类
的时候,我们可以使用这个注解进行标注。
package com.zyy.service;
/**
* Created by CaMnter on 2014/8/20.
*/
public interface PersonDao {
void add();
}
package com.zyy.service.impl;
import com.zyy.service.PersonDao;
import org.springframework.stereotype.Repository;
/**
* Created by CaMnter on 2014/8/20.
*/
@Repository("personDaoBean")
public class PersonDaoBean implements PersonDao {
public void add() {
System.out.println("***** PersonDaoBean-add()方法 *****");
}
}
package com.zyy.service;
/**
* Created by CaMnter on 2014/8/20.
*/
public interface PersonService {
void hello();
}
package com.zyy.service.impl;
import com.zyy.service.PersonDao;
import com.zyy.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* Created by CaMnter on 2014/8/20.
*/
/**
* 1、@Component
@Component 是所有受Spring 管理组件的通用形式,@Component注解可以放在类的头上,@Component不推荐使用。
2、@Controller (action)
@Controller对应表现层的Bean,也就是Action
3、@ Service (ServiceImpl)
@Service对应的是业务层Bean 4、@ Repository(DAOImpl)
@Repository对应数据访问层Bean
*/
/**
* @Service("xxx") xxx可以指定bean的名称 默认为personServiceBean_2
* @Service @Scope("yyy") property 或 default(单例)
* @Autowired @Qualifier("sessionFactory") 将Autowired转换为按 名称匹配
* @PostConstruct 标注初始化方法
* @PreDestroy 标注销毁方法
*/
@Service("personServiceBean_2")
public class PersonServiceBean_2 implements PersonService {
@Autowired
@Qualifier("personDaoBean")
private PersonDao personDao;
public PersonDao getPersonDao() {
return personDao;
}
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
@PostConstruct
public void init() {
System.out.println("**** PersonServiceBean_2 init ****");
}
@PreDestroy
public void destory() {
System.out.println("**** destory ****");
}
public void hello() {
this.personDao.add();
}
}
junit4.4测试代码:
@Test
public void test_5() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans_2.xml");
PersonService personService = (PersonService) applicationContext.getBean("personServiceBean_2");
personService.hello();
}