网站简约式布局特点,dw做链接网站无法显示该页面,深圳外贸网站,自己的身份已经网站备案了在 Spring 框架中#xff0c;推荐使用构造函数注入而非Autowired注解进行字段注入#xff0c;主要有以下几个原因#xff1a;
1. 依赖不可变和空指针安全 构造函数注入#xff1a;使用构造函数注入时#xff0c;依赖在对象创建时就必须提供#xff0c;一旦对象创建完成推荐使用构造函数注入而非Autowired注解进行字段注入主要有以下几个原因
1. 依赖不可变和空指针安全 构造函数注入使用构造函数注入时依赖在对象创建时就必须提供一旦对象创建完成其依赖关系就固定下来不会再改变。这有助于确保对象的状态在整个生命周期中都是一致和可预测的。而且由于依赖是在构造函数中注入的在使用对象之前依赖已经被初始化不会出现空指针异常。 举例假设有一个UserService类它依赖于UserRepository。使用构造函数注入的方式如下
public class UserService {private final UserRepository userRepository;public UserService(UserRepository userRepository) {this.userRepository userRepository;}// 业务方法public User findUserById(Long id) {return userRepository.findById(id);}
}
在这个例子中UserService在创建时就必须传入UserRepository实例之后在调用findUserById方法时userRepository肯定已经被初始化不会出现空指针问题。 字段注入使用Autowired注解进行字段注入时依赖是在对象创建之后通过反射机制注入的。这就意味着在对象创建和依赖注入之间存在一个短暂的时间窗口在这个窗口内依赖可能为空。如果在依赖注入完成之前就访问依赖就会导致空指针异常。 举例同样是UserService类使用字段注入的方式如下
public class UserService {Autowiredprivate UserRepository userRepository;// 业务方法public User findUserById(Long id) {return userRepository.findById(id);}
}
在这个例子中如果UserService在构造函数或者其他方法中过早地访问userRepository而此时依赖注入还未完成就可能会抛出空指针异常。
2. 便于单元测试 构造函数注入构造函数注入使得单元测试更加简单和直观。在进行单元测试时只需要创建一个模拟的依赖对象并通过构造函数传递给被测试对象即可。这使得测试代码与生产代码之间的依赖关系更加清晰也更容易控制测试环境。 举例对于上述使用构造函数注入的UserService单元测试可以这样写
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;public class UserServiceTest {Testpublic void testFindUserById() {// 创建模拟的UserRepositoryUserRepository mockRepository mock(UserRepository.class);when(mockRepository.findById(1L)).thenReturn(new User());// 通过构造函数创建UserService实例UserService userService new UserService(mockRepository);// 调用方法进行测试User user userService.findUserById(1L);assertNotNull(user);}
}
字段注入使用Autowired注解进行字段注入时在单元测试中需要使用反射或者其他复杂的技术来模拟依赖注入。这使得测试代码变得更加复杂也增加了测试的难度和维护成本。 举例对于使用字段注入的UserService单元测试可能需要这样写
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;SpringBootTest
ContextConfiguration
public class UserServiceTest {Autowiredprivate ApplicationContext applicationContext;Testpublic void testFindUserById() {// 创建模拟的UserRepositoryUserRepository mockRepository mock(UserRepository.class);when(mockRepository.findById(1L)).thenReturn(new User());// 通过反射设置UserService的userRepository字段UserService userService applicationContext.getBean(UserService.class);try {java.lang.reflect.Field field UserService.class.getDeclaredField(userRepository);field.setAccessible(true);field.set(userService, mockRepository);} catch (NoSuchFieldException | IllegalAccessException e) {e.printStackTrace();}// 调用方法进行测试User user userService.findUserById(1L);assertNotNull(user);}
}
可以看到字段注入的测试代码更加复杂需要使用反射来设置依赖增加了测试的难度和维护成本。
3. 更好的代码可读性和可维护性 构造函数注入构造函数注入将对象的依赖关系明确地展示在构造函数的参数列表中。这使得代码的阅读者能够一眼看出该对象依赖于哪些其他对象以及这些依赖是如何被初始化的。这种清晰的依赖关系有助于提高代码的可读性和可维护性。 举例对于前面的UserService类通过构造函数注入我们可以清楚地看到UserService依赖于UserRepository并且依赖是在构造函数中进行初始化的。
字段注入使用Autowired注解进行字段注入时依赖关系被隐藏在字段声明中没有在构造函数或者其他明显的位置展示。这使得代码的阅读者需要花费更多的时间和精力来理解对象的依赖关系和初始化过程。 举例在使用字段注入的UserService类中依赖关系通过Autowired注解隐藏在字段声明中不直观。