I am having difficulty getting constructor injection using xml working. I have two implementations of a base repository class and I would like to use the xml file to specify which of these to use.

If I switch to annotation (put @Primary in one of the implementing classes) then it works but I don't want to have to change each class file just to switch from one repository to another.

What am I doing wrong?

The base class

```
package com.iznogoud.cinj.repositories;
import java.util.ArrayList;

public abstract class StringRepository {
    public abstract ArrayList<String> getStrings();
}

```

The two implementing classes:

```
package com.iznogoud.cinj.repositories;
import java.util.ArrayList;
import org.springframework.stereotype.Repository;

@Repository
public class StringRepositoryImplOne extends StringRepository{

public StringRepositoryImplOne() {}

public ArrayList<String> getStrings() {
    ArrayList<String> result = new ArrayList<String>();
    result.add("One");
    result.add("Un");
    result.add("Eins");
    return result;
   }
}

```

...and...

```
package com.iznogoud.cinj.repositories;
import java.util.ArrayList;
import org.springframework.stereotype.Repository;

@Repository
public class StringRepositoryImplTwo extends StringRepository{

public StringRepositoryImplTwo() {}

public ArrayList<String> getStrings() {
    ArrayList<String> result = new ArrayList<String>();
    result.add("Two");
    result.add("Deux");
    result.add("Zwei");
    return result;
}
}

```

The controller where I want to inject one of the two repositories above:

```
package com.iznogoud.cinj.controllers;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;import 
org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.iznogoud.cinj.repositories.StringRepository;

@RestController
@RequestMapping("/cinj")
public class AController {

private StringRepository repository;

public AController(StringRepository _r) {
    repository = _r;
}

@GetMapping(path="/test", produces = MediaType.APPLICATION_JSON_VALUE)
List<String> getTimeSlots(HttpSession _session) throws IllegalArgumentException, ParseException  {

    ArrayList<String> availableStrings = repository.getStrings();
    return availableStrings;

 }
}

```

The Application class

```
package com.iznogoud.cinj;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.SpringVersion;

 @SpringBootApplication 
 @ImportResource("applicationContext.xml")
 public class CinjApplication {

    public static void main(String[] args) {

         System.err.println("Spring version: " + SpringVersion.getVersion());

        SpringApplication.run(CinjApplication.class, args);
     }

}

```

The XML configuration:

```
<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans.xsd
                       http://www.springframework.org/schema/context
                       http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="acont" class="com.iznogoud.cinj.controllers.AController">
    <constructor-arg ref="sr1"/>
</bean>  

<bean id="sr1" class="com.iznogoud.cinj.repositories.StringRepositoryImplOne" primary="true"/>
<bean id="sr2" class="com.iznogoud.cinj.repositories.StringRepositoryImplTwo" />

</beans>

```

The pom.xml (generated by Spring Initializr and not modified):

```
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath/> 
</parent>
<groupId>com.iznogoud</groupId>
<artifactId>cinj</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cinj</name>
<description>Constructor injection</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

```

And finally, the error msg (the short version):

```
mvn clean spring-boot:run
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.iznogoud:cinj >----------------------- 
[INFO] Building cinj 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]------------------------------    ---
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ cinj ---
[INFO] Deleting D:\Users\c82ssim\eclipse-workspace\so_01\target
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) > test-        compile @ cinj >>>
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ cinj     ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ cinj ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 5 source files to D:\Users\c82ssim\eclipse-work  space\so_01\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ cinj ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\Users\c82ssim\eclipse-    workspace\so_01\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @     cinj ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\Users\c82ssim\eclipse-    workspace\so_01\target\test-classes
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) < test-    compile @ cinj <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) @ cinj -    --
Spring version: 5.1.8.RELEASE

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-08-01 11:41:27.466  INFO 10612 --- [           main] com.iznogoud.cinj.CinjApplication        : Starting CinjApplication on     E82AAT4014 with PID 10612 (D:\Users\c82ssim\eclipse-    workspace\so_01\target\classes started by c82ssim in D:\Users\c82ssim\eclipse-    workspace\so_01)
2019-08-01 11:41:27.470  INFO 10612 --- [           main]     com.iznogoud.cinj.CinjApplication        : No active profile set, falling back     to default profiles: default
2019-08-01 11:41:28.482  INFO 10612 --- [           main]     trationDelegate$BeanPostProcessorChecker : Bean     'org.springframework.hateoas.config.HateoasConfiguration' of type     [org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$bbc50a5] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-08-01 11:41:28.943  INFO 10612 --- [           main]     o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8084     (http)
2019-08-01 11:41:28.969  INFO 10612 --- [           main]     o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-08-01 11:41:28.970  INFO 10612 --- [           main]     org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-08-01 11:41:29.078  INFO 10612 --- [           main] o.a.c.c.C.    [Tomcat].[localhost].[/]       : Initializing Spring embedded     WebApplicationContext
2019-08-01 11:41:29.078  INFO 10612 --- [           main]     o.s.web.context.ContextLoader            : Root WebApplicationContext:     initialization completed in 1472 ms
2019-08-01 11:41:29.823  INFO 10612 --- [           main]     o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService     'applicationTaskExecutor'
2019-08-01 11:41:29.925  WARN 10612 --- [           main]     ConfigServletWebServerApplicationContext : Exception encountered during context     initialization - cancelling refresh attempt:     org.springframework.beans.factory.BeanCreationException: Error creating bean     with name 'requestMappingHandlerMapping' defined in class path resource     [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$Enab    leWebMvcConfiguration.class]: Invocation of init method failed; nested exception     is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'acont' method
java.util.List<java.lang.String>     com.iznogoud.cinj.controllers.AController.getTimeSlots(javax.servlet.http.HttpSe    ssion) throws java.lang.IllegalArgumentException,java.text.ParseException
to {GET /cinj/test, produces [application/json]}: There is already     'AController' bean method
java.util.List<java.lang.String>     com.iznogoud.cinj.controllers.AController.getTimeSlots(javax.servlet.http.HttpSe    ssion) throws java.lang.IllegalArgumentException,java.text.ParseException mapped.

```

**Update 1:**
If I change the controller annotation to this:

```
@RestController(value="acont")

```

I get this exception:

```
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.6.RELEASE:run (default-cli) on project cinj: An exception occurred while running. null: InvocationTargetException: Configuration problem: Failed to register bean definition with name 'acont'
[ERROR] Offending resource: class path resource [applicationContext.xml]; nested exception is 
org.springframework.beans.factory.support.BeanDefinitionOverrideException: 
Invalid bean definition with name 'acont' defined in class path resource 
[applicationContext.xml]: Cannot register bean definition [Generic bean: class 
[com.iznogoud.cinj.controllers.AController]; scope=; abstract=false; 
lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; 
primary=false; factoryBeanName=null; factoryMethodName=null; 
initMethodName=null; destroyMethodName=null; defined in class path resource 
[applicationContext.xml]] for bean 'acont': There is already [Generic bean: 
class [com.iznogoud.cinj.controllers.AController]; scope=singleton; 
abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; 
autowireCandidate=true; primary=false; factoryBeanName=null; 
factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in 
file 
[D:\Users\c82ssim\eclipse- 
workspace\so_01\target\classes\com\iznogoud\cinj
\controllers\AController.class]] 
bound.here

```