Spring众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的Java EE 企业应用开源框架。
为什么要使用Spring?
1.方便解耦,在早期的j2EE中,各个对象之间 是相互依赖,相互耦合的,而spring的思想(控制反转)将 对象 交由Spring容器进行管理,极大的降低了对象间的直接关联,从一定程度上使得对象之间的关系变得更加容易维护。
2.引入了Spring Aop的支持,但spring的Aop相较于AspectJ更加简陋,而aspectJ提供了更加完整的Aop技术,包括编译时织入,运行时织入等……但spring的让aop的使用难度降低,AspectJ原生的方式 使用Aop比较麻烦,需要接触三方的脚本。
3.引入了声明式的事务管理,在原生的jdbc事务中,需要手动控制commit,rollback的时机与流程,而spring只需要声明式的加入事务管理,使用注解@Transactional,将事务管理交由spring的事务管理器,极大的简化了事务管理的难度。、
4.方便测试等……
IOC/DI 思想,作为spring的核心思想之一,IOC和DI本质上 是同一种思想,不同的实现,分别代表控制反转,即将对象间的关联管理,交由spring进行管理,通过ApplicationContext spring上下文进行注入获取。DI,即依赖注入,我的理解是对象之间的依赖关系,通过spring进行匹配注入,不再需要显式的"new"对象,从而降低对象之间 依赖关系难以管理的问题。
具体的落地实施。
案例1:
mybatis mapper
public class UserDaoImpl implements IUserDao {
/**
* Describe:用户注册
*
* @param user:
*/
@Override
public void registerUser(User user) {
try {
/需要显式的获取到mapper对象,不方便!!!
UserMapper mapper = MapperHelper.getMapper(UserMapper.class);
User userOne = mapper.findUserOneNotDeletedByUserName(user);
if (!NotNullUtil.isNull(userOne)) {
throw new UserNameExistException("该用户名已经被使用!!");
}
String password = user.getPassword();
String passSalt = ShiroEncryptUtils.createSalt();
user.setPassSalt(passSalt);
String encryptPassword = ShiroEncryptUtils.createCredentials(password, passSalt);
user.setPassword(encryptPassword).setIsDeleted(0)
.setNickName(NICK_NAME_PRE + user.getUserName())
.setModifiedUser(user.getUserName())
.setModifiedTime(LocalDateTime.now())
.setGender(2)
.setCreateUser(user.getUserName())
.setCreateTime(LocalDateTime.now());
mapper.insertUserOne(user);
UserDetailMapper userDetailMapper = MapperHelper.getMapper(UserDetailMapper.class);
UserDetail userDetail = new UserDetail();
userDetail.setUId(user.getId())
.setIsAdmin(0)
.setIntegralTotal(0L)
.setCheckInTimes(0L).setExp(0L)
.setCreateUser(user.getUserName())
.setCreateTime(LocalDateTime.now())
.setModifiedTime(LocalDateTime.now())
.setModifiedUser(user.getUserName())
.setModifiedUser(user.getUserName())
.setIsDeleted(0);
userDetailMapper.insertUserDetailWhenRegister(userDetail);
} catch (IOException e) {
SqlSessionUtils.rollback();
e.printStackTrace();
} finally {
MapperHelper.mapperCommit();
}
}
使用spring
@Service
public class UserServiceImpl implements IUserService {
private final UserMapper userMapper;
private final UserDetailMapper userDetailMapper;
// 通过spring注入的方式
@Autowired
public UserServiceImpl(UserMapper userMapper, UserDetailMapper userDetailMapper) {
this.userMapper = userMapper;
this.userDetailMapper = userDetailMapper;
}
/**
* Describe:用户注册
*
* @param userVo:
*
*/
@Override
public void register(UserVO userVo) {
User user = new User();
BeanUtils.copyProperties(userVo,user);
User userOne = userMapper.findUserOneNotDeletedByUserName(user);
if (!NotNullUtil.isNull(userOne)) {
throw new UserNameExistException("该用户名已经被使用!!");
}
String password = user.getPassword();
String passSalt = ShiroEncryptUtils.createSalt();
user.setPassSalt(passSalt);
String encryptPassword = ShiroEncryptUtils.createCredentials(password, passSalt);
user.setPassword(encryptPassword).setIsDeleted(0)
.setNickName(NICK_NAME_PRE + user.getUserName())
.setModifiedUser(user.getUserName())
.setModifiedTime(LocalDateTime.now())
.setGender(2)
.setCreateUser(user.getUserName())
.setCreateTime(LocalDateTime.now());
userMapper.insertUserOne(user);
UserDetail userDetail = new UserDetail();
userDetail.setUId(user.getId())
.setIsAdmin(0)
.setIntegralTotal(0L)
.setCheckInTimes(0L).setExp(0L)
.setCreateUser(user.getUserName())
.setCreateTime(LocalDateTime.now())
.setModifiedTime(LocalDateTime.now())
.setModifiedUser(user.getUserName())
.setModifiedUser(user.getUserName())
.setIsDeleted(0);
userDetailMapper.insertUserDetailWhenRegister(userDetail);
}
2.场景2:方便各种框架的整合
/**
* Describe:
*
* @author OTM-CITRUS
* @date 12/13/2022 10:14 AM
*/
@Configuration
public class SwaggerConfiguration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.citrus.onlineorder.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
Contact contact=new Contact("OTM—CITRUS",
"https://github.com/suzukitakumisama","vchenyuyu@126.com");
return new ApiInfoBuilder()
.title("OTM 酒店订单服务")
.description("订单服务接口")
.contact(contact)
.version("1.0")
.build();
}
}