我在運行並打開休息服務時遇到了一些問題,我看到我的服務實例獲得了@Proxy值。下面你可以看到類,我有:使用@Autowired獲取正確的實例
初始化器類:
public class AllureWebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
createDispatcherServlet(servletContext);
initializeListener(servletContext);
}
private void initializeListener(ServletContext servletContext) {
AnnotationConfigWebApplicationContext rootContext = createContext();
servletContext.addListener(new ContextLoaderListener(rootContext));
}
private void createDispatcherServlet(final ServletContext context) {
AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
dispatcherServlet.register(AllureWebConfig.class);
ServletRegistration.Dynamic dispatcher = context.addServlet(AllureWebConstants.DISPATCHER, new DispatcherServlet(dispatcherServlet));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping(AllureWebConstants.SLASH);
}
private AnnotationConfigWebApplicationContext createContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AllureWebConfig.class);
return context;
}
}
配置類:
@Configuration
@Import(AllureServiceConfig.class)
@ComponentScan(basePackages = {"com.allure.events.web.controller"})
@EnableWebMvc
public class AllureWebConfig extends WebMvcConfigurerAdapter {
@Autowired
private ApplicationContextProvider applicationContextProvider;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(AllureWebConstants.RESOURCES_DOUBLE_WILDCARD)
.addResourceLocations(AllureWebConstants.RESOURCES);
}
@Bean
public ApplicationContextProvider applicationContextProvider() {
return applicationContextProvider;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource;
messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:META-INF/web/resources/release");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(Include.NON_NULL);
converter.setObjectMapper(objectMapper);
converters.add(converter);
super.configureMessageConverters(converters);
}
}
控制器類:
@Controller
public class SupplierEndpoint extends AllureEndpoint {
@Autowired
SupplierService supplierService;
@RequestMapping(value = AllureWebConstants.SUPPLIERS, method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<Supplier> getSuppliers() {
List<Supplier> suppliers = getSuppliersList(supplierService.getSuppliers());
return suppliers;
}
}
服務接口
public interface SupplierService {
public List<SupplierDto> getSuppliers();
}
服務默認地將Impl
@Service
public class SupplierServiceImpl implements SupplierService {
@Autowired
private SupplierMapper supplierMapper;
@Autowired
private SupplierDtoHelper supplierHelper;
@Override
@Transactional
public List<SupplierDto> getSuppliers() {
List<Supplier> suppliers = supplierMapper.getSuppliers();
List<SupplierDto> dtos = new ArrayList<SupplierDto>();
for (Supplier supplier : suppliers) {
dtos.add(supplierHelper.convertEntityToDto(supplier));
}
return dtos;
}
}
,如果我調試,我看到實例值是:
supplierService - $ Proxy28 | - > h = JdkDynamicAopProxy
有人可以指導我嗎? Regards, Edgardo Quiroz
那麼如果你問你爲什麼看到一個代理,這是因爲'@ Transactional'註釋。如果這不是你所問的,那就詳細說明你的問題。 – minion
奴才是對的 - 這是預期的行爲(春天的方式)。但代理將方法調用「轉發」到您的「SupplierServiceImpl」bean – Ralph
謝謝@minion,您是對的。我需要檢查更多關於註釋並設置我的應用程序 – Edgard0