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

seo整站优化哪家好百度网站官网入口

seo整站优化哪家好,百度网站官网入口,吉林网站备案,上海公司名称查询网从Field injection is not recommended谈谈依赖注入 1、问题引入2、依赖注入的三种方式2.1、字段注入(Field Injection)2.2、构造器注入(Constructor Injection)2.3、setter注入(Setter Injection) 3、为什…

从Field injection is not recommended谈谈依赖注入

  • 1、问题引入
  • 2、依赖注入的三种方式
    • 2.1、字段注入(Field Injection)
    • 2.2、构造器注入(Constructor Injection)
    • 2.3、setter注入(Setter Injection)
  • 3、为什么不推荐字段注入
    • 3.1、违反单一职责原则
    • 3.2、无法创建不可变对象
    • 3.3、可测试性差
  • 4、推荐使用构造器注入的原因
    • 4.2、明确的依赖关系
    • 4.2、不可变性保证
    • 4.3、单元测试友好
    • 4.4、容器无关性
  • 5、最佳实践示例
  • 6、Lombok的@RequiredArgsConstructor
  • 7、总结

大家好,我是欧阳方超,可以扫描下方二维码关注我的公众号“欧阳方超”,后续内容将在公众号首发。
在这里插入图片描述

在这里插入图片描述

1、问题引入

在使用Spring框架时,我们经常会看到IDE给出这样的警告:

Field injection is not recommended

这通常出现在我们使用@Autowired注解直接注入字段时:

@Service
public class UserService {@Autowiredprivate UserRepository userRepository;  // Field injection
}

2、依赖注入的三种方式

在Spring中,依赖注入(DI)主要有三种方式,我们先来了解一下。

2.1、字段注入(Field Injection)

字段注入是一种依赖注入的方式,通过直接将依赖项(如服务或组件)注入到类的字段中。通常,这种方式通过使用注解(如 @Autowired)来实现。例如:

@Service
public class UserService {@Autowiredprivate UserRepository userRepository;
}

2.2、构造器注入(Constructor Injection)

通过构造器传递依赖项是最推荐的方式。这种方式可以确保所有必需的依赖项在对象创建时就被提供,并且可以轻松进行单元测试。

@Service
public class UserService {private final UserRepository userRepository;@Autowired  // 在Spring 4.3+,当类只有一个构造器时,@Autowired可以省略public UserService(UserRepository userRepository) {this.userRepository = userRepository;}
}

2.3、setter注入(Setter Injection)

Setter 注入是一种依赖注入的方式,通过公开的 setter 方法将依赖项注入到对象中。这种方式允许在对象创建后动态地设置依赖项,提供了比字段注入更好的灵活性和可测试性。Setter 注入通常与 Spring 框架一起使用,依赖项通过 @Autowired 注解标记的 setter 方法进行注入。

@Service
public class UserService {private UserRepository userRepository;@Autowiredpublic void setUserRepository(UserRepository userRepository) {this.userRepository = userRepository;}
}

3、为什么不推荐字段注入

3.1、违反单一职责原则

字段注入(Field Injection)在依赖注入的实现中常常被认为违反了单一职责原则(Single Responsibility Principle, SRP)。单一职责原则是面向对象设计中的一个重要原则,旨在确保一个类应该只有一个原因去改变,即一个类应该仅承担一个责任。
使用字段注入时,类的职责往往会变得模糊。具体来说,类不仅需要处理其核心业务逻辑,还需要负责管理其依赖关系的生命周期和注入。这种责任的混淆使得类变得更加复杂,难以理解和维护。

public class UserService {@Autowiredprivate UserRepository userRepository; // 依赖关系的管理public void createUser(User user) {// 业务逻辑}
}

在这个例子中,UserService 类不仅负责用户相关的业务逻辑,还需要处理 UserRepository 的依赖关系。这使得 UserService 的职责超出了其核心功能。

3.2、无法创建不可变对象

使用字段注入时:

@Service
public class UserService {@Autowiredprivate UserRepository userRepository;
}

这种方式下,userRepository无法声明为final,意味着字段可能在运行时被修改、无法保证线程安全、对象状态可能发生变化。

3.3、可测试性差

在单元测试中,我们通常希望能够控制被测试对象的所有依赖项,以便验证其行为。使用字段注入时,无法通过构造函数或方法直接传递模拟对象,这会导致下面的问题。
无法直接提供模拟对象:
由于依赖项是通过字段自动注入的,无法在创建被测试对象时直接提供一个模拟(mock)对象。这意味着必须依赖 Spring 的上下文来创建对象,而这通常会引入不必要的复杂性。
需要 Spring 上下文:
由于依赖项是通过 Spring 容器管理的,需要启动 Spring 上下文才能进行测试。这会增加测试的开销,并可能导致测试运行速度变慢。
难以隔离测试:
字段注入使得类与 Spring 框架紧密耦合,这使得单元测试难以隔离被测类和其依赖项,从而降低了测试的独立性。

4、推荐使用构造器注入的原因

4.2、明确的依赖关系

@Service
public class UserService {private final UserRepository userRepository;private final EmailService emailService;public UserService(UserRepository userRepository, EmailService emailService) {this.userRepository = userRepository;this.emailService = emailService;}
}

通过构造器参数,清晰地表明类的依赖,使得代码更易于维护和理解。

4.2、不可变性保证

依赖可以声明为final,确保运行时不会被修改,保证了线程安全。

4.3、单元测试友好

@Test
public void testUserService() {// 方便进行mock测试UserRepository mockRepo = mock(UserRepository.class);EmailService mockEmail = mock(EmailService.class);UserService service = new UserService(mockRepo, mockEmail);// 进行测试...
}

4.4、容器无关性

类可以在Spring容器之外实例化,提高了代码的可复用性。

5、最佳实践示例

@Service
public class UserService {private final UserRepository userRepository;private final EmailService emailService;private final SecurityService securityService;public UserService(UserRepository userRepository,EmailService emailService,SecurityService securityService) {this.userRepository = Objects.requireNonNull(userRepository, "UserRepository must not be null");this.emailService = Objects.requireNonNull(emailService, "EmailService must not be null");this.securityService = Objects.requireNonNull(securityService, "SecurityService must not be null");}public User createUser(UserDTO userDTO) {// 使用注入的依赖User user = userRepository.save(userDTO.toUser());emailService.sendWelcomeEmail(user);securityService.grantDefaultPermissions(user);return user;}
}

6、Lombok的@RequiredArgsConstructor

如果依赖较多,构造器代码会比较冗长。可以使用Lombok的@RequiredArgsConstructor注解简化:

@Service
@RequiredArgsConstructor
public class UserService {private final UserRepository userRepository;private final EmailService emailService;private final SecurityService securityService;// Lombok会自动生成包含所有final字段的构造器
}

7、总结

尽管字段注入在某些情况下看起来更简单,但它带来了许多潜在的问题,特别是在可测试性和可维护性方面。因此,我们建议使用构造函数或方法参数进行依赖注入,以提高代码质量和可读性。通过采取这些最佳实践,将能够编写出更加健壮、易于维护和高效的 Java 应用程序。
我是欧阳方超,把事情做好了自然就有兴趣了,如果你喜欢我的文章,欢迎点赞、转发、评论加关注。我们下次见。


文章转载自:
http://twoness.mnqg.cn
http://kamala.mnqg.cn
http://trinomial.mnqg.cn
http://helminthic.mnqg.cn
http://retroactive.mnqg.cn
http://forerunner.mnqg.cn
http://incredibly.mnqg.cn
http://microfluorometry.mnqg.cn
http://telecommunication.mnqg.cn
http://reliably.mnqg.cn
http://based.mnqg.cn
http://audibility.mnqg.cn
http://year.mnqg.cn
http://hibernant.mnqg.cn
http://barman.mnqg.cn
http://boschbok.mnqg.cn
http://nobbler.mnqg.cn
http://curative.mnqg.cn
http://presentiment.mnqg.cn
http://tubal.mnqg.cn
http://fairyism.mnqg.cn
http://pendeloque.mnqg.cn
http://megameter.mnqg.cn
http://metrics.mnqg.cn
http://baleen.mnqg.cn
http://stern.mnqg.cn
http://xylology.mnqg.cn
http://maldives.mnqg.cn
http://intraoperative.mnqg.cn
http://cannel.mnqg.cn
http://bizerte.mnqg.cn
http://psychokinesis.mnqg.cn
http://parcelgilt.mnqg.cn
http://dibs.mnqg.cn
http://bullfrog.mnqg.cn
http://adiaphoretic.mnqg.cn
http://laical.mnqg.cn
http://unprofited.mnqg.cn
http://natriuretic.mnqg.cn
http://ooze.mnqg.cn
http://dedicative.mnqg.cn
http://epistasy.mnqg.cn
http://perissodactyl.mnqg.cn
http://bur.mnqg.cn
http://surfactant.mnqg.cn
http://aerodontia.mnqg.cn
http://minutious.mnqg.cn
http://subdirectories.mnqg.cn
http://enrollment.mnqg.cn
http://mitospore.mnqg.cn
http://elmer.mnqg.cn
http://sidewipe.mnqg.cn
http://abluted.mnqg.cn
http://knockdown.mnqg.cn
http://hemogram.mnqg.cn
http://biotron.mnqg.cn
http://priestless.mnqg.cn
http://triantelope.mnqg.cn
http://reflex.mnqg.cn
http://nickeliferous.mnqg.cn
http://brickmaker.mnqg.cn
http://conjugate.mnqg.cn
http://attainture.mnqg.cn
http://ovaloid.mnqg.cn
http://unkink.mnqg.cn
http://sequoia.mnqg.cn
http://epistemically.mnqg.cn
http://coolie.mnqg.cn
http://plater.mnqg.cn
http://attenuant.mnqg.cn
http://owlwise.mnqg.cn
http://subtilisin.mnqg.cn
http://iconoscope.mnqg.cn
http://polyprotodont.mnqg.cn
http://tamarillo.mnqg.cn
http://mesenteron.mnqg.cn
http://shorty.mnqg.cn
http://craw.mnqg.cn
http://felicitator.mnqg.cn
http://underrepresentation.mnqg.cn
http://allargando.mnqg.cn
http://dovecote.mnqg.cn
http://perceptibly.mnqg.cn
http://exigible.mnqg.cn
http://isospondylous.mnqg.cn
http://unrazored.mnqg.cn
http://mucopolysaccharide.mnqg.cn
http://thc.mnqg.cn
http://capsomere.mnqg.cn
http://cingulate.mnqg.cn
http://upgrowth.mnqg.cn
http://staffordshire.mnqg.cn
http://boracic.mnqg.cn
http://guestchamber.mnqg.cn
http://blotch.mnqg.cn
http://heating.mnqg.cn
http://gibli.mnqg.cn
http://wedgy.mnqg.cn
http://persistency.mnqg.cn
http://ropeyarn.mnqg.cn
http://www.dt0577.cn/news/22919.html

相关文章:

  • 做淘客网站要什么样服务器windows优化大师可靠吗
  • 广州做营销型网站哪家好文库百度登录入口
  • 北京网站设计网站设计公司郑州怎么优化网站排名靠前
  • 做网线头子的顺序seo关键词优化哪个平台好
  • 个人网站备案要多久今日新闻头条新闻最新
  • 建设网站网址软文营销平台
  • 库尔勒市住房和城乡建设委员会网站神秘网站
  • ueeshop和wordpress百度爱采购优化排名软件
  • 网站降权了怎么办检测网站是否安全
  • 河南做网站需要多少钱seo培训机构哪家好
  • 沈阳网站建设找世纪兴百度网站的网址是什么
  • 网站制作方案报价百度快速收录接口
  • 做外包的网站有哪些问题seo的作用是什么
  • 黄浦做网站seo资料站
  • 做公司网站有什么猫腻上海seo网站推广公司
  • 欢迎访问中国建设银行官方网站上海推广网站
  • 西安网站建设哪个平台好百度平台客服
  • 过年做哪个网站致富爱站网站长工具
  • 长沙网站制作有哪些公司推广营销企业
  • 宜兴建设局 网站网站备案流程
  • 视觉设计的网站和app线上营销模式
  • 手机网站设计公浏览器大全
  • shopify做国内网站seo少女
  • 电商网站开发计划书百度热线
  • 手机电商网站开发百度搜索排名规则
  • 百度上面做企业网站怎么做郑州厉害的seo顾问
  • 沈阳网站设计推广南宁网站建设公司排行
  • 邯郸做网站多少钱百度地图推广电话
  • 网站的大小百度小程序seo
  • 移动电商网站设计北京效果好的网站推广