Lets say i have properties class ExampleProps:
#ConfigurationProperties(prefix = "prefix.stuff")
#ConstructorBinding
#AllArgsConstructor
public class ExampleProps {
...
}
Which is located in src/main/java/com/example/props/.
When spring scans for ConfigurationProperties it gives it following name
"prefix.stuff-com.example.props.ExampleProps"
Can i somehow change naming strategy to the regular one (Class name with lowercase first letter)?
You can control the name of the bean to which configuration properties are bound if you define it using a #Bean method:
#Configuration
class ExampleConfiguration {
#Bean("theNameThatYouWantTheBeanToHave")
#ConfigurationProperties("prefix.stuff")
ExampleProps exampleProps {
return new ExampleProps();
}
}
For this to work, you'd have to stop using constructor binding and switch to setter-based binding instead. In outline form, that would leave ExampleProps looking like this:
public class ExampleProps {
// Properties fields
// Getters and setters
}
Having said this, I would think twice about following this approach. Generally speaking, the name that's assigned to a bean should not matter. You may want to look at why the name is important and see if that can be changed rather than trying to force the bean to have a particular name.
Related
I have a postgres database which stores (as a String) the relevant class to use dependent on the information coming in from the user.
e.g. user has input Name, the database has the value NameFinder() stored against this and the code needs to create an instance of NameFinder().
I was wondering if there was a way of using reflection to instantiate this class as an #Autowired component, and then call the relevant function.
I can't seem to find a guide that uses #Autowired classes so any help would be appreciated.
For autowiring to work you need the class which uses #Autowired to be a #Component (or a child like #Service ...). https://www.baeldung.com/spring-autowire
For Spring to know what to inject, you need to define a #Bean in your Configuration
https://www.baeldung.com/spring-bean
As for the reflective instantiation in the bean:
#Bean
public Name getName(Database db) {
String nameFqn = db.getConfigTable().getNameFQN();
return (Name) Class.forName(nameFqn).getConstructor().newInstance();
}
Note this uses a no-arg public constructor. FQN means fully-qualified name, i.e. com.some.pkg.NameFinder
assuming:
package com.some.pkg;
class NameFinder implements Name {
public NameFinder(){}
}
I feel like a Spring Bean should be configurable also directly from a FQN without using reflection but I don't know how. Try reading up on a BeanFactory or something similar. Usually reflection is to be avoided.
How can I enable a #Configuration class only if the corresponding #EnableCustomConfiguration annotation has been used?
To make it clear I am trying to recreate the behaviour of common SpringBoot annotations, like e.g. #EnableEurekaClient, #EnableWebSecurity and so on.
This is my enabler:
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
public #interface EnableMultitenancy {}
Here my configuration properties:
#Getter
#Setter
#Validated
#ConfigurationProperties("multitenancy")
public class MultitenancyProperties {
#NotEmpty(message = "You must provide at least one tenant")
private List<Tenant> tenants;
}
and this is my Configuration class:
#Configuration
#Conditional("On EnableMultitenancy used")
#EnableConfigurationProperties(MultitenancyProperties.class)
public class MultitenancyConfiguration{
#Bean
public MyFirstBean first(MultitenancyProperties properties){
return new MyFirstBean(properties);
}
#Bean
public MySecondBean second(MultitenancyProperties properties){
return new MySecondBean(properties);
}
}
How can I write such a condition, e.g. the annotation has been used on a class/component?
org.springframework.context.annotation.ConditionContext interface shows theability of #Conditional.
ConditionContext contains five methods:
getRegistry
getBeanFactory
getEnvironment
getResourceLoader
getClassLoader
So it seems your cannot directly depend on annotation, eg: #EnableCaching. But you can depend on the bean related to them, eg: ProxyCachingConfiguration for EnableCaching.
But i think is not a good design. Usually we will declare our dependency, this makes component sperately, also make things easy. When user want to use our component, they don't need to add all of depended annotation, eg: #EnableEureka,#EnableWebSecurity, just # MultitenancyConfiguration.
In your MultitenancyConfiguration class, you just need to replace the current #Conditional annotation with #ConditionalOnClass(EnableMulitenancy.class).
Update: Sorry, I misunderstood. Why don't you just import the MultitenancyConfiguration class by using #Import(MultitenancyConfiguration.class) in your EnableMultitenancy class? This will guarantee that your MultitenancyConfiguration class is enabled whenever #EnableMultitenancy is used.
I have 3 beans which implements same interface. But i want to instantiate only one of them, if A is available then A, if there is no A then B, and if there is no A and B then C.
When i had only two of them it was simple, default one had '#ConditionalOnMissingBean' annotation. But not sure will that work with 3 of them.
Is it possible to solve this with annotations?
I don't want to create factory method, as I have multiple applications using those components, and i dont have ability to change some of them
Yes, using the #ConditionalOnMissingBean annotation , you should set the code like this:
#Configuration
public class ConditionalOnMissingBeanConfig {
#Bean
public A beanA(){
return new A(); // will initialize as normal
}
#Bean
#ConditionalOnMissingBean(name="beanA")
public B beanB(){
return new B(); // it will not initialize as
// beanA is present in the beanFactory.
}
#Bean
#ConditionalOnMissingBean(name="beanB")
public C beanC(){
return new C(); // will get initialized as there is no
// bean with name beanB in BeanFactory.
}
}
Conditional that only matches when the specified bean is missing from the beanfactory. This will only match when the bean definition is processed by the application context and as such is recommended to be used by auto-configuration classes only.
Let me propose an alternative approach:
You can introduce a configuration variable that will denote the "domain" related explanation of what should the system do
Example:
If A, B, C are, say, different caching strategies (in-memory, redis, ehcache), then introduce the following configuration value:
caching.type=ehcache // or in-memory or redis
Then the beans can be declared as follows:
#ConditionalOnProperty(name="caching.type" , havingValue="in-memory", matchIfMissing = true) // this is a fallback if no property is specified
class InMemoryCaching implements CachingStrategy {}
#ConditionalOnProperty(name="caching.type" , havingValue="ehcache", matchIfMissing = false)
class EhcacheCaching implements CachingStrategy {}
#ConditionalOnProperty(name="caching.type" , havingValue="redis", matchIfMissing = false)
class RedisCaching implements CachingStrategy {}
I know its slightly different solution, but it has the following benefits:
its easy to add yet another implementation of the interface - no code changes in existing beans
beans are not aware of each other even not at the level of bean names in spring
easy to debug - just look at the configuration property value
this approach can be applied to #Configuration and manage set of beans
I'm just learning spring, and something struck me as very odd about the annotation configurations using the name attribute as a string.
#Bean(name = "com.my.injected.Service")
public InjectedService injectedService() {
return injectedService;
}
Is this name similar to the Spring Bean XML configuration id and class attributes?
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
Why isn't this simply
#Bean(clazz = com.my.injected.Service.class)
public InjectedService injectedService() {
return injectedService;
}
instead?
You're fully qualifying the path in both cases and actually using the class makes it way easier for your IDE to tell you when you've screwed it up. I understand that the XML configuration came first, and naturally it was always looking up things by string, so is this just a holdover? Is there some advantage to using strings or major disadvantage to using .class?
Question was originally based on a false premise. I edited it to spell out what this premise was and make it less confusing for new people who come along. Hopefully I did this such that the given answers are still exactly applicable; apologies if not.
#Bean annotation is meant to provide a spring bean. The type of the bean to provide will be the same type of the class/interface you define in the return method. So, instead of declaring to return a concrete class in the method, return the top (abstract) class/interface instead.
Imagine this case:
public interface MyEntityDao {
MyEntity get(String id);
}
#Repository
public class MyEntityDaoDatabaseImpl implements MyEntityDao {
#Override
public MyEntity get(String id) {
/* implementation that goes to database every time */
}
}
#Repository
public class MyEntityDaoCacheImpl implements MyEntityDao {
#Override
public MyEntity get(String id) {
/* implementation that looks the data
up in cache, never in database */
}
}
#Configuration
public class MyAppConfiguration {
#Bean
public MyEntityDaoDatabaseImpl method1() {
return new MyEntityDaoDatabaseImpl();
}
#Bean
public MyEntityDaoCacheImpl method2() {
return new MyEntityDaoCacheImpl();
}
}
#Service
public class MyEntityService {
#Autowired //what to inject here?
MyEntityDao dao;
}
In case above, there are two implementations of the proposed interface. How the framework may be able to understand which implementation to use except for the name?
#Service
public class MyEntityService {
#Autowired
#Qualifier("properBeanNameToInject")
MyEntityDao dao;
}
Bean name is not necessarily related to its class or even any of interfaces it implements. It is a name and nothing more. When you use the annotation configuration, Spring figures out what the exact class or interface the #Bean provides like the rest of java code would: either through the fully qualified name in the code or through the imports specified in the file. In your case, you presumably have an import com.my.injected.Service; statement at the top of the java file.
Your example is using the fully qualified class name as the bean name. It is your choice. You could use any other identifier. Using the fully qualified name could be useful if your code is providing an object that is named exactly like another 3rd party #Bean object that your code must include or consume. However, you could just as easily use name = "myService".
The bean name helps Spring (and application programmer) to distinguish between multiple instances of of the same bean class because you can deploy the same class as bean several times. If only one instance of bean type appear you event do not have to give it name manually: spring does this by default.
If you have several beans that have the same type or implement the same interface and you want to refer specific bean use #Qualifier annotation.
Suppose you have one interface
public interface A {
public void doSomething();
}
and two implementation classes
#Component(value="aImpl1")
public class AImpl1 implements A {
}
#Component(value="aImpl2")
public class AImpl2 implements A{
}
And finally a class that will use an "A" implementation:
#Component
public class MyClass {
#Autowire
A a;
}
Now if I want to inject AImpl1 I add the #Qualifier("aImpl1") while if I want to inject AImpl2 I add #Qualifier("aImpl2")
The question is: Is it possible to instruct spring somehow to look up all implementations of "A" in this case AImpl1 and AImpl2 and use some application specific conventions to choose the most appropriate implementation? for example in this case my convention could be use the implementation with the greatest suffix (i.e. AImpl2)?
EDIT: the class MyClass should not be aware at all about the implementation lookup logic, it should just find its property "a" set with an object of AImpl2.
You can inject all implentations as List:
#Autowired
List<A> as;
or as Map with bean name as key:
#Autowired
Map<String, A> as;
and then choose proper implementation manually (perhaps, in a setter method):
#Autowired
public void setAs(Map<String, A> as) {
this.a = ...;
}
Assuming you already have hundreds of interfaces and implementations (as you said in a comment), and you do not want to refactor all the code... then is a tricky problem... and this is a tricky solution:
You could create a custom BeanDefinitionRegistryPostProcessor and implement either the method postProcessBeanDefinitionRegistry or postProcessBeanFactory.
This way you have access to all bean definitions before they are instantiated and injected. Do your logic to find which is the preferred implementation for each one of your interfaces, and then, set that one as primary.
#Component
public class CustomBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {
#Override
public void postProcessBeanDefinitionRegistry(
BeanDefinitionRegistry registry) throws BeansException {
// this method can be used to set a primary bean, although
// beans defined in a #Configuration class will not be avalable here.
}
#Override
public void postProcessBeanFactory(
ConfigurableListableBeanFactory beanFactory) throws BeansException {
// here, all beans are available including those defined by #configuration, #component, xml, etc.
// do some magic to somehow find which is the preferred bean name for each interface
// you have access to all bean-definition names with: beanFactory.getBeanDefinitionNames()
String beanName = "aImpl2"; // let's say is this one
// get the definition for that bean and set it as primary
beanFactory.getBeanDefinition(beanName).setPrimary(true)
}
}
The hard part is to find the bean name, it depends of the specifics of your application. I guess that having a consistent naming convention will help.
Update:
It seems that both methods in the interface BeanDefinitionRegistryPostProcessor can be used for this purpose. Having in mind that in the postProcessBeanDefinitionRegistry phase, beans configured through #configuration classes are not yet available, as noted in the comments below.
On the other hand they are indeed available in postProcessBeanFactory.
If you have a Configuration class you could use a method in that to make the decision of which implementation of A to return. Then the autowired will inject the appropriate instance for that class.
#Configuration
public class ApplicationConfiguration {
#Bean
A getA() {
// instantiate the implementation of A that you would like to have injected
// or you could use reflection to find the correct class from the classpath.
// return the instance
}
}
This assumes you always want to use the same instance everywhere you are injecting A. If not, then you could have different #Bean annotated methods with names to get different versions.
You can try to use Spring Profiles.