Spring框架入门:Bean装配与生命周期解析

需积分: 9 0 下载量 58 浏览量 更新于2024-09-04 收藏 160KB PDF 举报
"该资源是关于Spring框架的入门学习材料,重点关注Bean的装配过程,特别是通过XML配置的方式。内容涵盖了Bean的配置项、作用域以及生命周期等关键概念。" 在Spring框架中,Bean装配是核心功能之一,它涉及到如何定义、创建、管理和销毁Bean。XML配置是传统且常见的装配方式,以下将详细介绍其中的关键知识点: 1. Bean配置项: - Id:用于标识Bean,确保其在容器中的唯一性。 - Class:指定Bean对应的Java类全路径名,是Bean实例化的基础。 - Scope:定义Bean的作用域,决定何时及如何创建Bean实例。 - Constructorarguments:用于通过构造方法注入依赖。 - Properties:设置Bean的属性值,进行属性注入。 - Autowiringmode:自动装配模式,如byName、byType等,让Spring自动寻找并注入依赖。 - Lazy-initializationmode:是否采用懒加载策略,如果设为true,Bean在首次使用时才初始化。 - Initialization/destructionmethod:指定Bean的初始化和销毁方法。 2. Bean的作用域: - Singleton:默认作用域,整个应用中只有一个实例,适用于全局共享的对象。 - Prototype:每次请求都会创建一个新的实例,适用于需要多个实例的情况。 - Request:在HTTP请求生命周期内创建一个实例,适用于请求相关的数据。 - Session:在用户会话期间保持一个实例,适用于用户特定的数据。 - GlobalSession:在portlet应用中,跨越所有portlet的会话,对于单点登录等场景可能有用。 3. Bean生命周期: - 生命周期步骤:定义Bean,IOC容器加载并创建实例,从容器中获取并使用Bean,最后销毁Bean实例。 - 初始化和销毁:可以指定初始化和销毁方法。在XML中通过`init-method`和`destroy-method`属性设置,或者让Bean实现`InitializingBean`和`DisposableBean`接口,覆盖`afterPropertiesSet()`和`destroy()`方法。 理解这些概念是掌握Spring框架基础的关键,它们有助于开发者更好地管理应用中的对象,实现依赖注入,提高代码的可测试性和可维护性。通过XML配置,开发者可以精细控制Bean的创建和管理,使得应用程序的结构更加清晰,易于维护。

springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.support.ScheduledMethodRunnable#0': Cannot resolve reference to bean 'sysTask' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sysTask': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ecp.crm.service.SaleTrackingService com.ecp.system.task.SysTask.saleTrackingService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'saleTrackingServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.ecp.crm.dao.SaleTrackingDao com.ecp.crm.service.impl.SaleTrackingServiceImpl.fDao; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'saleTrackingDao' defined in file [D:\�����ļ�\soyogit\epm\trunk\target\epm-1.0.0\WEB-INF\classes\com\ecp\crm\dao\SaleTrackingDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory': : Error creating bean with name 'sqlSessionFactory' defined in URL [file:/D:/�����ļ�/soyogit/epm/trunk/target/epm-1.0.0/WEB-INF/classes/spring.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'URL [jar:file:/D:/�����ļ�/soyogit/epm/trunk/target/epm-1.0.0/WEB-INF/lib/ecp-platform-extend-1.9.4.jar!/com/ecp/attachment/dao/mysql/AttachmentAssociateMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: XML fragments parsed from previous mappers already contains value for com.ecp.attachment.dao.AttachmentAssociateMapper.table-name; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in URL [file:/D:/�����ļ�/soyogit/epm/trunk/target/epm-1.0.0/WEB-INF/classes/spring.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'URL [jar:file:/D:/�����ļ�/soyogit/epm/trunk/target/epm-1.0.0/WEB-INF/lib/ecp-platform-extend-1.9.4.jar!/com/ecp/attachment/dao/mysql/AttachmentAssociateMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: XML fragments parsed from previous mappers already contains value for com.ecp.attachment.dao.AttachmentAssociateMapper.table-name

2023-07-22 上传