spring-bean

Dynamically add new queues, bindings and exchanges as beans

空扰寡人 提交于 2019-11-29 06:14:39
I'm currently working on a rabbit-amqp implementation project and use spring-rabbit to programmatically setup all my queues, bindings and exchanges. (spring-rabbit-1.3.4 and spring-framework versions 3.2.0) The declaration in a javaconfiguration class or xml-based configuration are both quite static in my opinion declared. I know how to set a more dynamic value (ex. a name) for a queue, exchange or binding like this: @Configuration public class serverConfiguration { private String queueName; ... @Bean public Queue buildQueue() { Queue queue = new Queue(this.queueName, false, false, true,

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

萝らか妹 提交于 2019-11-29 01:56:41
问题 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? 回答1: What you are asking is already available in Spring 4.3.3 By default, configuration classes use a @Bean method’s name

Spring choose bean implementation at runtime

廉价感情. 提交于 2019-11-28 18:36:14
I'm using Spring Beans with annotations and I need to choose different implementation at runtime. @Service public class MyService { public void test(){...} } For example for windows's platform I need MyServiceWin extending MyService , for linux platform I need MyServiceLnx extending MyService . For now I know only one horrible solution: @Service public class MyService { private MyService impl; @PostInit public void init(){ if(windows) impl=new MyServiceWin(); else impl=new MyServiceLnx(); } public void test(){ impl.test(); } } Please consider that I'm using annotation only and not XML config.

How to define @Value as optional

情到浓时终转凉″ 提交于 2019-11-28 16:55:56
问题 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

Spring DI (Beans) with multiple concretes…picking one of them

痴心易碎 提交于 2019-11-28 14:41:55
I have a similar question here Guice with multiple concretes......picking one of them with a solution for Guice. But I have a different project using spring di (beans), but with the same kind of issue. I have an interface with N number of concretes. (3 here) public interface OrderProcessorInterface { void ProcessOrder(String preferredShipperAbbreviation, Order ord); } public class FedExShipper implements ShipperInterface { private Log logger; public FedExShipper(Log lgr) { if (null == lgr) { throw new IllegalArgumentException("Log is null"); } this.logger = lgr; } public void ShipOrder(Order

Spring bean with runtime constructor arguments

青春壹個敷衍的年華 提交于 2019-11-28 06:51:57
I want to create a Spring bean in Spring Java configuration with some constructor arguments passed at runtime. I have created the following Java config, in which there is a bean fixedLengthReport that expects some arguments in constructor. @Configuration public class AppConfig { @Autowrire Dao dao; @Bean @Scope(value = "prototype") **//SourceSystem can change at runtime** public FixedLengthReport fixedLengthReport(String sourceSystem) { return new TdctFixedLengthReport(sourceSystem, dao); } } But i am getting error that sourceSystem couldn't wire because no bean found. How can I create bean

Difference between JavaBean and Spring bean

拥有回忆 提交于 2019-11-27 20:02:17
问题 I am new to Spring MVC and have a little idea of the usage of java beans in Java. What is the basic difference between a Java bean and Spring bean? 回答1: JavaBeans: At a basic level, JavaBeans are simply Java classes which adhere to certain coding conventions. Specifically, classes that have public default (no argument) constructors allow access to their properties using accessor (getter and setter) methods implement java.io.Serializable Spring Beans: A Spring bean is basically an object

Spring DI (Beans) with multiple concretes…picking one of them

偶尔善良 提交于 2019-11-27 08:42:20
问题 I have a similar question here Guice with multiple concretes......picking one of them with a solution for Guice. But I have a different project using spring di (beans), but with the same kind of issue. I have an interface with N number of concretes. (3 here) public interface OrderProcessorInterface { void ProcessOrder(String preferredShipperAbbreviation, Order ord); } public class FedExShipper implements ShipperInterface { private Log logger; public FedExShipper(Log lgr) { if (null == lgr) {

how to get multiple instances of same bean in spring?

怎甘沉沦 提交于 2019-11-27 07:43:37
问题 By default, spring beans are singletons. I am wondering if there is a way to get multiple instances of same bean for processing. Here is what I do currently @Configuration public class ApplicationMain { @Value("${service.num: not configured}") private int num; //more code @PostConstruct public void run(){ for (int i = 0; i < num ; i++) { MyService ser = new MyService(i); Future<?> tasks = executor.submit(ser); } } } Here is the Service class public class MyService implements Runnable {

Spring choose bean implementation at runtime

ⅰ亾dé卋堺 提交于 2019-11-27 00:20:59
问题 I'm using Spring Beans with annotations and I need to choose different implementation at runtime. @Service public class MyService { public void test(){...} } For example for windows's platform I need MyServiceWin extending MyService , for linux platform I need MyServiceLnx extending MyService . For now I know only one horrible solution: @Service public class MyService { private MyService impl; @PostInit public void init(){ if(windows) impl=new MyServiceWin(); else impl=new MyServiceLnx(); }