Spring、Spring自动扫描和管理Bean

时间:2024-01-11 17:24:56

Spring2.5为我们引入了组件自动扫描机制,它可以在类路径下寻找标记了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入到spring容器中管理,它的作用和在xml中使用bean节点配置组件一样。

Spring、Spring自动扫描和管理Bean

1. 引入context命名空间(在Spring的配置文件中),并打开组件自动扫描配置,并指定要纳入管理的命名空间(Spring自动扫描命名空间下的及命名空间子级下的所有类),配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<context:component-scan base-package="cn" /> <!-- 自动扫描配置节点 -->
</beans>

2.在ServiceBean中加入注解,使用@Service注解:

@Service @Scope("prototype")
public class PersonServiceImpl implements PersonIService {
@Resource
private PersonIDao personIDao; public void setPersonIDao(PersonIDao personIDao) {
this.personIDao = personIDao;
}
public PersonIDao getPersonIDao() {
return personIDao;
}
@Override
public void save() {
personIDao.save();
}
}

3.在daoBean中加入注解@Repository:

@Repository
public class PersonDaoImpl implements PersonIDao{
@Override
public void save() {
System.out.println("我是dao的Save方法");
}
}

测试代码:

	@Test
public void test4() {
AbstractApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
PersonIService personIService=(PersonIService)ac.getBean("personServiceImpl");
personIService.save();
}

注:如果在ServiceBean中不在@Service中指定name,那默认在getBean时,bean的name是ServiceBean的名称(首字母小写);

还可以为ServiceBean指定单独的name,通过特定的name获取Bean:

@Service("personIService")
public class PersonServiceImpl implements PersonIService {

DaoBean也是一样的,也可以指定name:

@Repository("personIDao")
public class PersonDaoImpl implements PersonIDao{

4.在ServiceBean中还可以通过@Scope来指定ServiceBean的作用域:

@Scope("prototype") 可以保证当有请求的时候都创建一个Action对象

@Scope("prototype")
值有:singleton,prototype,session,request,session,globalSession
@Service("personIService") @Scope("prototype")
public class PersonServiceImpl implements PersonIService {

测试代码:

@Test
public void test4() {
AbstractApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
PersonIService personIService=(PersonIService)ac.getBean("personIService");
PersonIService personIService2=(PersonIService)ac.getBean("personIService");
System.out.println(personIService==personIService2);
}

Spring、Spring自动扫描和管理Bean

5.@PostConstruct 和@PreDestroy 可以指定当Bean初始化和销毁的时候能够执行的方法:

@PostConstruct 

相当于init-method,使用在方法上,当Bean初始化时执行。

 

@PreDestroy 

相当于destory-method,使用在方法上,当Bean销毁时执行。

	@PostConstruct
public void initBean(){
System.out.println("被实例之前调用");
}
@PreDestroy
public void destoryBean(){
System.out.println("实例被关闭之后调用");
}

测试代码:

	@Test
public void test4() {
AbstractApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
PersonIService personIService=(PersonIService)ac.getBean("personIService");
personIService.save();
ac.close();
}

Spring、Spring自动扫描和管理Bean