用java原生的注解替换spring的注解

whdahanh 发布于 2023-06-27 291 次阅读


1、引入pom依赖

        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
2、@Named("alarmService")可以替换@Service等
3、使用@Inject替换@Autowired或者使用@Resource注解
4、现在的用法:
@Named("WeatherService")
public class WeatherServiceImpl implements WeatherService {

    @Resource
    private LocationUtils locationUtils;

    @Inject
    public WeatherServiceImpl(LocationUtils locationUtils) {
        super();
        this.locationUtils = locationUtils;
    }

    @Override
    public Result queryWeather(HttpServletRequest request) {
        String requestIp = IpUtils.getRemoteIP(request);
        if (!StringUtils.checkNull(requestIp)) {
            if (!requestIp.equals("0:0:0:0:0:0:0:1") && !requestIp.equals("127.0.0.1") && !requestIp.equals("localhost")) {
                WeatherVo vo = locationUtils.getLocationByIp(requestIp);
                return Result.ok(vo);
            }
        }
        return Result.ok();
    }
}
5、以前的用法
@Server("alarmService")
public class AlarmServiceImpl implements AlarmService {
    @Autowired
    private AlarmRecordMapper alarmRecordMapper;


微信扫描下方的二维码阅读本文

此作者没有提供个人介绍
最后更新于 2023-06-27