直接从主类调用服务层

时间:2016-02-24 08:31:37

标签: spring spring-mvc

我有一个项目结构控制器 - >响应构建器 - >服务层。 这里服务层调用存储库层(数据库层)。

遵循结构时一切都很好。

但是,对于测试,我想直接从java主类调用服务层。

我该怎么做??????

我的控制器:

@RestController
@RequestMapping("/ishmam")
public class IshmamAddressController {

    @Autowired
    @Qualifier("ishmamAddressBuilder")
    IshmamAddressBuilder ishmamAddressBuilder;


    @RequestMapping("/getAddress")
    public IResponseDto<WebbCustomerAddressDto> getAllAddress(){
        return ishmamAddressBuilder.getAllAddress();
    }

}

我的构建器类是:

@Component("ishmamAddressBuilder")
public class IshmamAddressBuilder {

    @Autowired
    @Qualifier("ishmamAddressServiceImpl")
    IshmamAddressInterface ishmamAddressService;

    public IResponseDto<IshmamAddressResponseDto> getAllAddress(){
    IResponseDto<WebbCustomerAddressDto> response=new 
               IResponseDto<WebbCustomerAddressDto>(); 
    try{
        //here i call the service layer
        List<String> addressList=ishmamAddressService.getAllAddress();

    }catch(Exception e){
        throw e;
    }
    return addressList;
 }

我的服务层是:

@Service("ishmamAddressServiceImpl")
@Transactional

public class IshmamAddressServiceImpl implements IshmamAddressInterface {

    @Autowired(required = true)
    @Qualifier("webCustomerAddressRepository")
    WebCustomerAddressRepository webCustomerAddressRepository;

    @Override
    public IshmamAddressResponseDto getAllAddress() {
        List<WebCustomerAddress> aList = new ArrayList<WebCustomerAddress>();
        List<WebbCustomerAddressDto> dtoWebCustomerAddressList = new 
                      ArrayList<WebbCustomerAddressDto>();

        IshmamAddressResponseDto ishmamAddressResponseDto=new 
                      IshmamAddressResponseDto();

        try{

            aList = 
            address.getAllAddress(1);//Calling database layer

             ishmamAddressResponseDto=//Doing something,not important for 
                                       //question
    }

    return ishmamAddressResponseDto;

}           

现在我想要的是直接从主类调用服务层:

public class Address{

     public void getAddress(){
           IshmamAddressServiceImpl i=new IshmamAddressServiceImpl();
           List<String> list=i.getAllAddress();
     }


     public static void main(String[] args){
           Address a=new Address();
           a.getAddress();
      }

}

这个过程不起作用。我怎么能这样做???????

1 个答案:

答案 0 :(得分:1)

一旦你使用spring,你必须从不使用new来构建一个应该由spring管理的对象。

所以你可以:

  • 要么手动执行,要么意味着引导应用程序上下文,它具有与应用程序中相同的初始化,并明确地从中获取bean

    ApplicationContext ctx = new AnnotationConfigApplicationContext(
        Class<?>... annotatedClasses); // if it is the way you use it ...
    IshmamAddressInterface i = ctx.getBean("ishmamAddressServiceImpl", 
        IshmamAddressInterface .class);
    
  • 或使用在Junit测试中自动为您执行此操作的Spring test framework

    @WebAppConfiguration
    @ContextConfiguration(classes = ...)
    public class WebIntegrationTests {
    
        @Autowired
        @Qualifier("ishmamAddressServiceImpl")
        IshmamAddressInterface ishmamAddressService;
        ...
        @Test
        public void testGetAddress() {
            ishmamAddressService.getAddress();
            ...
        }
    }