autowired

spring - ApplicationContext registerBean autowiring fails but getBean works in Spring 5

橙三吉。 提交于 2019-12-07 10:27:39
问题 I'm using a configuration class that uses dynamic bean registration: @Configuration public class ConfigClass { @Autowired private GenericApplicationContext applicationContext; @PostConstruct private void init() { System.out.println("init"); applicationContext.registerBean("exService", ExecutorService.class, () -> Executors.newFixedThreadPool(10), bd -> bd.setAutowireCandidate(true)); System.out.println("init done"); } } If I try to autowire the bean, application startup fails with error Field

injecting derived property for @Repository bean without @Autowired in super class

≯℡__Kan透↙ 提交于 2019-12-07 04:03:50
问题 I would like to use @Repository spring annotation to avoid adding bean in context.xml. I use ibatis integration, so my repository class looks like this @Repository("userDao") public class UserDaoMybatis extends SqlMapClientDaoSupport implements UserDao { // ... } SqlMapClientDaoSupport (spring library class) has final method for setting required property which is not annotated with @Autowired or @Resourse public final void setSqlMapClient(SqlMapClient sqlMapClient) { if (!this

What's the difference between @Autowired and getting a bean from the application context?

点点圈 提交于 2019-12-07 04:00:57
问题 I have a dao unit test that is declared as follows: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring/applicationContext.xml"}) @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) @Transactional public class RegisterDaoTest extends AbstractTransactionalJUnit4SpringContextTests { ApplicationContext applicationContext = new FileSystemXmlApplicationContext("classpath:/spring/applicationContext.xml"); private

Autowiring Spring superclass

怎甘沉沦 提交于 2019-12-07 03:07:52
问题 Why does Spring automatically choose the superclass types during autowiring? For instance, if I have @Component public class Foo {} @Component public class Bar extends Foo {} and someone autowires @Autowired private Foo foo; How come Spring always chooses the supertype Foo ? Shouldn't this be an " ambiguous " mapping (and cause Spring to throw an error)? Don't you technically have two Foo candidates? (e.g., Bar gets automatically picked when @Component is removed from Foo...) 回答1: That might

How to autowire this TokenStore

徘徊边缘 提交于 2019-12-07 00:30:10
问题 How do I trigger auto-logout of this sample Spring Boot OAuth2 app? I tried adding the following code from an answer to this other posting into a new controller class in the demo package of the authserver app: package demo; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.provider.token.TokenStore; import org

Autowired spring bean is not a proxy

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 21:49:25
问题 I'm working on a very small application connecting to a MySQL database. I'm trying to create table record but getting 'no transaction in progress'. I have all the right stuff in place: a service interface MyService and its implementation MyServiceImpl I have annotated the service impl with @Service In the controller I used the interface name for the field @Autowired MyService I have the correct transaction configuration as it was originally generated by roo There is a public method MyService

Spring @Autowired详解

自作多情 提交于 2019-12-06 18:20:07
In last Spring auto-wiring in XML example, it will autowired the matched property of any bean in current Spring container. In most cases, you may need autowired property in a particular bean only. In Spring, you can use @Autowired annotation to auto wire bean on the setter method, constructor or a field. Moreover, it can autowired property in a particular bean. Note The @Autowired annotation is auto wire the bean by matching data type. See following full example to demonstrate the use of @Autowired . 1. Beans A customer bean, and declared in bean configuration file. Later, you will use “

Spring MVC: How autowiring of HttpSession works?

孤街醉人 提交于 2019-12-06 15:20:23
问题 I would like to know how autowiring of HttpSession works. If we declare like below: @Autowired private HttpSession httpSession; When exactly in the Spring workflow, httpSession variable declared above will be initialized with request.getSession(true) ? 回答1: I don't understand why you want to autowire HttpSession but here is how autowiring works. To Autowire a class you need to specify it as a bean either by using annotations (@Controller, @Service, @Repository, @Component) or by declaring

Spock How to mock Autowired class' function call within a method

a 夏天 提交于 2019-12-06 15:00:09
I have a class that I want to test in that looks like this: package com.something; import org.springframework.beans.factory.annotation.Autowired; public class ClassToTest implements InterfaceToTest{ @Autowired AnotherService serviceA; @Override public List<String> methodToTest(List<String> randomVar){ ... String stringA = serviceA.someFunction(randomVar); ... } } How can I mock the results from the call to serviceA.someFunction(randomVar) to return any String of my choice when testing with spock? package com.something; import spock.lang.Shared import spock.lang.Specification class TestClass

Make object spring managed

心不动则不痛 提交于 2019-12-06 12:11:23
How can I get an already existing object spring managed? I would like to hook it up to Springs AoP capabilities using aspectj . I know this to be a challenge since Spring AoP uses dynamic proxies which probably are created along with the object. Why do I need this? I have a third-party class which takes a constructor argument which is only known in runtime , hence it seems I cannot add it to my applicationContext or use springs FactoryBean interface for construction. Is there any other way? I've already tried the following without great success: Obj obj = new ThirdPartyObj("runtime constructor