Spring+Ibatis:控制反转与依赖注入的探讨

需积分: 3 2 下载量 96 浏览量 更新于2024-08-18 收藏 687KB PPT 举报
本文档主要探讨了Spring+Ibatis技术中的关键知识点,特别是围绕Spring框架在企业级Java开发中的应用。Spring是一个广泛使用的轻量级Java应用程序框架,它提供了以下几个核心概念: 1. **IoC (Inversion of Control)**: 控制反转是Spring的核心原则之一,它改变了传统编程模式中程序员手动管理对象生命周期的方式。在Spring中,控制权不再由代码直接控制,而是由外部容器(如Spring容器)在运行时进行管理,实现了组件间的松耦合。 2. **DI (Dependency Injection)**: Spring采用依赖注入,使得组件之间的依赖关系在运行时动态确定,而不是在编译时硬编码。这种设计降低了代码复杂度,提高了灵活性和可测试性。 3. **Container (容器)**: 春天的容器负责管理和初始化组件,根据配置文件中的信息,通过setter方法自动注入依赖。Spring容器是无侵入式的,意味着开发者可以使用标准Java代码编写,而无需引入Spring特定的API。 4. **对比EJB容器**: EJB容器是J2EE(Java Enterprise Edition)的一部分,具有高度侵入性,需要组件遵循严格的规范和接口。相比之下,Spring提供了一个更加灵活且易于扩展的容器,可以在任何服务器上部署,不需要组件实现特定接口。 5. **Spring架构特点**: - 轻量级:Spring专注于核心功能,不包含完整的事务管理或JNDI支持,允许开发者选择性地使用其组件。 - 可移植性:Spring可以在任何支持Java的环境中部署,不受服务器类型限制。 - 高度扩展性:由于其非侵入式特性,Spring可以轻松与其他框架集成,增强应用程序的功能。 6. **基于配置的接口编程示例**: 在Spring配置文件中,如给出的 `<bean>` 标签示例,展示了如何使用Spring进行依赖注入。`<property>` 标签用于设置bean实例的属性,如`<reflocal>` 就是将本地bean的引用注入到目标bean中,体现了接口编程的配置方式。 本文档详细介绍了Spring框架的核心组件及其在与Ibatis集成时的作用,以及如何通过配置来实现依赖注入和模块化的应用程序设计。理解并熟练运用这些概念有助于在实际项目中高效地利用Spring提高开发效率和应用程序的质量。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'deptServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'deptMapper' defined in file [D:\WorkSpace\work13\djd_server\target\classes\com\jiading\djd\mapper\DeptMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 40; 元素内容必须由格式正确的字符数据或标记组成。 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1422) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:893) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551) ~[spring-context-5.2.7.RELEASE.jar:5.2.7.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) ~[spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.1.RELEASE.jar:2.3.1.RELEASE] at com.jiading.djd.DjdApplication.main(DjdApplication.java:14) [classes/:na]报错了

2023-07-25 上传