Announcement Announcement Module
No announcement yet.
component-scan problem in Eclipse project Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • component-scan problem in Eclipse project

    I'm trying to test a service that uses an aspect to manage a necessary connection.

    The aspect handles creating a pool, opening the connection, etc. It's working when the webapp war is deployed. It's not working in a simple unit test I've written.

    The test is something very close to this:
    public class GatewayTest {
    	GatewayImpl gatewayImpl;
    	public void testConnection() throws Exception {
    Running the test, I get a BeanCreationException, on that @Autowired annotation at the GatewayImpl.

    I have this in test.xml:
    <context:component-scan base-package="com.example" />
    In this Eclipse project, there are multiple source directories, including src/test/java/com/example and src/main/java/com/example.

    The GatewayImpl I need for the test is in src/main, not src/test. src/test doesn't even have a GatewayImpl.

    Anyone have any idea how to resolve this?


  • #2
    Please use the search as these questions have been answered numerous times before... Program to interfaces not concrete classes. So use Gateway (the interface) instead of GatewayImpl..


    • #3
      Right, the thought occurred to me as I was driving home last night. The only change I made was moving to the Gateway from GatewayImpl, and there ya go, test works.

      Why was the component-scan failing? It scans differently on classes and interfaces?


      • #4
        The component-scan isn't failing your testcase is... I suggest a read of the AOP chapter in the reference guide this explains how AOP is applied. In short spring uses proxies to apply AOP which creates an object of type Gateway which wraps around the actual class.