我正在使用基于注释的配置——根本没有 XML。
我已经配置了所有内容,但无法弄清楚为什么OrderService
没有自动装配并继续null
. 下面最底部的类是显示实际问题的类。其他类都是我的配置。
我确实有log4j
这个应用程序,但对它没有经验。有没有办法可以记录正在扫描的包/类以帮助确定为什么这不起作用?
订单服务
@Service
public class OrderService extends GenericService<OrderDAO, Order> {
@Autowired
OrderDAO dao;
}
服务配置
@Configuration
public class Config {
@Bean
public OrderService orderService() {
return new OrderService();
}
}
主要配置
@Configuration
@ComponentScan(basePackages = {
"com.production.api",
//todo: may not need the rest of these
"com.production.api.dao",
"com.production.api.models",
"com.production.api.requests",
"com.production.api.requests.job",
"com.production.api.resources",
"com.production.api.resources.job",
"com.production.api.services"
})
@Import({
com.production.api.services.Config.class,
com.production.api.dao.Config.class
})
@PropertySource(value= "classpath:/META-INF/application.properties")
@EnableTransactionManagement
public class Config {
主.java
public static void main(String[] args) throws IOException {
//process annotation configuration
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
HttpServer httpServer = startServer();
System.out.println(String.format("Jersey app started with WADL available at " + "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...", BASE_URI, BASE_URI));
System.in.read();
httpServer.stop();
}
问题出在哪里...
@Component
public class NewJobRequestHandler {
public static Logger logger = Logger.getLogger(Logger.class.getName());
//@todo Why isn't this autowiring?
@Autowired
OrderService orderService;
public void instantiateOrderService() {
//@todo remove this manual wiring
orderService = (OrderService) ApplicationContextProvider.getApplicationContext().getBean("orderService");
}
public AuthenticatedApiResponse getResponse(AuthenticatedRequest<NewJobRequest> request) {
instantiateOrderService();