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>

总结:

当前页的笔记主要就是一些配置文件记忆性需求大。
内容概要:本文介绍了一个基于MATLAB实现的无人机三维路径规划项目,采用蚁群算法(ACO)与多层感知机(MLP)相结合的混合模型(ACO-MLP)。该模型通过三维环境离散化建模,利用ACO进行全局路径搜索,并引入MLP对环境特征进行自适应学习与启发因子优化,实现路径的动态调整与多目标优化。项目解决了高维空间建模、动态障碍规避、局部最优陷阱、算法实时性及多目标权衡等关键技术难题,结合并行计算与参数自适应机制,提升了路径规划的智能性、安全性和工程适用性。文中提供了详细的模型架构、核心算法流程及MATLAB代码示例,涵盖空间建模、信息素更新、MLP训练与融合优化等关键步骤。; 适合人群:具备一定MATLAB编程基础,熟悉智能优化算法与神经网络的高校学生、科研人员及从事无人机路径规划相关工作的工程师;适合从事智能无人系统、自动驾驶、机器人导航等领域的研究人员; 使用场景及目标:①应用于复杂三维环境下的无人机路径规划,如城市物流、灾害救援、军事侦察等场景;②实现飞行安全、能耗优化、路径平滑与实时避障等多目标协同优化;③为智能无人系统的自主决策与环境适应能力提供算法支持; 阅读建议:此资源结合理论模型与MATLAB实践,建议读者在理解ACO与MLP基本原理的基础上,结合代码示例进行仿真调试,重点关注ACO-MLP融合机制、多目标优化函数设计及参数自适应策略的实现,以深入掌握混合智能算法在工程中的应用方法。
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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值