如何使用“ @Qualifier”动态指定参数?

时间:2019-01-15 06:08:09

标签: java spring

我想使用@Qualifier动态指定参数吗?怎么做?

@Qualifier(“ two”)'two'作为参数,可以是'one','three'或其他。

我可以使用aop动态设计“两个”吗?

表示我想通过参数@Qualifier更改服务名称。

网址“令牌”中的参数。

情况:网址:http://localhost:8080/insert/order,令牌具有参数:companyId = one


@RestController
public class ApiWebService {
       @Autowired
       @Qualifier("two")
      //@Qualifier("one")
       private BaseService baseService;

       @GetMapping("insert/order")
       public void test() {
            baseService.insertOrder();
       }

}


@Service("one")
public class CompanyOneService extends BaseService {

    @Override
    public void insertOrder() {
        System.out.println("conpanyOne");
        System.out.println("baseInsertOrder");
    }
}

@Service("two")
public class CompanyTwoService extends BaseService {

    @Override
    public void insertOrder(){
        System.out.println("companyTwo");
        System.out.println("baseInsertOrder");
    }
}

three
four
...

@Service
public class BaseService {

    public void insertOrder(){
        System.out.println("baseInsertOrder");
    }

}

3 个答案:

答案 0 :(得分:1)

你好!

不,您不能,主要是因为Java批注中的属性不允许分配变量。

实际上,您希望根据某些运行时条件(即您的情况下的companyId)选择要使用的实现。您可以使用带有@Configuration@Bean的工厂模式来实现它,这比丑陋的AOP解决方案更加优雅,更易于理解:

首先定义一个工厂:

@Configuration
public class ServiceFactory{

   @Bean
   public  BaseService companyOneService(){
     return new CompanyOneService();
   }

   @Bean
   public  BaseService companyTwoService(){
     return new CompanyTwoService();
   }

   public BaseService getService(Integer companyId){
        if(companyId == 1){
            return companyOneService();
        }else if(company==2){
            return companyTwoService();
        }else{
            //blablablab
        }
   }
}

在控制器中,注入ServiceFactory以根据公司ID获得相关服务

@RestController
public class ApiWebService {

       @Autowired
       private ServiceFactory serviceFactory;

       @GetMapping("insert/order")
       public void test() {
            Integer companyId = getCompanyIdFromToken(httpServletRequest); 
            BaseService service = serviceFactory.getService(companyId);
            service.blablabla();
       }

}

答案 1 :(得分:0)

将({auto})ApplicationContext注入类,并使用getBeans *方法之一查找所需的确切bean。

答案 2 :(得分:0)

方面

@Aspect
@Component
public class ApiAspect {

    @Pointcut("execution(* com.example.demo.control.ApiWebService.*(..))")
    public void apiInputWebService() {

    }

    @Before("apiInputWebService()")
    public void apiInputAuth(JoinPoint joinPoint) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes())
                .getRequest();
        String token = request.getHeader("Authorization");

        //compangId can be from token
        String compangId = "one";
        Object target = joinPoint.getTarget();
        Method method = target.getClass().getMethod("before", String.class);
        method.invoke(target, compangId);
    }

}

控制

@RestController
public class ApiWebService {

    private ApiService baseService;

    @Autowired
    private ApplicationContext applicationContext;

    public void before(String company) {
        baseService = (ApiService) applicationContext.getBean(company);
    }

    @GetMapping("insert/order")
    public void test() {
        baseService.insertOrder();
    }
}

服务

@Service
public class ApiService {

    public void insertOrder(){
        System.out.println("baseInsertOrder");
    }

}

@Service("one")
public class CompanyOneService extends ApiService {

    @Override
    public void insertOrder() {
        System.out.println("conpanyOne");
        System.out.println("baseInsertOrder");
    }
}

@Service("two")
public class CompanyTwoService extends ApiService {

    @Override
    public void insertOrder(){
        System.out.println("companyTwo");
        System.out.println("baseInsertOrder");
    }
}