In a Spring 4 application, we are extending PropertySourcesPlaceholderConfigurer to do some custom task during resolving properties.
To register our new class (MyPropertyConfigurer) o Spring we use the following class:
#Configuration
public class AppConfig {
#Bean
public static PropertySourcesPlaceholderConfigurer
propertySourcesPlaceholderConfigurer() {
return new MyPropertyConfigurer();
}
}
As a matter of curiosity, can I add my new bean to Spring with spring.xml instead of using the #Configuration annotation?
I had the same problem, my problem was fixed simply with placing the bean of my class in my Springbeans.xml file like below:
<beans:bean class="MyPropertyConfigurer"/>
You can very well use a combination of both Spring based xml and java based config in your spring application.
You have to use
#ImportResource({"classpath:sampleapp.xml"})
along with
#Configuration
Look at the sample post below which explains it:
Mixing xml and java config with spring
Related
I have a simple question about PropertySourcesPlaceholderConfigurer and #PropertySource annotation. I have simple class with bean. I would like to inject property from application.properties and some another test.properties wiht #Value annotation. I read in several sources that #PropertySource needs to define staticPropertySourcesPlaceholderConfigurer bean. From documentation:
In order to resolve ${...} placeholders in definitions or
#Value annotations using properties from a PropertySource, one must
register a PropertySourcesPlaceholderConfigurer. This happens
automatically when using in XML, but
must be explicitly registered using a static #Bean method when using
#Configuration classes.
But for me it works fine without this bean. Is Spring in some way auto configures PropertySourcesPlaceholderConfigurer at application startup? Here is my simple example (first property is from application.properties and second from test.properties):
#Configuration
#PropertySource("classpath:test.properties")
public class AppConfiguration {
#Value("${first.property}")
private String firstProp;
#Value("${second.property}")
private String secondProp;
#Bean
public TestModel getModel() {
TestModel model = new TestModel();
model.setFirstProperty(firstProp);
model.setSecondProperty(secondProp);
return model;
}
}
The static PropertySourcesPlaceholderConfigurer bean is registered automatically when missing by PropertyPlaceholderAutoConfiguration class which appears to be introduced in Spring Boot 1.5.0.RELEASE (as there is no on-line javadoc for it in the previous releases).
Interestingly this new auto configuration is not mentioned in Spring Boot 1.5 Release Notes.
Given the following Java class
#Order(12)
#Component
public class MyComponent {
//....
}
what is the equivalent in the Spring XML configuration? I couldn't find anything matching the #Order annotation for the XML based configuration:
<bean class="MyComponent" />
In spring you have 2 choices:
annotation
interface implementation
In your case you will have to go with the second option.
Your class needs to implement Ordered, but this will bind your class with spring API. It's same when using annotation over class.
But if you are using configuration classes, instead of xml config, then you can have plain java beans, and keep all Spring API in configurations.
Example:
#Bean(destroyMethod = "shutdown")
#Order(12)
public ScheduledExecutorService scheduledExecutorService() {
return Executors.newSingleThreadScheduledExecutor();
}
Configuration classes give you the option to separate Spring API(annotations) from your beans.
I have a SpringBoot main application, as well as a separate Maven module project that compiles as a separate Jar. The module has a Spring config class annotated with #Configuration, which I want to get loaded, when the main app loads.
Apparently, this does not happen out of the box (by just including the module to the main app). What else do I need to do, to get the module configuration class also get loaded by the main app?
The easiest way is to scan the package that the #Configuration class is in.
#ComponentScan("com.acme.otherJar.config")
or to just load it as a spring bean:
#Bean
public MyConfig myConfig() {
MyConfig myConfig = new MyConfig ();
return myConfig;
}
Where MyConfig is something like:
#Configuration
public class MyConfig {
// various #Bean definitions ...
}
See docs
#ComponentScan annotation will scan all classes with #Compoment or #Configuration annotation.
Then spring ioc will add them all to spring controlled beans.
If you want to only add specific configurations, you can use #import annotation.
example:
#Configuration
#Import(NameOfTheConfigurationYouWantToImport.class)
public class Config {
}
#Import Annotation Doc
I am using spring framework 4.1.4
I have JBoss5XmlWebApplicationContext as my contextClass reading xml configuration.
I want to add #Configuration class to be read aside from the xml.
Is this possible? How to do it?
Yes, Its possible. One way I know is using springboot classes. I am using MySpringConfiguration class to declare beans using #Configuration and MySpringConfig.xml for xml based bean configuration. I am able to load beans from the context coming from both sources.
Check below code:
public static void main(String[] args) {
Resource res = new FileSystemResource("E:\\MySpringConfig.xml");
SpringApplication app = new SpringApplication(MySpringConfiguration.class,res);
ApplicationContext ctx = app.run("");
MyXMLBean myXMLBean = (MyXMLBean)ctx.getBean("myNewBean"); // From XML file...
GeData geData= (GeData)ctx.getBean("geData"); // From Java Configuration ...
....
Configuration class goes like this
#Configuration
public class MySpringConfiguration {
#Bean
public GeData geData(){
return new GeData();
}
Hope this helps...
I'm working on updating an older Spring MVC application from xml based to annotation based configuration. I'm not sure how to add "name-generator" to the #ComponentScan annotation.
Here's what I have:
#Configuration
#EnableAsync
#EnableScheduling
#EnableAspectJAutoProxy
#ComponentScan({"com.styxrbad", "com.styxrbad.common"})
#Import({DatabaseConfiguration.class, WebMvcConfiguration.class})
public class SpringConfiguration
{
}
I need to include my implemented BeanNameGenerator to replace the "name-generator" field from the xml within the annotations. I'm rather new to Spring and I do not know the syntax nor can I find any examples in my research. How can I do this?
#ComponentScan has a property nameGenerator.
Is it enough for you ?
UPDATE:
#ComponentScan(value = {"com.styxrbad", "com.styxrbad.common"}, nameGenerator = MyBeanNameGenerator.class)