new对象如何使用 @mapper或者 @service

需求

定时任务中注册task
在这里插入图片描述
这里如果直接使用
@Autowired
DynamicPrintTask task;
这个task的话 因为设置的是单例的 所以 不管你加多少 task 他永远只会以最后一个task 来执行所有的任务
所以这里需要用 new的形式来创建 task
在这里插入图片描述
但是这个task 里面 用到了 很多 mapper 如果直接 用 new 的话 这些mapper 则会报 null
所以我们需要通过 ApplicationContext 来获取上下文

解决方案

public class SpringContextUtil {
    private static ApplicationContext applicationContext;

    //获取上下文
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //设置上下文
    public static void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextUtil.applicationContext = applicationContext;
    }

    //通过名字获取上下文中的bean
    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }

    //通过类型获取上下文中的bean
    public static Object getBean(Class<?> requiredType){
        return applicationContext.getBean(requiredType);
    }
}
public class WebApplication {
	public static void main(String[] args) {
		ApplicationContext context = SpringApplication.run(WebApplication.class, args);
		SpringContextUtil.setApplicationContext(context);

	}

}

错误使用springbean

//    @Autowired
//    SpringScheduledCronMapper springScheduledCronRepository;
//
//    @Autowired
//    ReportModulMapper reportModulMapper;
//
//    @Autowired
//    TemplateAssemblyMapper templateAssemblyMapper;
//
//    @Autowired
//    VulnerabilityDataAnalysisService vulnerabilityDataAnalysisService;
//
//    @Autowired
//    ReportAssemblyMapper reportAssemblyMapper;
//
//    @Autowired
//    SafetyIncidentHandlingService safetyIncidentHandlingService;
//
//    @Autowired
//    ReportRecordMapper reportRecordMapper;
//

正确使用

  ReportModulMapper reportModulMapper = (ReportModulMapper) SpringContextUtil.getBean(ReportModulMapper.class);
        TemplateAssemblyMapper templateAssemblyMapper = (TemplateAssemblyMapper)SpringContextUtil.getBean(TemplateAssemblyMapper.class);
        VulnerabilityDataAnalysisService vulnerabilityDataAnalysisService = (VulnerabilityDataAnalysisService)SpringContextUtil.getBean(VulnerabilityDataAnalysisService.class);
        ReportAssemblyMapper reportAssemblyMapper= (ReportAssemblyMapper)SpringContextUtil.getBean(ReportAssemblyMapper.class);
        SafetyIncidentHandlingService safetyIncidentHandlingService = (SafetyIncidentHandlingService)SpringContextUtil.getBean(SafetyIncidentHandlingService.class);
        ReportRecordMapper reportRecordMapper =(ReportRecordMapper)SpringContextUtil.getBean(ReportRecordMapper.class);

原因

@Autowired 是获取springioc容器里面的对象 而 通过 new 的形式 是 不能 获取到的 所以 需要通过ApplicationContext 获取上下午 获取到 bean对象

好的,我可以为你提供一个简单的登录案例,使用Spring Boot框架和相关注解来实现。 首先,我们需要创建一个名为"UserController"的控制器类。该类将处理用户登录请求,并返回相应的响应。 ```java @Controller public class UserController { @Autowired private UserService userService; @PostMapping("/login") @ResponseBody public ResponseEntity<String> login(@RequestBody UserDto userDto) { User user = userService.login(userDto.getUsername(), userDto.getPassword()); if (user != null) { return new ResponseEntity<>("Login Successful", HttpStatus.OK); } else { return new ResponseEntity<>("Login Failed", HttpStatus.UNAUTHORIZED); } } } ``` 在上面的代码中,我们使用了@Controller注解来标记该类为控制器类,@Autowired来注入UserService实例,@PostMapping注解用于处理HTTP POST请求,@ResponseBody注解用于将响应体直接返回给客户端,而无需使用模型和视图。@RequestBody注解用于将请求体转换为UserDto对象,然后我们使用UserService实例进行用户登录验证,如果用户存在,则返回成功响应,否则返回失败响应。 接下来,我们需要创建一个UserService类,该类将处理用户登录逻辑。 ```java @Service public class UserService { @Autowired private UserRepository userRepository; public User login(String username, String password) { User user = userRepository.findByUsername(username); if (user != null && user.getPassword().equals(password)) { return user; } else { return null; } } } ``` 在上面的代码中,我们使用了@Service注解来标记该类为服务类,@Autowired用于注入UserRepository实例,该类将处理用户登录逻辑。我们首先通过调用UserRepository的findByUsername方法来获取用户实例,然后比较用户密码是否与传递的密码相同。如果是,则返回用户实例,否则返回null。 最后,我们需要创建一个名为"UserDto"的数据传输对象类,该类将用于从请求体中提取用户名和密码。 ```java @Data public class UserDto { private String username; private String password; } ``` 在上面的代码中,我们使用了@Data注解来生成getter和setter方法,以及equals、hashCode和toString方法,这些方法将用于从请求体中提取用户名和密码。 最后,我们需要创建一个名为"UserRepository"的接口,该接口将扩展JpaRepository接口,并提供自定义方法来查询用户实例。 ```java @Repository public interface UserRepository extends JpaRepository<User, Long> { User findByUsername(String username); } ``` 在上面的代码中,我们使用了@Repository注解来标记该接口为存储库接口,该接口将扩展JpaRepository接口,并提供自定义方法findByUsername来查询用户实例。 以上就是使用Spring Boot框架和相关注解实现登录案例的全部内容。希望可以对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值