我正在使用基於註釋的配置 - 根本沒有XML。服務未正確自動佈線 - 基於註釋的配置
我已經配置了所有內容,但無法弄清楚爲什麼OrderService
未被自動裝配並且仍然是null
。最底層的類是顯示實際問題的類。其他類都是我的配置。
我確實有log4j
在這個應用程序,但我沒有經驗。有沒有一種方法可以記錄正在掃描的軟件包/類,以幫助確定這不起作用的原因?
OrderService
@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 {
Main.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();
您的'@ ComponentScan'包中是否都有'OrderService'和'NewJobRequestHandler'?另外,您不需要'@ OrderService'的'@ Bean'。 '@ Service'已經將它識別爲一個bean。 – 2013-02-28 15:31:15
是的,它們都在'@ ComponentScan'中。你是說我不需要服務配置文件,因爲它被標記爲'@ Service'並且正在被掃描? – Webnet 2013-02-28 15:33:30
如果那是那裏唯一的豆子,是的,那就是我所說的。 – 2013-02-28 15:34:18