SpringDay1

Spring笔记

DAY01

IOC配置:
bean
bean属性scope
bean声明周期
bean对象创建方式(了解)
DI
set注入
构造器注入
集合注入:
list
props
array
set
map
使用p命名空间简化配置(了解)
SPEL(了解)
properties文件
团队开发
ApplicationContext
第三方资源配置

spring简介:

  • 底层核心容器
    • Beans
    • Core
    • Context
    • SpringEl表达式
  • 中间层技术
    • Aop
    • Aspects
  • 底层技术
    • 数据访问与数据集成
    • Web集成
    • Web实现
  • 基于Test测试

在这里插入图片描述

IOC简介

IOC是Inversion of Control的缩写,多数书籍翻译成“控制反转”
耦合与内聚
耦合:(Coupling):代码书写过程中所使用技术的结合紧密度,用于衡量各个模块之间的互联程度。
内聚:(Cohesion):代码书写过程中单个模块内部各组成部分间的联系,用于衡量软件中各个功能模块内部的功能联系。
程序书写目标:高内聚,低耦合。

Ioc配置(xml格式)
bean

  • 类型:标签

  • 归属:beans标签

  • 作用:定义Spring中的资源,受此标签定义的资源收到spring的控制

  • 格式:

    	<beans>
    		<bean />
    	</beans>
    
  • 基本属性:

    <bean 	id="beanId" :bean的名称,用于获取bean即从ioc容器中找到该类
    		name="beanName1,beanName2" :可以通过name值获取bean,方便多人配合时代码编写
    		class="ClassName">:bean的类型
    </bean>
    

bean属性Scope

  • 类型:属性

  • 归于bean标签

  • 作用:定义bean的作用范围,一般最多使用的就是junit的test范围

  • 格式:

    <bean scope="singleton">
    当scope=‘singletion’时,spring容只会给你创建出一个对象供使用
    当scope=“prototype”时,会创建出多个对象供使用
    </bean>
    
  • 取值:
    request、session、application、 websocket :设定创建出的对象放置在web容器对应的位置

bean生命周期

  • 名称:init-method,destory-method

  • 类型:属性

  • 归属:bean标签

  • 作用:定义bean对象在初始化或销毁时的工作

  • 格式:

    <bean init-method="init" (引号中写具体的方法名)
    		destroy-method="destroy></bean>
    
    • 当scope=“singleton”时,关闭容器会导致bean实例的销毁,调用destroy方法一次

    • 当scope=“prototype”时,对象的销毁由垃圾回收机制gc()控制,destroy方法将不会被执行

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    bean对象创建方式
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   	- 名称:factory-bean
      
  • 类型:属性

  • 归属:bean标签

  • 作用:定义bean对象创建方式,使用静态工厂的形式创建bean,兼容早期遗留系统的升级工作

  • 格式:

<bean class="FactoryClassName"
	factory-method="factoryMethodName">
</bean>
  • 取值:工厂bean中用于获取对象的静态方法名

  • 注意事项:

    • class属性必须配置成静态工厂的类名 factory-bean,factory-method
  • 名称:factory-bean,factory-method

  • 类型:属性

  • 归属:bean标签

  • 作用:定义bean对象创建方式,使用实例工厂的形式创建bean,兼容早期遗留系统的升级工作

  • 格式:

 <bean factory-bean="factoryBeanId" factory-method="factoryMethodName"></bean>
  • 取值:工厂bean中用于获取对象的实例方法名

  • 注意事项:

    • 使用实例工厂创建bean首先需要将实例工厂配置bean,交由spring进行管理

    • factory-bean是实例工厂的beanId

DI

Ioc(Inversion Of Control) 控制反转,Spring反向控制应用所需要使用的外部资源
DI(Dependency Injection)依赖注入,应用程序运行依赖的资源由Spring为其提供,资源进入应用程序的方式称其为注入。
	简单来说(我自己理解):就是从spring 容器中直接拿取到所需要的类对象

使用Set注入(主流)

  • 名称:property

  • 类型:标签

  • 归属:bean

  • 作用:使用set方法的形式为bean提供资源

  • 格式:

    <bean>
    	<property />此处类似于类中的属性值
    </bean>
    
  • 基本属性

    <property name="propertyName"  name:对应bean类中的属性名,要求该属性必须是可以访问的set方法(即set方法名 set后的名字首字母小写  与其对应)
       		 value="propertyValue"  value:设定非引用类型属性对应的值,不能于ref同时使用
       		 ref="beanId"/> ref:设定引用类型属性的对应的值
       		 一般,value是直接赋值,ref则用于使用外部资源
    

构造器注入

  • 名称:constructor-arg
  • 类型:标签
  • 归属:bean标签
  • 作用:使用构造方法的形式为bean提供资源,兼容早期遗留系统的升级工作
  • 格式:
    <bean>
       <constructor-arg />
    </bean>
    
  • 基本属性:
    <constructor-arg name="argsName" value="argsValue />
       name:对应bean中的构造方法所携带的参数名
       value:设定非引用类型构造方法对应的值,不能于ref同时使用
    
  • 其他属性
    <constructor-arg index="arg-index" type="arg-type" ref="beanId"/>
    ref:设定引用类型构造方法参数对应的bean的id,不能于value同时使用
    type:设定构造方法参数的类型,用于按类匹配参数或进行类型校验
    index:设定构造方法参数的位置,用于按位置匹配参数,参数index值从0开始及技术
    注意:一个bean可以有多个constructor-arg标签
    

集合类型数据注入

  • 名称:array,list,set,map,props
  • 类型:标签
  • 归属:property标签或construstor-arg
  • 作用:注入几个数据类型属性
  • 格式:
    <property>
    	<list></list>
    </property>
    

集合类型数据注入——list
<这里的格式调不好了以后有机会再搞吧>

<property name="al">
    <list>
        <value>itheima</value>
        <value>66666</value>
    </list>
</property>

集合类型注入——props

<property name="properties">
    <props>
        <prop key="name">itheima666</prop>
        <prop key="value">666666</prop>
    </props>
</property>

集合类型注入——array

<property name="arr">
    <array>
        <value>123456</value>
        <value>66666</value>
    </array>
</property>

集合类型注入——set

 <property name="hs">
     <set>
         <value>itheima</value>
         <value>66666</value>
     </set>
</property>

集合类型注入——map

<property name="hm">
    <map>
        <entry key="name" value="itheima66666"/>
        <entry key="value" value="6666666666"/>
    </map>
</property>

使用P命名空间简化配置

  • 名称:p:propertyName,p:propertyName-ref
  • 类型:属性
  • 归属:bean标签
  • 作用:为bean注入属性值
  • 格式:
     <bean p:propertyName="propertyValue" p:propertyName-ref="beanId"/>
    
    注意:使用p命令空间需要先开启spring对命令空间的支持,再beans标签中添加对应空间的支持
    <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"       
     xsi:schemaLocation="http://www.springframework.org/schema/beans     
     https://www.springframework.org/schema/beans/spring-beans.xsd">
    

spEL

  • Spring提供了对EL表达式的支持,统一属性注入格式

  • 类型:属性值

  • 归属:value属性值

  • 作用:为bean注入属性值

  • 格式:

    <property value="EL"></bean>
    
  • 注意:所有属性值不区分是否引用类型统一使用value赋值

  • 所有格式统一value=“***
    - 常量 #{10} #{3.14} #{2e5}
    - 引用bean #{beandId}
    - 引用bean属性 #{beanid.propertyName}
    - 引用bean方法 #{beanId.methodName().method2()}–>为了防止空指针异常可以#{beanId.methodName()?.method2()}
    - 引用静态属性或着方法 #(T(java.lang.Math).PI)/#{T(java.lang.Math).randm
    100}
    - 运算符支持#{3lt4==4ge3}
    - 正则表达式支持#{user.name.matches’[a-z]{6,}’}
    - 集合支持 #{likes[3]}
    案例:

     <bean id="userService" class="cn.yhd.service.impl.UserServiceImpl">
         <property name="userDao" value="#{userDao}"/>
         <property name="bookDao" value="#{bookDao}"/>
         <property name="num" value="#{666666666}"/>
         <property name="version" value="#{'itcast'}"/>
    </bean>
    

properties文件
Spring提供了读取外部properties文件的机制,使用读取到的数据为bean的属性赋值
操作步骤

	 1. 准备外部properties文件 		
	 2. 开启context命名空间支持
	xmlns:context="http://www.springframework.org/schema/context"
加载指定的properties文件	
		<context:property-placeholder location="classpath:filename.properties">

使用加载的数据

<property name="propertyName" value="${propertiesName}"/>

注意:如果需要加载所有的properties文件,可以使用 *.propertise 表示加载所有的properties文件
注意:读取数据使用 ${propertiesName} 格式进行,其中propertiesName指properties文件的属性名
团队开发

  • 名称:import
  • 类型:标签
  • 归属:beans标签
  • 作用:再当前配置文件中导入其他配置文件中的项
  • 格式:
        <beans>
           <import />
       </beans>
    
    基本属性:
    <import resource=“config.xml"/>
    
    resource:加载的配置文件名
  • Sping容器加载多个配置文件
    	new ClassPathXmlApplicationContext("config1.xml","config2.xml");
    
  • Spring容器中的bean定义冲突问题
    - 同id的bean,后定义的覆盖先定义的
    - 导入配置文件可以理解为将导入的配置文件赋值粘贴到对应位置
    - 导入配置文件的顺序与位置不同可能会导致最终程序运行结果不同
    ApplicationContext
  1. ApplicationContext是一个接口,提供了访问Spring容器的API
  2. ClassPathXmlApplicationContext是一个类,实现了上述功能
  3. ApplicationContext的顶层接口是BeanFactory
  4. BeanFactory定义了bean相关的最基本操作
  5. ApplicationContext在BeanFactory基础上追加了若干新功能

对比beanfactory

  1. beanfactory创建的bean采用延迟加载形式,使用才创建
  2. ApplicationContext创建的bean默认采用立即加载形式

FileSystemXmlApplicationContext
可以加载文件系统中任意位置的配置文件,而ClassPathXmlApplicationContext只能加载类路径下的配置文件
我也看不懂的图BeanFactory

Resource res = new ClassPathResource(“applicationContext.xml”);
BeanFactory bf = new XmlBeanFactory(res); UserService userService =
(UserService)bf.getBean(“userService”);

第三方资源配置
阿里数据资源方案Druid

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/spring_ioc"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>

总结:

当前页的笔记主要就是一些配置文件记忆性需求大。
在充满仪式感的生活里,一款能传递心意的小工具总能带来意外惊喜。这款基于Java开发的满屏飘字弹幕工具,正是为热爱生活、乐于分享的你而来——它以简洁优雅的视觉效果,将治愈系文字化作灵动弹幕,在屏幕上缓缓流淌,既可以作为送给心仪之人的浪漫彩蛋,也能成为日常自娱自乐、舒缓心情的小确幸。 作为程序员献给crush的心意之作,工具的设计藏满了细节巧思。开发者基于Swing框架构建图形界面,实现了无边框全屏显示效果,搭配毛玻璃质感的弹幕窗口与圆润边角设计,让文字呈现既柔和又不突兀。弹幕内容精选了30条治愈系文案,从“秋天的风很温柔”到“你值得所有温柔”,涵盖生活感悟、自我关怀、浪漫告白等多个维度,每一条都能传递温暖力量;同时支持自定义修改文案库,你可以替换成专属情话、纪念文字或趣味梗,让弹幕更具个性化。 在视觉体验上,工具采用柔和色调生成算法,每一条弹幕都拥有独特的清新配色,搭配半透明渐变效果与平滑的移动动画,既不会遮挡屏幕内容,又能营造出灵动治愈的氛围。开发者还优化了弹幕的生成逻辑,支持自定义窗口大小、移动速度、生成间隔等参数,最多可同时显示60条弹幕,且不会造成电脑卡顿;按下任意按键即可快速关闭程序,操作便捷无负担。 对于Java学习者而言,这款工具更是一份优质的实战参考。源码完整展示了Swing图形界面开发、定时器调度、动画绘制、颜色算法等核心技术,注释清晰、结构简洁,哪怕是初学者也能轻松理解。开发者在AI辅助的基础上,反复调试优化细节,解决了透明度控制、弹幕碰撞、资源占用等多个问题,这份“踩坑实录”也为同类项目开发提供了宝贵经验。 无论是想给喜欢的人制造浪漫惊喜,用满屏文字传递心意;还是想在工作间隙用治愈文案舒缓压力,或是作为Java学习的实战案例参考,这款满屏飘字弹幕工具都能满足你的需求。它没有复杂的操作流程,无需额外配置环境,下载即可运行,用最纯粹的设计传递最真挚的
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2025-06-28T23:56:16.421+08:00 ERROR 20108 --- [SpringDay1] [ restartedMain] o.s.boot.SpringApplication : Application run failed java.lang.IllegalStateException: Error processing condition on org.springframework.ai.autoconfigure.chat.client.ChatClientAutoConfiguration.chatClientBuilderConfigurer at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:60) ~[spring-boot-autoconfigure-3.5.3.jar:3.5.3] at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:99) ~[spring-context-6.2.8.jar:6.2.8] at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:186) ~[spring-context-6.2.8.jar:6.2.8] at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:147) ~[spring-context-6.2.8.jar:6.2.8] at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:123) ~[spring-context-6.2.8.jar:6.2.8] at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:430) ~[spring-context-6.2.8.jar:6.2.8] at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:290) ~[spring-context-6.2.8.jar:6.2.8] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:349) ~[spring-context-6.2.8.jar:6.2.8] at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:118) ~[spring-context-6.2.8.jar:6.2.8] at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:791) ~[spring-context-6.2.8.jar:6.2.8] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:609) ~[spring-context-6.2.8.jar:6.2.8] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.5.3.jar:3.5.3] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) ~[spring-boot-3.5.3.jar:3.5.3] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) ~[spring-boot-3.5.3.jar:3.5.3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:318) ~[spring-boot-3.5.3.jar:3.5.3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1361) ~[spring-boot-3.5.3.jar:3.5.3] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1350) ~[spring-boot-3.5.3.jar:3.5.3] at com.spring.SpringDay1Application.main(SpringDay1Application.java:11) ~[classes/:na] at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:565) ~[na:na] at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.5.3.jar:3.5.3] Caused by: java.lang.IllegalStateException: @ConditionalOnMissingBean did not specify a bean using type, name or annotation and the attempt to deduce the bean's type failed at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.validate(OnBeanCondition.java:630) ~[spring-boot-autoconfigure-3.5.3.jar:3.5.3] at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.<init>(OnBeanCondition.java:577) ~[spring-boot-autoconfigure-3.5.3.jar:3.5.3] at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:142) ~[spring-boot-autoconfigure-3.5.3.jar:3.5.3] at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-3.5.3.jar:3.5.3] ... 20 common frames omitted Caused by: org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanTypeDeductionException: Failed to deduce bean type for org.springframework.ai.autoconfigure.chat.client.ChatClientAutoConfiguration.chatClientBuilderConfigurer at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.deducedBeanTypeForBeanMethod(OnBeanCondition.java:659) ~[spring-boot-autoconfigure-3.5.3.jar:3.5.3] at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.deducedBeanType(OnBeanCondition.java:649) ~[spring-boot-autoconfigure-3.5.3.jar:3.5.3] at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.<init>(OnBeanCondition.java:570) ~[spring-boot-autoconfigure-3.5.3.jar:3.5.3] ... 22 common frames omitted Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.ai.autoconfigure.chat.client.ChatClientAutoConfiguration] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@659e0bfd] at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:483) ~[spring-core-6.2.8.jar:6.2.8] at org.springframework.util.ReflectionUtils.findMethod(ReflectionUtils.java:238) ~[spring-core-6.2.8.jar:6.2.8] at org.springframework.util.ReflectionUtils.findMethod(ReflectionUtils.java:218) ~[spring-core-6.2.8.jar:6.2.8] at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.findBeanMethod(OnBeanCondition.java:688) ~[spring-boot-autoconfigure-3.5.3.jar:3.5.3] at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.getMethodReturnType(OnBeanCondition.java:683) ~[spring-boot-autoconfigure-3.5.3.jar:3.5.3] at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.getReturnType(OnBeanCondition.java:667) ~[spring-boot-autoconfigure-3.5.3.jar:3.5.3] at org.springframework.boot.autoconfigure.condition.OnBeanCondition$Spec.deducedBeanTypeForBeanMethod(OnBeanCondition.java:656) ~[spring-boot-autoconfigure-3.5.3.jar:3.5.3] ... 24 common frames omitted Caused by: java.lang.NoClassDefFoundError: org/springframework/ai/chat/client/observation/ChatClientInputContentObservationFilter at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na] at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3035) ~[na:na] at java.base/java.lang.Class.getDeclaredMethods(Class.java:2331) ~[na:na] at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:465) ~[spring-core-6.2.8.jar:6.2.8] ... 30 common frames omitted Caused by: java.lang.ClassNotFoundException: org.springframework.ai.chat.client.observation.ChatClientInputContentObservationFilter at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:580) ~[na:na] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:490) ~[na:na] ... 34 common frames omitted 如何解决
06-29
Spring Boot 应用启动失败涉及 `ChatClientAutoConfiguration` 的条件评估错误,通常与自动配置类的依赖缺失或版本不兼容有关。这类问题可能表现为以下两种主要异常信息: 1. **@ConditionalOnMissingBean could not deduce bean type** 此问题是由于 Spring Boot 无法推断出期望的 Bean 类型,通常发生在使用 `@ConditionalOnMissingBean` 注解时没有明确指定类型、名称或注解。这种情况下需要确保相关的依赖项已正确引入,并且目标 Bean 可以被 Spring 容器识别[^3]。 2. **NoClassDefFoundError for ChatClientInputContentObservationFilter** 这种错误通常表示某个类在编译时存在,但在运行时找不到。这可能是由于相关依赖未被正确引入到项目中,或者不同模块之间的版本不一致导致的。对于 Spring Boot 3.x 版本,建议检查是否使用了与当前 Spring Boot 版本兼容的依赖库[^1]。 ### 解决方案 - **确认依赖版本兼容性** 检查项目中所有与 Spring 相关的依赖版本,尤其是 `spring-web`、`spring-boot-starter` 和 `spring-cloud-starter` 等核心模块。例如,在 Spring Boot 3.2 中,默认使用的 `spring-web` 版本应为 6.1.x,因此需确保 `pom.xml` 或 `build.gradle` 文件中这些依赖的版本与 Spring Boot 主版本兼容[^3]。 - **清理并重新构建项目** 使用 Maven 或 Gradle 清理本地仓库缓存并重新下载依赖: ```bash mvn clean install -U ``` 或者 ```bash gradle clean build --refresh-dependencies ``` - **升级 Spring Cloud 依赖** 如果使用了 Spring Cloud 相关组件(如服务发现、配置中心等),请确保其版本也与 Spring Boot 3.2 兼容。例如,Spring Cloud 2023.x.x 是适用于 Spring Boot 3.x 的版本。 - **检查自动配置排除** 在某些情况下,可以通过显式排除冲突的自动配置类来绕过问题: ```yaml spring: autoconfigure: exclude: org.springframework.boot.autoconfigure.chat.ChatClientAutoConfiguration ``` - **启用调试日志** 启用 Spring Boot 的自动配置报告和调试日志有助于进一步诊断问题根源: ```yaml logging: level: org.springframework.boot.autoconfigure: DEBUG ``` 通过上述措施,可以有效解决由 `ChatClientAutoConfiguration` 引发的启动失败问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值