Spring、Spring自动扫描和管理Bean
2016-12-01 00:00
387 查看
Spring2.5为我们引入了组件自动扫描机制,它可以在类路径下寻找标记了@Component、@Service、@Controller、@Repository注解的类,并把这些类纳入到spring容器中管理,它的作用和在xml中使用bean节点配置组件一样。
1. 引入context命名空间(在Spring的配置文件中),并打开组件自动扫描配置,并指定要纳入管理的命名空间(Spring自动扫描命名空间下的及命名空间子级下的所有类),配置文件如下:
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">[/code]<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); }
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(); }
相关文章推荐
- Spring2.5学习3.4_让Spring自动扫描和管理Bean
- Spring、Spring自动扫描和管理Bean
- Spring、Spring自动扫描和管理Bean
- spring学习笔记4--让sping自动扫描和管理Bean
- Spring(九)让Spring自动扫描和管理Bean
- Spring第八发—自动装配及让Spring自动扫描和管理Bean
- spring_让 spring自动扫描和管理bean
- 第二章 IoC Spring自动扫描和管理Bean
- 让spring自动扫描管理bean
- Spring学习笔记(9)----让Spring自动扫描和管理Bean
- (7) 让Spring自动扫描和管理Bean ---- 通过在classpath自动扫描方式把组件纳入spring容器中管理
- Spring自动扫描和管理Bean
- 让Spring自动扫描和管理Bean
- 引用 Spring学习笔记(让Spring自动扫描和管理Bean)
- Spring-自动扫描管理bean
- JavaEE_SSH_Spring学习笔记(9)----让Spring自动扫描和管理Bean
- Spring自动扫描和管理bean bug1
- (转)让Spring自动扫描和管理Bean
- [置顶] Spring自动扫描和管理Bean
- 让Spring自动扫描和管理Bean