Injecting property value into Spring Bean using annotations

0 votes

In my Spring app, I have beans configured using annotations, for e.g:

@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {
    // .......
}

In the XML file, I have a bean called “PropertyPlaceholderConfigurer” defines as:

<bean id="propertyConfigurer" 
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="loc" value="/WEB-INF/app.properties" />
</bean>

Now, from the app.properties I need to inject a property in the above bean. I tried doing the following:

<bean class="com.example.PersonDaoImpl">
    <property name="maxResults" value="${results.max}"/>
</bean>

It's not working, I think the reason behind this is PersonDaoImpl is not a feature in the Spring XML file and is picked up from the classpath using annotations.

So, I tried the below code, but still, I am not able to access the property from PropertyPlaceholderConfigurer.

@Repository("personDao")
public class PersonDaoImpl extends AbstractDaoImpl implements PersonDao {

    @Resource(name = "propertyConfigurer")
    protected void setProperties(PropertyPlaceholderConfigurer ppc) {
    // How to access results.max? 
    }

Can someone suggest how do I access it?

May 29, 2018 in Java by anto.trigg4
• 3,440 points
2,706 views

1 answer to this question.

0 votes

I think you can easily perform this operation in Spring 3 as it provides EL support. Follow the below:

@Value("#{systemProperties.databaseName}")
public void setDatabaseName(String db) { ... }

@Value("#{strategyBean.databaseKeyGenerator}")
public void setKeyGenerator(KeyGenerator keygen) { ... }

Here,

Systemproperties: It is an implicit object.

Strategybean: It is the name of a bean.

Similarly, when you want to access some property from a Properties object, you can use @Value to its fields:

@Value("#{myProperties['github.oauth.clientId']}")
private String githubOauthClientId;
Hope this clears your doubt.

answered May 29, 2018 by code.reaper12
• 3,500 points

Related Questions In Java

+1 vote
0 answers

Initially autowired object has value,it becomes null after paging(using JSF 2.2 and spring 4.01.)

i'm trying to create basic crud operations ...READ MORE

Jul 5, 2019 in Java by swats
1,655 views
+1 vote
1 answer

How to find even or odd using call by value?

Call by value is, when a primitive ...READ MORE

answered Nov 23, 2018 in Java by Namitha
1,503 views
0 votes
1 answer

How does Java convert integer value into byte?

When an integer value is converted into ...READ MORE

answered Feb 28, 2019 in Java by Avantika
• 1,520 points

edited Feb 28, 2019 by Omkar 18,404 views
0 votes
1 answer

how to read csv file form sftp connection and store into string object in java code and convert into json.....post it using rest api

Hey, @Pooja, Before starting with anything you should ...READ MORE

answered May 13, 2020 in Java by Roshni
• 10,480 points
3,833 views
0 votes
1 answer

Bean Scopes in Java Spring

According to my knowledge, the Spring Framework ...READ MORE

answered Jul 4, 2018 in Java by geek.erkami
• 2,680 points
1,191 views
0 votes
1 answer

DTO vs VO vs POJO vs JavaBeans

Data Transfer Object is used for transferring a collection ...READ MORE

answered Aug 14, 2018 in Java by code.reaper12
• 3,500 points

edited Oct 7, 2021 by Sarfaraz 11,554 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,920 points
1,919 views
0 votes
1 answer

The best way to filter a Java Collection?

 this problem is solved using streams and ...READ MORE

answered May 29, 2018 in Java by Parth
• 4,640 points
834 views
0 votes
1 answer

Bean life cycle in Spring Bean Factory Container

Bean life cycle in Spring Bean Factory ...READ MORE

answered Aug 30, 2018 in Java by code.reaper12
• 3,500 points
3,298 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP