spring 自动注入和 dubbo服务调用问题

今天 遇到这样一个问题:
工程A中我写写了一个spring security的自定义认证类,这个类始终不能由容器管理,但是这个时候 我需要通过spring 自动注入某个服务类来获取用户相关信息 来进行认证)
问题出现在:
自定义认证类不是由容器管理的 那么在类里面使用@Autowried这些都是没有用的,服务是注入不进来的
解决方案:
在自定义认证类中手动初始化 容器,从容器中用getBean的方式来获取类


public class MyUserDeatilsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //手动初始化容器类 如果配置用的java类配置方式 就是 AnnotationConfigApplicationContext
        ApplicationContext cnt = new ClassPathXmlApplicationContext("classpath:config/*.xml");
        ISellerService sellerService = (ISellerService) cnt.getBean("sellerService");

        System.out.println("springSecurity 自定义认证类");
        //后面代码省略
        }
   }
Spring Boot中整合Dubbo服务通常需要以下几个步骤: 1. **添加依赖**: 首先,你需要在项目的`pom.xml`文件中添加DubboSpring Boot的相关依赖。例如,如果你使用的是Spring Cloud Alibaba的Dubbo版本,可以添加如下依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-dubbo</artifactId> </dependency> ``` 2. **配置Dubbo应用**: 在`application.properties`或`application.yml`中配置Dubbo服务的基本信息,如注册中心地址、端口等: ```properties dubbo.application.name=my-dubbo-service dubbo.registry.address=zk://localhost:2181 ``` 3. **创建Dubbo服务接口和实现**: 定义Dubbo服务接口,并在Spring Bean中实现它: ```java @Service("myService") public interface MyService { String sayHello(String name); } // 实现 @Component public class MyServiceImpl implements MyService { @Override public String sayHello(String name) { return "Hello, " + name; } } ``` 4. **启用Dubbo扫描**: 在Spring Boot中启用Dubbo自动扫描功能,以便Spring能够发现并管理你的Dubbo服务: ```java @EnableDubbo public class AppConfig { //... } ``` 5. **消费Dubbo服务**: 如果有其他模块需要消费这个Dubbo服务,可以通过Spring的@Autowired注解注入调用服务方法: ```java @Autowired private MyService myService; public void callService() { String response = myService.sayHello("User"); System.out.println(response); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值