Spring学习之@Value

注解说明 @Value 是Spring提供用于处理属性与表达式注入的注解,可标注在字段、方法或构造器的参数上,并且支持动态解析SpringMVC中的方法参数。支持注入方式: 直接注入如: @Value(“test”) ${my.app.myProp} 属性注入 #{systemProperties.myProp} SpEL表达式 它提供了不同的配置源(属性文件、系统属性)的注入 注解属性 @Value 注解只有一个属性 value ,表示要注入的实际值的表达式,支持属性与SpEL表达式两种方式 测试代码 以下测试代码MyController的username的注入值为: tomyli,而不是配置文件中的admin @Controller public class MyController { @Value("${USER}") @Getter private String username; } @Configuration @ComponentScan(basePackages = "cn.imcompany.bean.autowired") @PropertySource("classpath:application.properties") public class MyConfiguration { } public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class); MyController myController = context.getBean(MyController.class); System.out.println(myController.getUsername()); // tomyi } } application.properties配置文件 USER=admin 源码阅读 @Value 注解的处理由 AutowiredAnnotationBeanPostProcessor 类负责,与处理 @Autowired 注解的类为同一个PostProcessor。主要的处理逻辑实现在 DefaultListableBeanFactory#doResolveDependency 方法,对于 @Value注解的处理主要集中在第2步,解析@Value的操作主要分为3步 ...

June 10, 2025 · 1 min · 170 words · tomyli

Spring学习之@Autowireid

注解说明 @Autowired 是Spring框架的核心注解,用于自动装载构造器、字段、set方法、配置方法上的依赖,它是 JSR-330 @Inject 注解的替代,支持配置 依赖是否必须 选项 属性说明 此注解只有一个属性 required ,默认为 true 表示待注入组件必须存在 注解注入规则说明 构造器注入 如果一个类声明了多个构造器且没有一个显式标注 @Autowired,则Spring会使用默认的构造方法进行装载 如果一个类只声明了一个构造器,不管有没有标注 @Autowired 注解它都会被直接使用 多参数可以配置多个required属性,对于可选注入,可以声明为 Java8 的 Optional 类型 对于可选注入也可以使用 JSR-305 的 javax.annotation.Nullable 注解 字段注入 字段在构造器之后,配置方法注入之前进行自动注入 字段注入支持组件自引用(self references) 方法注入 方法注入不限制方法名与参数个数,所有参数会被Spring进行匹配注入 参数注入 目前只在 spring-test 模块中有效,其它地方使用会被忽略 数组/集合注入 Spring通过匹配数组/集合的值进行注入,Map的key必须为String类型,支持集合中元素的Order配置 不支持 BeanPostProcessor 或者 BeanFactoryPostProcessor 因为真实的注入动作发生在 BeanPostProcessor 阶段,所以不能在 BeanPostProcessor 或者 BeanFactoryPostProcessor 中使用此注解 源码阅读 在Spring中,处理 @Autowired 注解对应的类为 AutowiredAnnotationBeanPostProcessor ,又是一个Bean后置处理器 对于此类,需要关注其实现的两个接口类 MergedBeanDefinitionPostProcessor (运行时合并Bean的定义回调接口)和 InstantiationAwareBeanPostProcessor (在Bean实例化前后进行处理后置处理器) MergedBeanDefinitionPostProcessor 主要是为了在真正bean实例化前准备缓存的bean元数据 InstantiationAwareBeanPostProcessor Bean实例化回调接口,通常用于为bean创建代理 测试代码 @Service public class MyService { } @Controller public class MyController { @Autowired MyService myService; public void showService() { System.out.println("myService = " + myService); } } @Configuration @ComponentScan(basePackages = "cn.imcompany.bean.autowired") public class MyConfiguration { } public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class); MyController myController = context.getBean(MyController.class); myController.showService(); } } 元数据收集 调用栈信息如下: ...

June 6, 2025 · 2 min · 267 words · tomyli

The Key To Accelerating Your Coding Skills(收藏)

加速你编程技能的关键 http://blog.thefirehoseproject.com/posts/learn-to-code-and-be-self-reliant/ 文章收藏防丢 When you learn to code, there is a moment when everything begins to change. At Firehose, we like to call this the inflection point of coding. After this phase, the way you operate as a developer will be dramatically different. Building up to the inflection point is the process of becoming self-sufficient in programming, to the point where you no longer need any hand-holding. It can be a frustrating experience, but once it’s behind you, it is incredibly empowering. At Firehose, our goal isn’t just to teach you Ruby, how to build web applications, or how to write tests. Although we do teach these skills and more, our primary goal is to accelerate students past the inflection point so they gain the ability to solve any problem they encounter. We believe that being able to problem solve on your own is an invaluable skill, and this method of teaching will take you much further than simply learning how to build a set of apps. The first step to becoming a self-sufficient developer is learning how to do specific tasks. Once you master certain tasks, the broad strokes of how the pieces fit together will start to become apparent. Over time, you’ll begin to recognize patterns and eventually, the things that initially seemed confusing and foreign will become second nature. For students starting out, the most important skill to acquire is attention to detail. Paying close attention to detail is important when going through materials like documentation or tutorials. Even the most minor typos and misspellings will result in error messages or bugs. Seeing error messages is a frustrating experience at first, but it’s a crucial step in the learning process. Dealing with error messages and problems in this phase teaches you one of the most important skills of programming within a safe environment: being detail-oriented. Debugging error messages is incredibly important. The fact of the matter is, error messages are just a part of programming: they are seen by inexperienced and very experienced developers alike. The only difference is, the more experience you have dealing with error messages, the less time you’ll need to spend trying to fix them. Here’s why: Over time, you will learn how to read error messages and extract the relevant details of the problem quickly. The first time you see an error message, it will take you a while to decode what it actually means. But after you’ve seen hundreds of error messages (and you will see hundreds!), you will be able to pinpoint the problem’s location and the relevant details you need in order to fix it. You should learn from each error message that you resolve. Don’t just fix the error and be done with it; understand what is wrong with the code you’re fixing. By learning from each of your errors, the next time you make the same mistake, you’ll be able to fix it much faster. Initially, you will probably ask for help on each error message you see. Over time, you’ll learn to ask for help less frequently by double-checking your code and conducting smart Google searches. In the tutorial phase, you will follow instruction. At first, you’ll find it challenging to follow instructions and error messages will happen frequently. Over time, you’ll develop the skill to debug errors and pay better attention to small details, and you’ll be able to make progress much quicker. As you’re wrapping up the tutorial phase, you’ll notice you’re able to write code at a much more rapid pace. At this point, some people feel confident– like they’re ready to ditch the training wheels and start building things without structured guidance– and will happily dive into the deep end. Other students will reach for more tutorials, trying to obtain more domain-specific knowledge in search of a “full understanding.” Unfortunately, tutorials will only take you so far, and true confidence isn’t found in tutorials or guides. True confidence comes from struggling through a problem you have no idea how to solve, and discovering a solution on your own. The dirty little secret of programming is… You will never know everything you need to know to solve all your problems. Going into the journey, you probably envisioned yourself eventually learning everything you need to learn, and then being squared away. This moment will never happen. Programming is a life-long learning experience. Experienced software engineers seek to find solutions to problems they haven’t solved yet because it gives them the opportunity to learn more. If you find yourself waiting for the moment when you finally feel like you know everything there is to know about coding, know this: the day you’re waiting for will never come. And that is a wonderful thing. You will be ready to jump into the next phase of your journey when: You’ve seen enough error messages that they no longer startle you. Instead, you know how to decipher what they mean and where to look for the problems in your code. You’re a pro at Googling for solutions. When you’re working to add a feature or see a confusing error message, you know what to search for to find the information you need. You’re able to reference code you’ve written in other parts of your application and follow patterns within them, rather than always seeking out step-by-step instructions. The Inflection Point (2-4 weeks with the right mentality) The inflection point stage is one of the most frustrating stages of learning to code, but in many ways, it’s the only stage that matters. It’s the point when you phase out of using tutorials and begin solving problems for which no one has lined up a solution for you At some points, you will feel like you aren’t ready to tackle this phase and like you want to return to building something with an outline of exactly what to do. Don’t fall prey to this mentality. The reason you’ll feel frustrated is: During the inflection phase, you will be coding 10-20 times SLOWER than in the previous phase. You may start questioning yourself and wondering if you are actually capable of becoming a programmer. Feelings of insecurity and doubt are common in this stage. Despite the fact that you’ll feel like you’re learning and accomplishing things at a much slower rate, in reality, you are achieving the things that matter the most. While your domain-specific knowledge is screeching to a putter, everything you’re learning will be about procedural knowledge. Procedural knowledge is the ability to teach yourself what you don’t know along the way. When you need to implement a new feature, what type of Google search should you do? At this point in time, you’ll feel like you’re “in the dark” when it comes to many of the things you want to accomplish. Learning how to find the light on your own is critical because you can never know everything there is to know, so you need to be able to teach yourself how to solve the problem at hand. Most people do not realize that in order to learn to code, you need to learn both domain-specific and procedural knowledge. For the rest of your life, go outside your limits every single day Some software engineers stay inside their comfort zone once they find their footing. These types of programmers are known as maintenance programmers– not something you should strive to be. Instead, you should strive to go outside your limits every single day. The most common reason programmers quit their jobs is because “it’s not challenging anymore since I’ve solved all the interesting problems.” Rather than trying to pull coding projects into your comfort zone, you should be seeking out problems that are outside your current skill set. This is the only way to build on and expand your skills. In the words of a Firehose student upon passing his inflection point: I still feel like I’m in the deep end! I’m just getting more comfortable knowing that’s where I’ve got to be! Strategies for Passing the Inflection Point as Efficiently as Possible Because passing the inflection point is the most important part of learning to code, you should set yourself up to make the process as smooth as possible. This means you should start preparing while you’re in the tutorial phase and maintain the right mindset during this period of time. During the tutorial phase, take breaks from going over structured material and give yourself challenge problems along the way. For every handful of lessons, try to do something that is outside the scope of the tutorial you’re following. If the tutorials you’re going through provide “challenges” or “self-directed” pieces, do all of them. Solving unguided challenges will give you the important experience of doing things without structured guidance. Try to use tutorials as little as possible. At Firehose, we often walk students through how to integrate certain gems or do things using the provided documentation. Rather than simply following the instructions explained in tutorials that are geared towards people who are just starting out, many students will follow the documentation and use the tutorials as a back-up. Note that documentation will treat you like a developer who has passed the inflection point. Getting comfortable reading and following documentation on GitHub will give you a leg up when you’re on your own. Focus on the essentials and use repetition. Learn how to do common things like spinning-up an application from scratch, pushing a new app to GitHub and Heroku, and building a database migration early on Pushing through the inflection point can be challenging. Here are some pointers to get you through it: Understand that this is a difficult process and go easy on yourself. Also, set realistic expectations. You can’t compare your “superman”-level speed of going through tutorials to your “snail”-speed of learning things on your own. Keep in mind that you’re learning plenty, but at this phase, you’re learning a brand new skill of figuring new things out on your own. If you’re struggling with self-confidence, know that what you’re feeling is completely normal. Keep working. If you continue to struggle, try talking to someone who has recently passed the inflection point. They will be able to relate to the position you’re in and will assure you that what you’re experiencing is only temporary. Work consistently, but don’t overwork yourself. At this phase of the game, know that you can only be productive for around 6 hours a day at the most. Working in an exhausted state will only prolong the time you spend building up to the inflection point. The best way to gain confidence at this stage is to power through any doubts you have. Your emotions may start to feel like a roller coaster. At times, you’ll feel like you’re on fire, but after 15 hours of struggling on the same problem, it’s very common to feel the polar opposite. It can be frustrating to have no idea if something will take you 5 minutes or 5 hours, but every time you power through and successfully implement a new feature, the rush of confidence will be everything you need. After solving a handful of hard problems without any help, you’ll be addicted to the feeling of building things outside your comfort zone. How to know when you’ve passed the inflection point The final stage of the inflection point process is acceptance. Acceptance that software development is a process of continuous learning. Acceptance that the feeling that you’ve successfully learned everything just means you should start thinking about solving more complicated problems. REF https://www.cnblogs.com/yxiaodao/p/10674891.html

May 30, 2025 · 10 min · 1931 words · tomyli

众乐乐-weekly 第17期

Read 📖 Programming On 34 Keys · oppi.li 作者自己构建了一个分离式的34键的键盘用来日常编程,由于只有34个键,包含26个字母+4个常用符号+4个功能键(Tab,Space, Enter,Backspace),对于其它的功能键就需要使用不同的layter来完成,作者就定义了三种模式来实现不能的功能 NAV层,导航模式,实现上下左右的移动,通过按住左侧的 Space 触发 SYM层,符号模式,包含所有常用的符号,主要集中在左侧键盘,通过按住右侧的 Enter 触发 NUM层,数字模式,包含所有数字,主要集中在右侧键盘 ,通过按住左侧的 Tab 触发 对于常用的 Esc 键盘,作者通过组合按键的方式来实现,分别按住左右两侧的键 Tools 🧰 trycua/cua: c/ua is the Docker Container for Computer-Use AI Agents. 运行AI Agents的Docker容器,这下可以大胆的尝试AI Agents了 crhuber/kelp: A simple alternative to homebrew for installing binary packages on MacOS written in Go. Mac上软件安装管理的工具,homebrew的简易替代品 zen-browser/desktop: Welcome to a calmer internet Firefox浏览器的替代,号称最大限度平衡速度、隐私与效率,试了一下挺不错的,颜值也能打 Our mission is to give you a balance between speed, privacy and productivity! ...

May 16, 2025 · 1 min · 107 words · tomyli

Spring学习之@CompnentScan

注解说明 @ComponentScan 注解是Spring 提供的对组件进行扫描的注解指令,通常与 @Configuration 注解一起使用,支持在类上重复标注 注解能做什么? 此注解支持以下重要特性: 自定义扫描的包范围 配置包含扫描组件的过滤器 配置排除组件的过滤器 是否使用默认过滤器 是否配置懒加载 源码分析 由于此注解通常与 @Configuration 注解一起使用,其解析的主要逻辑都是在 refresh 阶段的 invokeBeanFactoryPostProcessors 方法中,最终会定位到 ConfigurationClassParser#doProcessConfigurationClass() 方法中,在方法中可以看到对 ComponentScan 注解的操作逻辑了 对注解属性的解析 所有对属性的解析都在 ComponentScanAnnotationParser#parse() 方法中进行,此方法中会创建一个 ClassPathBeanDefinitionScanner 实例scanner,此实例初始化过程中会对默认的Filter进行加载 处理默认Filter主要是 JSR-250 与 JSR-330 API 增加的注解 然后会把配置的 includeFilter 和 excludeFilter 设置到 scanner 中,接着会对 basePackage 扫描包属性进行解析配置,如果配置了就添加,没有配置,默认就使用 @ComponentScan 注解标注类所在的包路径。解析完注解属性后,就开始了真正的扫描操作 扫描操作 扫描操作由 ClassPathBeanDefinitionScanner#doScan() 方法进行处理,此方法会调用 findCandidateComponents() 方法在给定的包下查找符合条件的组件,查找的任务由 scanCandidateComponents() 进行,该方法调用 isCandidateComponent() 方法,根据解析的includeFilter和excludeFilter 来选择满足条件的组件 这样扫描到所有满足条件的Bean后,会接着对这些Bean进行parse操作,直到注解再无其它依赖 注册Bean 扫描完所有Bean后,会调用 checkCandidate() 方法进行检查,检查当前要注册的Bean是否与已经注册过的Bean的定义是否兼容,如果冲突会报 ConflictingBeanDefinitionException 。检查没问题后,会调用 registerBeanDefinition() 方法对Bean进行注册,这个过程就涉及到Spring其它的生命周期了(这个后续专门学习总结),对于@ComponentScan 组件来说已经完成了它的使命 总结 最后以注解解析过程中的涉及的核心类做一下总结 常见问题 调整包结构后一些@Bean配置扫描不到了 调用启动类的包路径为最外层,这样可以扫描当前包及子包的配置 默认只扫描当前标注注解的包,可以通过配置 basePackage 属性指定扫描路径,配置后默认的扫描路径就不生效了 如何将未配置Spring相关组件注解的Bean纳入Spring管理? 可以通过配置 ComponentScan的includeFilter 来实现 ...

May 15, 2025 · 1 min · 99 words · tomyli

Spring学习之@Configuration

@Configuration是什么? @Configuration 是 Spring提供的一个用于配置与声明Bean与相应Bean之间依赖的注解,根据文档描述,此注解通常由 AnnotationConfigApplicationContext 上下文进行启动加载 @Configuration能做什么? 先看一下此注解的声明,在此注解上标识了 @Component ,则证明此注解是一个 Component,拥有 @Component 注解的基础能力(可以被Spring容器自动扫描与加载) 上图标识了注解常用的配置方式,分类为以下几种 属性资源导入 引入其它配置类的配置 Value注入 Spring容器组件注入 支持Profile配置 注解属性 在Spring5.x版本中,只支持两个属性配置 value 配置注解的名称 proxyBeanMethods 是否代理增强(基于CGLIB)标识 @Bean 的方法,默认为true,保证声明的bean是单例共享的,而不是每一次通过方法调用都生成一个新的 此属性的两个值被定义为两种模式 full 模式(值为true) lite 模式(值为false) 源码分析 @Configuration 注解的解析类为 ConfigurationClassPostProcessor ,本次主要关注从 AnnotationConfigApplicationContext 启动后的整个处理过程, AnnotationConfigApplicationContext 启动分为三个阶段 先上一张三个阶段的涉及的主要类与调用关系图 初始化 初始化reader 此过程会实例化 AnnotatedBeanDefinitionReader 类,此类在实例化时会调用 AnnotationConfigUtils.registerAnnotationConfigProcessors 来注册解析配置的Processor,如果registry中没有包含 ConfigurationClassPostProcessor 类的定义,则会新new一个 ConfigurationClassPostProcessor 的Bean定义类 整体调用链路如下: 初始化scanner 此过程会实例化Bean定义扫描器 ClassPathBeanDefinitionScanner , 在此过程中主要会设置 Environment 和 ResourceLoader 相关信息 注册 注册阶段主要是对标注了 @Configuration 类(MyConfig)进行注册,以便Spring可以正常识别,主要逻辑在 AnnotatedBeanDefinitionReader#doRegisterBean 中,此方法会调用 BeanDefinitionReaderUtils.registerBeanDefinition 对此Bean进行注册 至此, @Configuration 类信息就注册到了 Spring 容器中 ...

May 9, 2025 · 1 min · 202 words · tomyli

众乐乐-weekly 第16期

Read 📖 CLion Is Now Free for Non-Commercial Use | The CLion Blog C编辑器CLion对非商业使用免费了 voideditor/void 开源的Cursor编辑器,基于VSCode开发 BrokkAi/brokk: Brokk brings code intelligence to AI 号称理解代码语法,对于大型项目支持更好的AI工具 奇客Solidot | 食用超加工食品或有害健康 目前超加工食品越来越多,我们被困在了里面 BHznJNs/MotionEaseTune: A simple application that can prevent you from motion sickness via 100Hz sound. 播放100Hz声音来预防晕车的Android应用 Ebrizzzz/Youtube-playlist-to-formatted-text: A desktop application that extracts YouTube playlist transcripts and enhances them using Google’s Gemini AI models. The output is a book in any language you want. 将Youtube列表通过AI转成电子书 ...

May 8, 2025 · 1 min · 125 words · tomyli

Windows相关软件或工具

背景 平常只使用MAC,但是架不Windows的占用率,有一些感兴趣的东西想要记录一下 Win11-23H2-22631.3155_办公版-装机版_【2024.02.28】 – TWM000

April 27, 2025 · 1 min · 5 words · tomyli

SpringMVC实践之集成Sentinel限流功能

背景 近期收到线上报警,发现个别新接口在同1s内被请求了接近上百次,对系统的稳定造成了一些影响,近期调研了一下通用的限流框架,打算将Sentinel的限流功能集成进新服务 Sentinel前置知识 Sentinel 限流主要涉及以下几方面 Resource 限流的资源,资源支持动态加载,核心类为: AbstractDataSource ,Sentinel 默认提供了市面常见的资源管理服务的实现,也可以自定义实现 Rule 对定义的 Resouce 应用的限流规则,基于QPS、响应时间与系统负载。Sentinel 是通过一系列的功能Slot来实现不同的统计/控制功能,对于限流功能对应的就是 FlowSlot FlowSlot 的功能实现需要依赖前面的 NodeSelectorSlot 和 ClusterBuilderSlot 等统计Slot,有了统计信息才可以针对配置进行限流操作,这里面借用一张官方的实现图 各Slot默认的顺序也可以在代码中找到 核心处理逻辑 核心逻辑是在执行调用chain的捕获Blockexcption然后执行后续操作 集成步骤 借助SpringMVC中提供的 HandlerInterceptor 拦截器功能 与 Sentinel 提供的SpringMVC框架集成类库可以方便快速的实现限流功能 引入Sentinel SpringMVC Adapter <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-spring-webmvc-adapter</artifactId> <version>1.8.8</version> </dependency> 配置SentinelWebInterceptor Sentinel SpringMVC Adapter中提供了 SentinelWebInterceptor 来实现限流功能,这个 Interceptor 支持一些自定义的行为,主要通过 SentinelWebMvcConfig 来实现 SentinelWebMvcConfig 常用配置说明 SentinelWebMvcConfig 支持以下配置能力 urlCleaner 自定义对请求url的处理,比如厂商此次需要通过请求的设备id维度进行限流,对于其它的请求参数都进行忽略,最终可定义效果为: /api/v1/test/ping?did=6308103f1026acf274bbcc1b10001291551xxX httpMethodSpecify 是否指定特定的Http请求方法,此设置是对资源的请求方法进行了细粒度的配置,默认为false不开启,配置为true后,会在设定好的资源名前加上请求的方法,类似效果: GET: /api/v1/test/ping?did=6308103f1026acf274bbcc1b10001291551xxX blockExceptionHandler 被Block的请求异常处理器,对Block的请求进行自定义处理,可以设置返回的值,如果未配置则需要服务自己捕获 BlockException,可以通过配置 ExceptionHandler 进行统一处理 originParser 请求来源处理器,针对请求的来源进行处理,在配置资源的规则的可以使用此解析的origin细粒度控制,例如可以解析ip、用户做为请求 origin ...

April 25, 2025 · 1 min · 131 words · tomyli

众乐乐-weekly 第15期

Read 📖 Cherry Studio 网络搜索与知识库最佳实践 奇客Solidot | YouTube 用户上传了 20 万亿则视频 20年,20万亿的视频,惊到了 如何避免在 AI 时代技能退化 | 宝玉的分享 把AI当做协作者,而不是拐杖,做了一个完整独立的人 Tools 🧰 the-via/app 改键神器,如果使用中老是卡在查找设备的页面,可以在设置中改成3D模式 Image 🖼️ Speech 💬 天不生人上之人,也不生人下之人 规则就像汽车上的安全带:一开始可能会有点不舒服,但一段时间后,使用规则就会成为习惯,不使用规则就变得不可想象。 Idea 💡

April 22, 2025 · 1 min · 30 words · tomyli