[英]Spring boot : constructor injection with xml gives “there is already n bean method”
I am having difficulty getting constructor injection using xml working. 我很难使用xml进行构造函数注入。 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. 我有两个基本存储库类的实现,我想使用xml文件指定要使用的哪个。
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. 如果我切换到注释(将@Primary放在实现类中的一个中),则它可以工作,但我不想只为了从一个存储库切换到另一个存储库而更改每个类文件。
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配置:
<?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): pom.xml(由Spring Initializr生成,未修改):
<?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: 更新1:如果我将控制器注释更改为:
@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
If the issue is about the ambigious method in the bean named "acont", maybe you have to name it: 如果问题与名为“ acont”的bean中的模棱两可的方法有关,则可能必须将其命名为:
@Controller("/acont")
public class AController {...}
By default the bean is detected by type in spring. 默认情况下,在spring中按类型检测Bean。 In your case you are injecting directly by abstract class. 在您的情况下,您直接通过抽象类进行注入。
private StringRepository repository;
In this case you need to use @Qualifier
to indicate which bean you are exactly referring to. 在这种情况下,您需要使用@Qualifier
来指示您确切指的是哪个bean。
@Repository("ImplTwo)
public class StringRepositoryImplTwo extends StringRepository{
Simillary, 类似的,
@Repository("ImplOne)
public class StringRepositoryImplOne extends StringRepository{
And update your dependency injection as, 并将您的依赖项注入更新为
@Qualifier("ImplTwo") // or "ImplOne" as per your requirement
private StringRepository repository;
Or as you are using constructor injection than do following, 或者您正在使用构造函数注入,而不是以下情况,
private StringRepository repository;
public AController(@Qualifier("ImplTwo") StringRepository _r) {
repository = _r;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.