Under what circumstances #Bean excute before #PostConstruct? - spring

i met a urger issue, the anotain #Bean method execute before the #PostConstruct method in the same class,
in spring boot oauth2. there is a class named as
AuthorizationServerEndpointsConfiguration.java
There are 2 methods in it.
#PostConstruct
public void init() {
xxxx
}
}
#Bean
public AuthorizationServerTokenServices defaultAuthorizationServerTokenServices() {
return endpoints.getDefaultAuthorizationServerTokenServices();
}
According to my understanding,the init shuold run before defaultAuthorizationServerTokenServices method, but when i debug it in IDEA, the defaultAuthorizationServerTokenServices before init.
As below config can to reproduce this issue.
#Configuration
#Import({ ClientDetailsServiceConfiguration.class, AuthorizationServerEndpointsConfiguration.class })
class MyAuthorizationServerConfigurer extends AuthorizationServerConfigurerAdapter {
.....
}
Could you tell me what's the reason?
Thanks,

We had this issue when debugging a configurer with #RefreshScope.
When we dropped the RefreshScope lazy loading it appeared to fix the issue
// #RefreshScope
public class MyAuthorizationServerConfigurer extends AuthorizationServerConfigurerAdapter {
}

Related

Spring #Bean - creation of Spring Bean depends on service class name

I use Spring 4.2.8 and I do have the service class below. If this class has the name ScheduleEmailCreateAndSendServiceImpl than everything works fine (method generalEmailMessage is invoked at start time in order to create the Spring Bean)
If I rename this class to EmailCreateAndSendServiceImpl than method generalEmailMessage will not be invoked at start time - does anyone know why?
#Service("emailCreateAndSendService")
public class ScheduleEmailCreateAndSendServiceImpl extends AbstractService
implements EmailService {
protected EmailMessage generalMessage;
#Override
public void createAndSendMessage(String receiver, boolean emailActive, Object[] values) throws BusinessException {
// create and send email
}
#Bean
public EmailMessage generalEmailMessage() {
this.generalMessage = new GeneralEmailInformationMessage();
return generalMessage;
}
}
[EDIT]
with this code it is the same
#Configuration
public #Data class ScheduleGeneralEmailConfiguration {
protected EmailMessage generalMessage;
public ScheduleGeneralEmailConfiguration() {
System.out.println("asdf");
}
#Bean
public EmailMessage generalEmailMessage() {
this.generalMessage = new GeneralEmailInformationMessage();
return generalMessage;
}
}
#Bean annotated methods should be in #Configuration annotated class.
You can also put the #Bean annotated methods in the main class of the Spring Boot application annotated with #SpringBootApplication which encapsulates #Configuration, #EnableAutoConfiguration, and #ComponentScan annotations.
Make sure your #Configuration annotated class is placed in the same package or sub package of the Spring Boot Application class

Spring boot defining configuration beans per user

I am using Spring boot. I have some question regarding the spring boot beans.
But I have doubt
I use bean which are default scope that is singleton. So they will have only one instance per application.
#Configuration
public class ...{
#Bean
public void method() {}
}
And
Now i use bean which scope is prototype. So they will have each instance per request.
#Configuration
public class ...{
#Bean
#Scope("prototype")
public void method() {}
}
But
I want single instance per user..? all request use single instance per user.
#Configuration
class Abc {
#Bean
#Scope("session")
public YourBean getYourBean() {
return new YourBean();
}
}
You will need to define one singleton bean with a property using prototype bean:(xml example)
With #bean definition:
#Component
#Scope("singleton")
public class SingletonBean {
// ..
#Autowired
private PrototypeBean prototypeBean;
//..
}
#Component
#Scope("prototype")
public class PrototypeBean {
//.......
}
Example: https://www.baeldung.com/spring-inject-prototype-bean-into-singleton

#Profile Spring Annotation in Camel

I have a Spring Boot + Apache Camel project that works brilliantly. I just added a new bean though where I wanted to have its implementation be profile-specific. I created Spring tests to verify it, and it works as expected, but when I run the server I get the following stack trace:
Caused by: org.apache.camel.NoSuchBeanException: No bean could be found in the registry for: MyFancyBean
at org.apache.camel.component.bean.RegistryBean.getBean(RegistryBean.java:94)
at org.apache.camel.model.language.MethodCallExpression.createExpression(MethodCallExpression.java:196)
at org.apache.camel.model.language.MethodCallExpression.createPredicate(MethodCallExpression.java:210)
at org.apache.camel.model.language.ExpressionDefinition.createPredicate(ExpressionDefinition.java:148)
at org.apache.camel.model.ValidateDefinition.createProcessor(ValidateDefinition.java:63)
at org.apache.camel.model.ValidateDefinition.createProcessor(ValidateDefinition.java:35)
at org.apache.camel.model.ProcessorDefinition.makeProcessorImpl(ProcessorDefinition.java:545)
at org.apache.camel.model.ProcessorDefinition.makeProcessor(ProcessorDefinition.java:506)
at org.apache.camel.model.ProcessorDefinition.addRoutes(ProcessorDefinition.java:222)
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1068)
I have an interface and two implementations:
public interface MyFancyBean { ... }
public class FooFancyBean implements MyFancyBean { ... }
public class NonFooFancyBean implements MyFancyBean { ... }
Depending on profile, the correct bean is read instantiated:
#Configuration
public class AppConfig {
#Bean
#Profile("foo")
MyFancyBean fooBean() {
return new FooFancyBean();
}
#Bean
#Profile("!foo")
MyFancyBean nonFooBean() {
return new NonFooFancyBean();
}
}
I've verified this works a couple of ways. First, a couple tests:
#ActiveProfiles("anything-but-foo")
#RunWith(SpringJUnit4ClassRunner.class)
#ComponentScan(basePackages = {"com.example", "com.jtv.spring.boot"})
#EnableAutoConfiguration
#Component
public class NonFooBean_SpringTest {
#Autowired
private MyFancyBean bean;
#Test
// ... here "bean" is instantiated as "NonFooFancyBean"
So the test works.
Further, when I start my app, depending on profile the correct bean in my #Configuration class above is called.
But Camel is still angry and says "NoSuchBeanException" on startup.
FWIW, here's how I'm referencing the bean:
#Component
public class MyCamelRoute extends RouteBuilder {
#Override
public void configure() throws Exception {
// [...]
from("direct:processStuff").
validate().method("MyFancyBean").
process("MyProcessor");
}
}
How do I get Camel to honor this config?
Whoooo... Y'all get to be my rubber duck today. I just autowired it. (This doesn't work for my processor, which is why it didn't occur to me initially.)
#Component
public class MyCamelRoute extends RouteBuilder {
#Override
public void configure() throws Exception {
// [...]
#Autowired MyFancyBean myFancyBean;
from("direct:processStuff").
validate().method(myFancyBean).
process("MyProcessor");
}
}

Spring 4, MongoDB, using #Value in MongoConfiguration

I use a MongoConfiguration class to setup my Sping 4 MongoDB. I want to read properties from application.properties so I use #Value:
....
#Configuration
#EnableMongoRepositories
#ComponentScan(basePackageClasses = {Application.class})
public class MongoConfiguration extends AbstractMongoConfiguration {
#Value("${mongodb.host}")
String mongodb_host;
#Value("${mongodb.port}")
int mongodb_port;
#Value("${mongodb.databasename}")
String mongodb_databasename;
#Override
protected String getDatabaseName() {
return mongodb_databasename;
}
#Override
public Mongo mongo() throws Exception {
return new MongoClient( mongodb_host, mongodb_port );
}
#Override
protected String getMappingBasePackage() {
return "com.example.mongodb01";
}
}
This works fine for a web application -- but when I try the same idea in a command line Java application it fails (it's as if the application.properties was found but #Value never ran). I know I am reading the applications.properties file OK. It must have something to do with the differences in running in a servlet container vs. an application but after much searching and trials I have not been able to resolve this and fix it. I would appreciate any help on this -- Thank you!
I did see a similar question and I tried adding the below to my MongoConfiguration but still had the same problem:
#Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
Did you include the #PropertySource
#Configuration
#PropertySource("classpath:application.properties")
#EnableMongoRepositories
#ComponentScan(basePackageClasses = {Application.class})
public class MongoConfiguration extends AbstractMongoConfiguration {
...

How to configure Services with Spring Data JPA repositories with spring Java Configuration

I am trying to figure out how to get a hold of the OrderRepository so that I can pass it into the constructor of the OrderServiceImpl using Spring's java configuration (I already know how to do it with xml configuration).
#Configuration
#ComponentScan(basePackages = "com.sample.app")
#EnableJpaRepositories("com.sample.app")
#EnableTransactionManagement
public class AppConfig
{
#Bean
public OrderService orderService()
{
return new OrderServiceImpl(orderRepository());
}
#Bean
public OrderRepository orderRepository()
{
return ??? What goes here ???
}
...
}
#Configuration
#ComponentScan(basePackages = "com.sample.app")
#EnableJpaRepositories("com.sample.app")
#EnableTransactionManagement
public class AppConfig {
#Autowired
private OrderRepository orderRepository;
#Bean
public OrderService orderService() {
return new OrderServiceImpl(orderRepository);
}
}
Something like that should work. Or simply put a field inside your OrderServiceImpl which is annotated with #Autowired and remove the constructor which takes an orderRepository. Or rely on component-scanning and remove the #Bean methods all together.
You have a component-scan and #Bean method, you might run into duplicate instances of your service that way (if it is annotated with #Service).

Resources