Springboot2.X某个第三方类,想引入spring管理的bean对象,如何解决

环境:spring boot2.X + netty
在spring boot2.X架构中,在@Controller或者@Service注解下的类,可以通过依赖注入@Autowired,引入被@Service、@Component、@Configuration注解等被spring管理的bean对象,那么想在netty的主要功能Handler中引入操作数据库
的Dao层或者逻辑功能的Service层,怎么注入呢?

通过Spring的ApplicationContext进行spring上下文的管理,获取被spring管理的bean对象。
通过SpringUtils类进行获取被spring管理的bean对象。

@Component
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        if (SpringUtils.applicationContext == null){
            SpringUtils.applicationContext = applicationContext;
        }
    }

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

    // 通过name获取Bean
    public static Object getBean(String name){ return getApplicationContext().getBean(name);}

    // 通过class获取Bean
    public static <T> T getBean(Class<T> clazz){ return getApplicationContext().getBean(clazz);}

    // 通过name, 以及clazz返回指定的Bean
    public static <T> T getBean(String name, Class<T> clazz){ return getApplicationContext().getBean(name, clazz);}
}

使用时:
在这里插入图片描述即可获取被spring管理的bean对象,进行其他逻辑功能的实现,操作Dao或者Service。OK。

Spring Boot中,我们可以通过编写一个工具来实现对第三方HTTP接口的调用。通常,我们会使用RestTemplate来发送HTTP请求。以下是一个简单的示例: 首先,需要在Spring Boot项目中引入RestTemplate的依赖。在`pom.xml`中添加如下依赖(如果是Maven项目): ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 接下来,创建一个工具,比如`ThirdPartyApiCaller`,在这个中注入RestTemplate并提供一个方法来执行HTTP请求: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @Component public class ThirdPartyApiCaller { private final RestTemplate restTemplate; @Autowired public ThirdPartyApiCaller(RestTemplate restTemplate) { this.restTemplate = restTemplate; } // 发起GET请求的方法示例 public ResponseEntity<String> get(String url) { ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); return response; } // 发起POST请求的方法示例 public ResponseEntity<String> post(String url, Object requestEntity) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<Object> entity = new HttpEntity<>(requestEntity, headers); ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class); return response; } // 可以根据需要添加更多方法,如PUT、DELETE等 } ``` 在这个中,`get`方法用于发起GET请求,`post`方法用于发起POST请求。这些方法通过`RestTemplate`的相应方法实现HTTP调用。你也可以根据需要添加其他HTTP方法(如PUT、DELETE等)的调用。 使用此时,你只需要注入`ThirdPartyApiCaller`这个bean,然后就可以调用相应的方法来进行HTTP调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值