spring-java-config

Injecting object into Spring Configuration

梦想与她 提交于 2021-02-08 15:38:26
问题 I am turning old xml/java configuration into pure java config. In xml I used injection of parameters into configuration file like this: <bean class="com.project.SpringRestConfiguration"> <property name="parameters" ref="parameters" /> </bean> @Configuration public class SpringRestConfiguration { private Parameters parameters; public void setParameters(Parameters parameters) { this.parameters = parameters; } // @Bean definitions ... } Is it possible to inject Parameters in javaconfig? (Without

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

Make spring @Value take default value from static field

回眸只為那壹抹淺笑 提交于 2020-03-15 07:35:07
问题 I have a java configuration where I create bean using some properties, defined in application.properties . For one of them I have a default value which is pretty long, so I extracted this value to a public static final String field of this configuration, now I want to make @Value use it as a default value. So eventually I want something like this: @Configuration public class MyConfiguration { public static final String DEFAULT_PROPERTY_VALUE = "long string..."; @Bean("midPriceDDSEndpoint")

What is difference between @ImportAutoConfiguration and @Import

廉价感情. 提交于 2020-01-23 05:21:08
问题 Is it true that org.springframework.boot.autoconfigure.ImportAutoConfiguration is improved replacement for org.springframework.context.annotation.Import because does the same and additionally respects @AutoConfigureBefore , @AutoConfigureAfter and @AutoConfigureOrder ? 回答1: Is it true that org.springframework.boot.autoconfigure.ImportAutoConfiguration is improved replacement for org.springframework.context.annotation.Import ? No it is not a replacement since @ImportAutoConfiguration is a

Converting spring xml to java configuration with implicit setter autowiring and ComponentScan

南楼画角 提交于 2020-01-13 07:05:31
问题 I have two classes: vehicle.Tire and vehicle.Car. package vehicle; @Named public class Tire { private String age; } package vehicle; @Named public class Car { private Tire tire; // Spring calls this setter because default-autowire="byName" of xml configuration public void setTire(Tire newTire) { this.tire = newTire; } public Tire getTire() { return this.tire; } } The following spring xml works fine. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001

PropertySource not available during ConditionalOnExpression evaluation

感情迁移 提交于 2020-01-11 09:16:50
问题 I have this following component class which I'd like to instantiate depending on a property; @Component("componentA") @PropertySource("classpath:components.properties") @ConditionalOnExpression("'${components.enabled}'.contains('componentA')") public class ComponentA implements ComponentIF { ... components.properties file has the following property; components.enabled=componentA,componentB,componentD The problem is that @PropertySource("classpath:components.properties") seems to be not

spring web-mvc 4 java config doesn't work

一曲冷凌霜 提交于 2020-01-05 08:23:06
问题 I'm trying to run elementary spring-4 web-mvc application without xml configuration at all. I've looked spring documentation and examples, but it didn't work for me. My controller: package com.nikolay.exam.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HomeController {

Spring cache using memcached

不想你离开。 提交于 2020-01-05 07:31:09
问题 I'm using https://github.com/AKQABER/simple-spring-memcached to have memcached integrated as a cache implementation with spring. But I'had no success :P I'm using spring 4 and spring-data-jpa public interface FooRepository extends JpaRepository<Foo, Long> { @Cacheable(value = "defaultCache", key = "lala") Foo findByNameAndDescription(String name, String description); } I hardcoded the key for test purposes. Nothing special, use a cache called "defaultCache". The configuration of memcached is:

How to register many object as beans in Spring Java Config?

我的未来我决定 提交于 2020-01-05 06:17:23
问题 I would like dynamically register multiple objects as Spring beans. Is is possible, without BeanFactoryPostProcessor ? @Configuration public class MyConfig { @Bean A single() { return new A("X");} @Bean List<A> many() { return Arrays.asList(new A("Y"), new A("Z"));} private static class A { private String name; public A(String name) { this.name = name;} @PostConstruct public void print() { System.err.println(name); } } } Actual output shows only one bean is working: X Expected: X Y Z Spring 4