Skip to content

Instantly share code, notes, and snippets.

@moelholm
Created October 10, 2016 18:13
Show Gist options
  • Save moelholm/bea5b014d45f2d3cccd7efdd01118a38 to your computer and use it in GitHub Desktop.
Save moelholm/bea5b014d45f2d3cccd7efdd01118a38 to your computer and use it in GitHub Desktop.
Spring Framework needs @Nonbinding like support
package com.moelholm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class GreeterService {
@Autowired
@Uppercase("hElLo WoRlD")
private String upper;
@Autowired
@Uppercase("hElLo WoRlD")
private String lower;
public String sayHello(String caller) {
return String.format("%s (%s), %s", upper, lower, caller);
}
}
package com.moelholm;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
import org.springframework.beans.factory.InjectionPoint;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
public class MyBeanConfig {
@Bean
@Uppercase("hElLo WoRlD")// Spring needs @Nonbinding so that we can remove this 'value'
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public String uppercase(InjectionPoint ip) {
Uppercase annotation = findAnnotation(ip.getAnnotatedElement(), Uppercase.class);
return annotation.value().toUpperCase();
}
@Bean
@Lowercase("hElLo WoRlD")// Spring needs @Nonbinding so that we can remove this 'value'
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public String lowercase(InjectionPoint ip) {
Lowercase annotation = findAnnotation(ip.getAnnotatedElement(), Lowercase.class);
return annotation.value().toLowerCase();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment