网站大全网站免费招聘网站建设费用多少

张小明 2026/1/11 9:58:53
网站大全网站免费,招聘网站建设费用多少,如何自己做推广,少儿编程十大培训机构在Spring Boot中#xff0c;依赖注入是一项核心特性#xff0c;它有助于创建松散耦合的应用程序。 1. 构造函数注入 构造函数注入通过类的构造函数来传递依赖。这确保了在对象创建时#xff0c;依赖就已经准备好#xff0c;并且不可变。如果一个类的依赖在其整个生命周期内…在Spring Boot中依赖注入是一项核心特性它有助于创建松散耦合的应用程序。1. 构造函数注入构造函数注入通过类的构造函数来传递依赖。这确保了在对象创建时依赖就已经准备好并且不可变。如果一个类的依赖在其整个生命周期内都不会改变构造函数注入是一个很好的选择。它还能帮助确保依赖不为空因为构造函数参数通常是必需的。示例代码假设我们有一个UserService依赖于UserRepository。首先定义UserRepository接口和实现类importorg.springframework.stereotype.Repository;RepositorypublicclassUserRepository{publicvoidsaveUser(Stringuser){System.out.println(Saving user: user);}}然后定义UserService通过构造函数注入UserRepositoryimportorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;ServicepublicclassUserService{privatefinalUserRepositoryuserRepository;AutowiredpublicUserService(UserRepositoryuserRepository){this.userRepositoryuserRepository;}publicvoidregisterUser(Stringuser){userRepository.saveUser(user);}}在Spring Boot中Autowired注解并非必需如果构造函数只有一个Spring会自动进行依赖注入。上述代码可以简化为importorg.springframework.stereotype.Service;ServicepublicclassUserService{privatefinalUserRepositoryuserRepository;publicUserService(UserRepositoryuserRepository){this.userRepositoryuserRepository;}publicvoidregisterUser(Stringuser){userRepository.saveUser(user);}}2. Setter方法注入Setter方法注入通过调用Setter方法来设置依赖。这种方式更加灵活因为可以在对象创建后再设置依赖。适用于依赖在对象创建时可能不可用或者依赖可能在对象的生命周期内发生变化的情况。示例代码同样基于前面的UserRepository定义使用Setter注入的UserServiceimportorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;ServicepublicclassUserService{privateUserRepositoryuserRepository;AutowiredpublicvoidsetUserRepository(UserRepositoryuserRepository){this.userRepositoryuserRepository;}publicvoidregisterUser(Stringuser){userRepository.saveUser(user);}}3. 字段注入属性注入字段注入直接在类的字段上使用注解来注入依赖。这种方式代码简洁但不利于单元测试因为难以在测试中替换依赖。示例代码importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;ServicepublicclassUserService{AutowiredprivateUserRepositoryuserRepository;publicvoidregisterUser(Stringuser){userRepository.saveUser(user);}}4. 基于Java配置类的依赖注入在Spring Boot中除了使用组件扫描和自动装配还可以通过Java配置类来手动配置Bean及其依赖关系。这种方式在需要更精细控制Bean的创建和配置时非常有用。示例代码首先创建一个Java配置类AppConfigimportorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;ConfigurationpublicclassAppConfig{BeanpublicUserRepositoryuserRepository(){returnnewUserRepository();}BeanpublicUserServiceuserService(UserRepositoryuserRepository){returnnewUserService(userRepository);}}然后可以在其他组件中使用UserServiceSpring会根据配置类来注入依赖。5. 基于注解驱动的条件注入有时候我们可能希望根据某些条件来决定是否注入某个依赖。Spring Boot提供了基于注解的条件注入方式如Conditional注解及其变体。示例代码假设我们有一个DatabaseConfig类根据系统属性来决定是否创建DataSourceimportorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Conditional;importorg.springframework.context.annotation.Configuration;importjavax.sql.DataSource;importorg.apache.tomcat.jdbc.pool.DataSourceasTomcatDataSource;ConfigurationpublicclassDatabaseConfig{Value(${use.in.memory.database:false})privatebooleanuseInMemoryDatabase;BeanConditional(InMemoryDatabaseCondition.class)publicDataSourceinMemoryDataSource(){TomcatDataSourcedataSourcenewTomcatDataSource();dataSource.setUrl(jdbc:h2:mem:testdb);dataSource.setDriverClassName(org.h2.Driver);dataSource.setUsername(sa);dataSource.setPassword(password);returndataSource;}BeanConditional(ProductionDatabaseCondition.class)publicDataSourceproductionDataSource(){TomcatDataSourcedataSourcenewTomcatDataSource();dataSource.setUrl(jdbc:mysql://localhost:3306/productiondb);dataSource.setDriverClassName(com.mysql.cj.jdbc.Driver);dataSource.setUsername(root);dataSource.setPassword(password);returndataSource;}}这里定义了两个DataSource的BeaninMemoryDataSource和productionDataSource分别基于不同的条件进行创建。Conditional注解的参数是一个实现了Condition接口的类通过实现matches方法来定义条件逻辑。例如importorg.springframework.context.annotation.Condition;importorg.springframework.context.annotation.ConditionContext;importorg.springframework.core.type.AnnotatedTypeMetadata;publicclassInMemoryDatabaseConditionimplementsCondition{Overridepublicbooleanmatches(ConditionContextcontext,AnnotatedTypeMetadatametadata){returncontext.getEnvironment().getProperty(use.in.memory.database,Boolean.class,false);}}importorg.springframework.context.annotation.Condition;importorg.springframework.context.annotation.ConditionContext;importorg.springframework.core.type.AnnotatedTypeMetadata;publicclassProductionDatabaseConditionimplementsCondition{Overridepublicbooleanmatches(ConditionContextcontext,AnnotatedTypeMetadatametadata){return!context.getEnvironment().getProperty(use.in.memory.database,Boolean.class,false);}}
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

网站建站北京论文中引用网站中

FaceFusion如何解决头发边缘融合不自然的问题? 在数字人像合成的世界里,一个微小的细节往往决定了“真实”与“虚假”的边界。比如一缕飘动的发丝——当它从源人物的脸被替换到目标视频中时,如果处理不当,就会出现模糊、黑边、色差…

张小明 2026/1/6 0:04:31 网站建设

湖南做网站 多少钱磐石网络北京市网络推广竞价

掌握Blender glTF 2.0:从入门到精通的完整指南 【免费下载链接】glTF-Blender-IO Blender glTF 2.0 importer and exporter 项目地址: https://gitcode.com/gh_mirrors/gl/glTF-Blender-IO 作为3D内容创作和传输的重要桥梁,glTF 2.0格式在Web和移…

张小明 2026/1/6 5:45:39 网站建设

山东省建设监理协会网站打不开网站建设服务 行业代码

PyTorch DataLoader 多进程数据加载性能调优实战 在现代深度学习训练中,一个常被忽视却至关重要的环节是:数据供给是否能跟上 GPU 的计算速度。我们常常看到这样的场景——高端显卡如 A100 或 H100 的利用率长期徘徊在 20%~40%,而 CPU 却满载…

张小明 2026/1/5 10:04:41 网站建设

响应式一页网站文山住房和城乡建设局网站

1.文件运行 导入工程 双击运行桌面GraniStudio.exe。 通过引导界面导入相机标定例程,点击导入按钮。 打开相机标定例程所在路径,选中相机标定.gsp文件,点击打开,完成导入。 2.功能说明 实现相机到标定位置进行标定。 注意:每次…

张小明 2026/1/10 13:21:21 网站建设

网站ico图标怎么用小程序搭建需要多久

打造个性化AI门户:LobeChat主题与UI定制技巧分享 在企业纷纷拥抱大模型的今天,一个常被忽视的问题浮出水面:为什么我们有了强大的AI能力,用户却依然觉得“不好用”? 答案往往藏在前端——再聪明的模型,如…

张小明 2026/1/6 22:40:23 网站建设

淄博网站排名公司滨州网站建设滨州

快速体验 打开 InsCode(快马)平台 https://www.inscode.net输入框内输入如下内容: 开发一个企业级项目依赖分析器,专门解决Cannot find declaration问题。功能包括:1)可视化展示项目依赖图 2)高亮显示断开的引用链 3)自动检测循环依赖 4)提供…

张小明 2026/1/8 2:13:08 网站建设