spring-bean

Spring autowiring order and @PostConstruct

。_饼干妹妹 提交于 2019-12-03 02:44:32
问题 I have a question about auto-wiring order and @PostConstruct logic in Spring. For example following demo code I have a main Spring Boot class: @SpringBootApplication public class Demo1Application { @Autowired BeanB beanb; public static void main(String[] args) { SpringApplication.run(Demo1Application.class, args); } } and 2 @Service Definitions: @Service public class BeanB { @Autowired private BeanA beana ; @PostConstruct public void init(){ System.out.println("beanb is called"); } public

Spring autowiring order and @PostConstruct

梦想的初衷 提交于 2019-12-02 16:17:59
I have a question about auto-wiring order and @PostConstruct logic in Spring. For example following demo code I have a main Spring Boot class: @SpringBootApplication public class Demo1Application { @Autowired BeanB beanb; public static void main(String[] args) { SpringApplication.run(Demo1Application.class, args); } } and 2 @Service Definitions: @Service public class BeanB { @Autowired private BeanA beana ; @PostConstruct public void init(){ System.out.println("beanb is called"); } public void printMe(){ System.out.println("print me is called in Bean B"); } } @Service public class BeanA {

Bean order when autowired into list

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 07:44:05
I have defined an interface IWorker and some implementations of it as WorkerA and WorkerB , both annotated with @Component . I then autowire them into my app via: @Autowired private List<IWorker> workers = new ArrayList<IWorker>(); From what does the order the workers are put into the list depend on? How can I let additional WorkerC and WorkerD (also implementations of IWorker ) not annotated with @Component be autowired into the same list via my applicationContext.xml ? Is the order of WorkerC and WorkerD from the xml preserved? Is there a rule I can rely on in which order worker A, B, C and

Bean order when autowired into list

瘦欲@ 提交于 2019-12-02 07:09:01
问题 I have defined an interface IWorker and some implementations of it as WorkerA and WorkerB , both annotated with @Component . I then autowire them into my app via: @Autowired private List<IWorker> workers = new ArrayList<IWorker>(); From what does the order the workers are put into the list depend on? How can I let additional WorkerC and WorkerD (also implementations of IWorker ) not annotated with @Component be autowired into the same list via my applicationContext.xml ? Is the order of

Spring Bean Alias in JavaConfig

こ雲淡風輕ζ 提交于 2019-12-01 15:44:11
问题 I have a @Service annotated class which provides core functionality which I can use in all my projects: @Service public class MyService {} and another one which extends it to implement project specific stuff: @Service public class ExtendedMyService extends MyService {} Now I would like to configure a bean alias to be able to use @Qualifier("MyServiceAlias") when autowiring it using a property: # MyService qualifier (default: myService) myService.qualifier=extendedMyService In XML it would

Access a request scoped Bean in Service

扶醉桌前 提交于 2019-11-30 20:32:21
问题 I have a regular bean, which is either (a) @Scope("request") or (b) placed in a HttpServletRequest via Filter/ Interceptor. How to access this beans in a @Service which is kind of an application scoped singleton? The reason for this is, because I have a custom object RequestContext with some request metadata (mostly informations from custom httpHeaders). For know, i pass this object as parameter to each method on each service, which is a lot of boilerplate code. 回答1: As long as the bean is

Spring @Value(“${}”) often null

假如想象 提交于 2019-11-30 14:48:50
问题 I'm using Spring Boot application. In some @Component class @Value fields are loaded, instead on other classes they are always null . Seems that @Value (s) are loaded after my @Bean / @Component are created. I need to load some values from a properties file in my @Bean . Have you some suggestion? 回答1: The properties(and all of the bean dependencies) are injected after the bean is constructed(the execution of the constructor). You can use constructor injection if you need them there.

Spring @Value(“${}”) often null

佐手、 提交于 2019-11-30 12:23:58
I'm using Spring Boot application. In some @Component class @Value fields are loaded, instead on other classes they are always null . Seems that @Value (s) are loaded after my @Bean / @Component are created. I need to load some values from a properties file in my @Bean . Have you some suggestion? The properties(and all of the bean dependencies) are injected after the bean is constructed(the execution of the constructor). You can use constructor injection if you need them there. @Component public class SomeBean { private String prop; @Autowired public SomeBean(@Value("${some.prop}") String prop

Is it possible to set a bean name using annotations in Spring Framework?

时光怂恿深爱的人放手 提交于 2019-11-30 04:43:42
I have a bean like this: @Bean public String myBean(){ return "My bean"; } I want to autowire it: @Autowired @Qualifier("myBean") public void setMyBean(String myBean){ this.myBean=myBean; } I need something like: @Bean(name="myCustomBean") Is it possible to use custom names names for beans out of the box? If it isn't possible out of the box then how to create such a bean? You can set the name by using any one of the @Component annotations. Here is the official doc. @Service("myMovieLister") public class SimpleMovieLister { // ... } This will create a bean namely myMovieLister instead of

How to define @Value as optional

纵然是瞬间 提交于 2019-11-29 21:09:53
I have the following in a Spring bean: @Value("${myValue}") private String value; The value is correctly injected. However, the variable needs to be optional, it is passed in as a command line parameter (which is then added to the Spring context using a SimpleCommandLinePropertySource ), and this argument will not always exist. I have tried both the following in order to provide a default value: @Value("${myValue:}") @Value("${myValue:DEFAULT}") but in each case, the default argument after the colon is injected even when there is an actual value - this appears override what Spring should