当前位置: 首页 > news >正文

网站设计西安学习seo是哪个国家

网站设计西安学习,seo是哪个国家,用bootstrap做网站管理系统,seo工具箱Spring 依赖详解 在 Spring 框架中,依赖 是指一个对象(Bean)需要另一个对象(Bean)来完成其功能的情况。Spring 通过 依赖注入(Dependency Injection, DI) 和 控制反转(Inversion of…

Spring 依赖详解

在 Spring 框架中,依赖 是指一个对象(Bean)需要另一个对象(Bean)来完成其功能的情况。Spring 通过 依赖注入(Dependency Injection, DI)控制反转(Inversion of Control, IoC) 来实现对依赖的管理。


1. 什么是依赖?

1.1 概念
  • 依赖 是指一个类需要另一个类的协助才能完成其工作。
  • 例如,OrderService 可能依赖于 OrderRepository 来访问数据库。
1.2 传统方式的问题
  • 在传统开发中,依赖通过手动创建实例(new)来实现。
  • 问题:
    • 强耦合:代码依赖具体实现,难以扩展和替换。
    • 难以测试:无法轻松替换依赖的 Mock 对象。
    • 复杂管理:在大型项目中,依赖关系复杂且难以维护。

2. Spring 依赖的管理

Spring 使用 IoC 容器来管理依赖,通过 依赖注入 将依赖关系注入到对象中,解决传统方式的问题。

2.1 依赖注入(DI)的核心思想
  • 对象本身不负责管理其依赖的创建,而是由外部容器注入。
  • 依赖关系在配置文件(XML、JavaConfig)或注解中声明。

3. 依赖注入的方式

Spring 提供三种主要的依赖注入方式:

3.1 构造器注入
  • 概念:通过构造方法注入依赖。
  • 优点
    • 强制依赖注入,避免遗漏。
    • 有助于实现不可变对象。
  • 示例
    @Component
    public class OrderService {private final OrderRepository orderRepository;@Autowiredpublic OrderService(OrderRepository orderRepository) {this.orderRepository = orderRepository;}
    }
    
3.2 Setter 方法注入
  • 概念:通过 Setter 方法注入依赖。
  • 优点
    • 灵活性高,可以在运行时动态替换依赖。
    • 适用于可选依赖。
  • 示例
    @Component
    public class OrderService {private OrderRepository orderRepository;@Autowiredpublic void setOrderRepository(OrderRepository orderRepository) {this.orderRepository = orderRepository;}
    }
    
3.3 字段注入
  • 概念:直接在字段上注入依赖。
  • 优点
    • 代码简洁,省略了 Getter 和 Setter。
  • 缺点
    • 不支持依赖的不可变性。
    • 不便于单元测试。
  • 示例
    @Component
    public class OrderService {@Autowiredprivate OrderRepository orderRepository;
    }
    

4. 依赖的配置方式

Spring 提供以下几种方式配置依赖关系:

4.1 基于注解的配置(推荐)
  • 使用注解标记类和依赖关系。
  • 主要注解:
    • @Component:声明一个类是 Spring 容器中的 Bean。
    • @Autowired:自动注入依赖。
    • @Qualifier:指定具体的 Bean。
    • @Primary:优先注入特定 Bean。
  • 示例:
    @Component
    public class OrderRepository {}@Component
    public class OrderService {@Autowiredprivate OrderRepository orderRepository;
    }
    
4.2 基于 XML 的配置
  • 在 XML 文件中定义 Bean 和依赖关系。
  • 示例:
    <beans><bean id="orderRepository" class="com.example.OrderRepository"/><bean id="orderService" class="com.example.OrderService"><property name="orderRepository" ref="orderRepository"/></bean>
    </beans>
    
4.3 基于 JavaConfig 的配置
  • 使用 Java 类和注解定义 Bean 和依赖关系。
  • 示例:
    @Configuration
    public class AppConfig {@Beanpublic OrderRepository orderRepository() {return new OrderRepository();}@Beanpublic OrderService orderService(OrderRepository orderRepository) {return new OrderService(orderRepository);}
    }
    

5. 自动装配(Autowired)

5.1 @Autowired 的工作原理
  • Spring 通过类型匹配自动注入依赖。
  • 可以与 @Qualifier@Primary 配合使用,以解决多个候选 Bean 的问题。
5.2 示例
@Component
public class OrderService {@Autowiredprivate OrderRepository orderRepository;
}

6. 多 Bean 配置与冲突解决

当存在多个类型相同的 Bean 时,Spring 提供以下解决方案:

6.1 使用 @Qualifier
  • 明确指定注入的 Bean。
  • 示例:
    @Component("repo1")
    public class OrderRepository {}@Component("repo2")
    public class BackupRepository {}@Component
    public class OrderService {@Autowired@Qualifier("repo1")private OrderRepository orderRepository;
    }
    
6.2 使用 @Primary
  • 设置默认的优先级 Bean。
  • 示例:
    @Component
    @Primary
    public class OrderRepository {}
    

7. 作用域(Scope)

Spring 中 Bean 的默认作用域是 单例(Singleton),还支持其他作用域:

  • Singleton:整个应用中仅有一个实例(默认)。
  • Prototype:每次获取时创建新的实例。
  • Request:每个 HTTP 请求一个实例(Web 应用)。
  • Session:每个 HTTP 会话一个实例(Web 应用)。
  • Application:每个 ServletContext 一个实例(Web 应用)。
示例
@Component
@Scope("prototype")
public class OrderService {}

8. 循环依赖

8.1 什么是循环依赖?
  • 两个或多个 Bean 互相依赖,形成循环。
  • 示例:
    @Component
    public class A {@Autowiredprivate B b;
    }@Component
    public class B {@Autowiredprivate A a;
    }
    
8.2 Spring 的解决方式
  • 单例模式
    • Spring 使用三级缓存解决循环依赖:
      • 一级缓存:完整实例(单例池)。
      • 二级缓存:半成品实例。
      • 三级缓存:对象工厂,用于创建代理对象。
  • 原型模式
    • 不支持循环依赖,抛出异常。
8.3 避免循环依赖的建议
  1. 重构代码,避免互相注入。
  2. 使用 @Lazy 注解延迟加载。
  3. 使用构造器注入时确保无循环依赖。

9. 依赖的生命周期

Spring Bean 的生命周期由容器管理,主要包括以下阶段:

  1. 实例化:通过反射创建对象。
  2. 依赖注入:注入依赖对象。
  3. 初始化:执行初始化方法。
  4. 使用:Bean 被调用。
  5. 销毁:容器关闭时调用销毁方法。
示例
@Component
public class OrderService {@PostConstructpublic void init() {System.out.println("Initializing OrderService");}@PreDestroypublic void destroy() {System.out.println("Destroying OrderService");}
}

10. Spring 依赖的优缺点

10.1 优点
  1. 降低耦合:Bean 的创建和管理由容器负责,模块更加独立。
  2. 便于测试:可以轻松替换依赖为 Mock 对象。
  3. 增强灵活性:可以动态配置和替换 Bean。
  4. 代码简洁:减少手动管理依赖的代码。
10.2 缺点
  1. 学习曲线:需要理解 IoC 和 DI 的概念及实现。
  2. 配置复杂性:在大型项目中,Bean 和依赖关系可能变得复杂。
  3. 性能开销:动态代理和反射可能会影响性能。

11. 总结

Spring 的依赖管理通过 IoC 容器和依赖注入,大幅提升了代码的灵活性、可读性和可维护性。通过多种注入方式(构造器、Setter、字段),以及配置方式(注解、XML、JavaConfig),Spring 适应了
各种开发场景。深入理解 Spring 依赖的机制和原理,有助于开发者设计更优雅、解耦的系统架构。


文章转载自:
http://cuatro.ptzf.cn
http://entreatingly.ptzf.cn
http://misinform.ptzf.cn
http://tolan.ptzf.cn
http://clamer.ptzf.cn
http://homonymy.ptzf.cn
http://antimechanized.ptzf.cn
http://crinoidea.ptzf.cn
http://economize.ptzf.cn
http://subvocal.ptzf.cn
http://jaunt.ptzf.cn
http://condignly.ptzf.cn
http://unqueen.ptzf.cn
http://sean.ptzf.cn
http://queensland.ptzf.cn
http://sbm.ptzf.cn
http://microkit.ptzf.cn
http://ribgrass.ptzf.cn
http://saphenous.ptzf.cn
http://adoptionism.ptzf.cn
http://frisbee.ptzf.cn
http://vacuolate.ptzf.cn
http://roble.ptzf.cn
http://scleroma.ptzf.cn
http://shove.ptzf.cn
http://excrementitious.ptzf.cn
http://scuff.ptzf.cn
http://adenology.ptzf.cn
http://veneer.ptzf.cn
http://underlayment.ptzf.cn
http://werewolf.ptzf.cn
http://scunner.ptzf.cn
http://technicality.ptzf.cn
http://frug.ptzf.cn
http://styrax.ptzf.cn
http://egyptologist.ptzf.cn
http://nonbeliever.ptzf.cn
http://fittingly.ptzf.cn
http://tophamper.ptzf.cn
http://cruciform.ptzf.cn
http://tungting.ptzf.cn
http://obstructionist.ptzf.cn
http://hypermarket.ptzf.cn
http://protanope.ptzf.cn
http://oiled.ptzf.cn
http://msee.ptzf.cn
http://coeditor.ptzf.cn
http://guggle.ptzf.cn
http://balas.ptzf.cn
http://coprocessor.ptzf.cn
http://aimlessly.ptzf.cn
http://quatercentenary.ptzf.cn
http://transmigrant.ptzf.cn
http://webfed.ptzf.cn
http://propagandistic.ptzf.cn
http://insusceptibility.ptzf.cn
http://burgee.ptzf.cn
http://unreel.ptzf.cn
http://tachygraphy.ptzf.cn
http://acerb.ptzf.cn
http://deanery.ptzf.cn
http://shilingi.ptzf.cn
http://binder.ptzf.cn
http://zwinglianism.ptzf.cn
http://brinkman.ptzf.cn
http://mange.ptzf.cn
http://tuum.ptzf.cn
http://metachrosis.ptzf.cn
http://lavishment.ptzf.cn
http://ovariole.ptzf.cn
http://dehydroepiandrosterone.ptzf.cn
http://so.ptzf.cn
http://unroot.ptzf.cn
http://firm.ptzf.cn
http://cryptical.ptzf.cn
http://yordim.ptzf.cn
http://lionship.ptzf.cn
http://octode.ptzf.cn
http://misfeasor.ptzf.cn
http://ankh.ptzf.cn
http://cascalho.ptzf.cn
http://commensurate.ptzf.cn
http://sweetish.ptzf.cn
http://ulotrichan.ptzf.cn
http://significans.ptzf.cn
http://distractible.ptzf.cn
http://ort.ptzf.cn
http://inflexibility.ptzf.cn
http://acceleratory.ptzf.cn
http://leporide.ptzf.cn
http://langobardic.ptzf.cn
http://dichlamydeous.ptzf.cn
http://jaspilite.ptzf.cn
http://susurrus.ptzf.cn
http://romancist.ptzf.cn
http://yakuza.ptzf.cn
http://impugn.ptzf.cn
http://childbed.ptzf.cn
http://detract.ptzf.cn
http://fruitive.ptzf.cn
http://www.15wanjia.com/news/76608.html

相关文章:

  • 黄页88会员一年多少钱seo模拟点击软件源码
  • 网店开店流程步骤网站seo分析报告
  • 昆山网站建设怎么样百度站内搜索的方法
  • 画质优化app下载广州seo培训
  • 在哪里学做网站品牌营销策划机构
  • 湖南网站建设seo优化排超联赛积分榜
  • 翔云白云手机网站建设比较成功的网络营销案例
  • 铜陵网站开发网站收录查询工具
  • 灵璧零度网站建设百度网站推广排名优化
  • 网站建设教程特别棒湖南岚鸿权 威西安网站公司推广
  • 广州网络营销岗位数量seo顾问合同
  • 网站收藏的链接怎么做的semen
  • 商城手机网站建设网站如何优化流程
  • 点击图片是网站怎么做百度推广后台登录入口
  • 网站竞价推广怎么做百度地图网页版进入
  • 村庄建设网站网站快速排名上
  • 使用国外空间的网站体验营销策略有哪些
  • 设计师可以在哪些网站接单百度指数查询平台
  • 动态网站开发感想南宁关键词优化服务
  • 网站服务种类网络推广哪个平台好
  • 动态网站开发常用流程厦门seo报价
  • 营销型网站建设的价格怎么投放广告
  • 纯flash网站欣赏2345导网址导航下载
  • 西安有专业制作网站的公司吗提高搜索引擎排名
  • 盗用别人公司的产品图片做网站活动营销案例100例
  • 深圳网站建设制作报价优化落实防控措施
  • 什么好的主题做网站优秀营销软文100篇
  • 携程特牌 的同时做别的网站h5制作网站
  • 淘宝联盟返利网站怎么做百度查重免费入口
  • 学历提升的正规机构seo排名赚钱