Bean; import org. Configuration; import org. PropertySource; Configuration PropertySource "classpath:sport. Bean ;. Configuration ;. PropertySource ;. AnnotationConfigApplicationContext ;.
Previous Next? Pin It on Pinterest. Download Source Code: how-to-inject-properties-file-data-to-bean-in-spring. Write For Us. How to inject properties file data to bean in Spring By Atul Rai Last Updated: September 2, Previous Next This tutorial will help you to inject the properties file data to the bean or class. Let's assume that we have to inject some property values to a UrlBuilder object. This object needs three property values:.
These property values are used when the UrlBuilder object builds url addresses that are used to access different functions of our web application. If we inject these property values by using constructor injection and the Value annotation, the source code of the UrlBuilder class looks as follows:. If we inject these property values by using constructor injection and the Environment class, the source code of the UrlBuilder class looks as follows:. I admit that this doesn't look so bad.
If we inject individual property values directly into the beans that need them, and more than one bean A and B need the same property value, the first thing that comes to our mind is to specify the property names in both bean classes:. We can fix this problem by moving the property names to a constant class.
If we do this, our source code looks as follows:. This fixes the maintenance problem but only if all developers remember to use it. We can of course enforce this by using code reviews, but this is one more thing that the reviewer must remember to check. Let's assume that we have two classes A and B which need the value of the app.
If we inject this property value directly into the A and B beans, and we want to ensure that the value of that property is 'http' or 'https', we have to either. If we add the validation logic to both bean classes, the source code of these classes looks as follows:. This is a maintenance problem because A and B classes contain copy-paste code.
We can improve the situation a bit by moving the validation logic to a utility class and using it when we create new A and B objects.
The problem is that we still have to remember to invoke this utility method. We can of course enforce this by using code reviews, but once again, this is one more thing that the reviewer must remember to check. Everyone of these options causes problems because we cannot use them at the same time we are writing code that requires property values found from our properties files. If we need to read our documentation, we have to open "an external document" and this causes a context switch that can be very expensive.
We can solve the problems mentioned earlier by injecting the property values into configuration beans. The first thing that we have to do is to create a properties file.
The properties file of our example application is called application. Our next step is to create the configuration bean classes and inject the property values found from our properties file into them. First , we have to create the WebProperties class. Dependencies This post was written with Spring 5.
Configuration ComponentScan PropertySource "app. Assert ; import org. Test ; import org. RunWith ; import org. Value ; import org.
0コメント