spring2.5学习笔记(五):spring自动扫描和管理Bean

本文介绍Spring框架2.5版本中引入的自动组件扫描机制,通过使用@Component、@Service等注解,简化大型项目的Bean配置过程。
当我们使用xml的bean标签来配置组件时,如果遇到很大的项目会有成百上千个组件,那可以想象,xml文件体积是多么的庞大,维护起来也不太方便。spring2.5引入了组件的自动扫描机制,它可以在类路径需找标注了@Service,@Repository,@Controller,@Component这四个注解的类,来加以自动管理。

第一步:
beans2.xml

<?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
">
<!-- 以下是spring自动管理bean 的演示(两步:一:配置文件中有如下语句,二:源文件中有@Service,@Repository,@Controller,@Component) -->
<context:component-scan base-package="cn.itcast"></context:component-scan>
</beans>


base-package="cn.itcast"说明了要扫描的包(包括子包)

以下三句是使用spring自动管理bean必不可少的:
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd


第二步:定义的接口IPersonService,和接口实现,以及业务实现类PersonServiceBean2

package cn.itcast.service;
public interface IPersonService {
}


package cn.itcast.dao.impl;
public interface IPersonDao {
public abstract void add();
}

package cn.itcast.dao.impl;
import org.springframework.stereotype.Repository;
@Repository("persondao")
public class PersonDaoBean implements IPersonDao {
public void add(){
System.out.println("执行PersonDaoBean类中的add方法");
}
}


package cn.itcast.service.impl;
import cn.itcast.service.IPersonService;
@Service("personService_zidong")
public class PersonServiceBean2 implements IPersonService {
private IPersonDao iPersonDao;
public void save() {
iPersonDao.add();
}
}


在这里运用的@Service("personService_zidong"),就相当于,在配置文件里面做了这样的配置:
<bean id="persondao" class="cn.itcast.dao.impl.PersonDaoBean">
</bean>
<bean id="personService_zidong" class="cn.itcast.service.impl.PersonServiceBean" >

测试类:

package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.itcast.service.IPersonService;
public class springTest2 {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void instanceSpring(){
ApplicationContext ac = new ClassPathXmlApplicationContext("beans2.xml");
IPersonService ipersonService2 = (IPersonService)ac.getBean("personService_zidong");
ipersonService2.save();
}
}



学习感想:
1:@Service,@Repository,@Controller,@Component四个注解的区别,还有待研究
2:这样spring自动管理后,如果要得到某个类的Bean直接上类头上面看就是了,@Service("personService_zidong") ,里面的personService_zidong就是bean的id取值
**项目概述:** 本资源提供了一套采用Vue.js与JavaScript技术栈构建的古籍文献文字检测与识别系统的完整源代码及相关项目文档。当前系统版本为`v4.0+`,基于`vue-cli`脚手架工具开发。 **环境配置与运行指引:** 1. **获取项目文件**后,进入项目主目录。 2. 执行依赖安装命令: ```bash npm install ``` 若网络环境导致安装缓慢,可通过指定镜像源加速: ```bash npm install --registry=https://registry.npm.taobao.org ``` 3. 启动本地开发服务器: ```bash npm run dev ``` 启动后,可在浏览器中查看运行效果。 **构建与部署:** - 生成测试环境产物: ```bash npm run build:stage ``` - 生成生产环境优化版本: ```bash npm run build:prod ``` **辅助操作命令:** - 预览构建后效果: ```bash npm run preview ``` - 结合资源分析报告预览: ```bash npm run preview -- --report ``` - 代码质量检查与自动修复: ```bash npm run lint npm run lint -- --fix ``` **适用说明:** 本系统代码经过完整功能验证,运行稳定可靠。适用于计算机科学、人工智能、电子信息工程等相关专业的高校师生、研究人员及开发人员,可用于学术研究、课程实践、毕业设计或项目原型开发。使用者可在现有基础上进行功能扩展或定制修改,以满足特定应用场景需求。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值