autowired

Spring: unit test for class that has both field & constructor injection

好久不见. 提交于 2021-02-19 02:46:05
问题 I have below setup of classes. class Base { @Autowired private BaseService service; //No getters & setters .... } @Component class Child extends Base { private final SomeOtherService otherService; @Autowired Child(SomeOtherService otherService) { this.otherService = otherService; } } I am writing a unit test for the Child class. If I use @InjectMocks then the otherService comes out to be null. If I use, the constructor of Child class in the setup of the test, then the fields in Base class

Why does autowiring provides a different entity manager instance for event subscribers?

早过忘川 提交于 2021-02-10 11:53:38
问题 I have different services in a symfony4 project, which get the entity manager injected. I found out, that doctrine event subscribers and services used by them get a different entity manager instance than the other services and when you call self::$container->get('doctrine')->getManager(). I have seen up to three different instances in my project, but I don't know under which circumstances even more instances are created. I have added the function spl_object_id to all constructors to see which

Why does autowiring provides a different entity manager instance for event subscribers?

本小妞迷上赌 提交于 2021-02-10 11:52:13
问题 I have different services in a symfony4 project, which get the entity manager injected. I found out, that doctrine event subscribers and services used by them get a different entity manager instance than the other services and when you call self::$container->get('doctrine')->getManager(). I have seen up to three different instances in my project, but I don't know under which circumstances even more instances are created. I have added the function spl_object_id to all constructors to see which

Why does autowiring provides a different entity manager instance for event subscribers?

那年仲夏 提交于 2021-02-10 11:51:21
问题 I have different services in a symfony4 project, which get the entity manager injected. I found out, that doctrine event subscribers and services used by them get a different entity manager instance than the other services and when you call self::$container->get('doctrine')->getManager(). I have seen up to three different instances in my project, but I don't know under which circumstances even more instances are created. I have added the function spl_object_id to all constructors to see which

Use an external repository with symfony4 trouble with autoload and parameters

我的梦境 提交于 2021-02-08 10:16:30
问题 I use two self developed libraries located in github as a private repository so I can reuse it in several projects. I include them via composer: "license": "proprietary", "repositories": [ { "type": "vcs", "url": "https://github.com/my-account/puc-web-sap-client.git", "options": { "ssl": { "verify_peer": "false" } } }, { "type": "vcs", "url": "https://github.com/my-account/puc-web-soap-client.git", "options": { "ssl": { "verify_peer": "false" } } } ], Now symfony complains that the classes

What is the alternative of using Qualifier annotation in XML in Spring?

拈花ヽ惹草 提交于 2021-02-08 08:17:11
问题 In Spring , if there are two bean ids which refer to the same class and we just want to inject values from only one bean, then we normally use the following annotations in conjunction : @Autowired @Qualifier("bean1") How to achieve the same thing using the xml specification ? What is the alternative of using qualifier annotation in xml ? 回答1: Not an exact alternative but you can use autowire-candidate="false" to all those beans which you want to exclude from being autowired apart from the one

What is the alternative of using Qualifier annotation in XML in Spring?

有些话、适合烂在心里 提交于 2021-02-08 08:14:12
问题 In Spring , if there are two bean ids which refer to the same class and we just want to inject values from only one bean, then we normally use the following annotations in conjunction : @Autowired @Qualifier("bean1") How to achieve the same thing using the xml specification ? What is the alternative of using qualifier annotation in xml ? 回答1: Not an exact alternative but you can use autowire-candidate="false" to all those beans which you want to exclude from being autowired apart from the one

Using @Spy and @Autowired together

久未见 提交于 2021-02-05 22:57:33
问题 I have a Service Class with 3 methods, Service class is also using some @Autowired annotations. Out of 3 methods, I want to mock two methods but use real method for 3rd one. Problem is: If I am using @Autowired with @Spy, all three real method implementation is being called. If I am using @Spy only, call to real method is return with Null pointer as there is no initialisation of Autowired objects. 回答1: I know about these two options: Use @SpyBean annotation from spring-boot-test as the only

How to autowire a service in grails (2.5.5) with multiple capital letters at front

﹥>﹥吖頭↗ 提交于 2021-01-28 20:44:00
问题 I have a domain class named ABCDCode and created a service for the this ABCDCodeService. Now I want to use this service in controllers so I wrote it like below: class TestController{ ABCDCode abcdCode def index(int id){ abcdCode.getData(id) //Here I am getting NullPOinterException } } I am suspecting something wrong with the autowiring by name. 回答1: Grails looks first two characters for beans naming. If the second character of the controller/service is capital then Grails did not convert the

Will Spring throw an exception if there is ambiguity when resolving a bean

本秂侑毒 提交于 2021-01-28 18:09:42
问题 Can I get Spring to throw an exception when there is more than one bean with the same type? The current behavior seems to be to inject null . 回答1: You need to use the @Qualifier annotation together with @Annotated to resolve ambiguity between different beans with the same type. The parameter to Qualified is the name of the bean, which is automatically set based on the name of the method that is annotated with @Bean . @Autowired public RobotController (@Qualifier("gundam") RobotEngine