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
1,349 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,470 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
464 views
0 votes
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
213 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,500 points

edited Feb 28, 2019 by Omkar 7,420 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 12 in Java by Roshni
• 6,190 points
616 views
0 votes
1 answer

Bean Scopes in Java Spring

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

answered Jul 3, 2018 in Java by geek.erkami
• 2,680 points
296 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,470 points
6,458 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,890 points
793 views
0 votes
1 answer

The best way to filter a Java Collection?

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

answered May 28, 2018 in Java by Parth
• 4,630 points
205 views
0 votes
1 answer

Bean life cycle in Spring Bean Factory Container

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

answered Aug 29, 2018 in Java by code.reaper12
• 3,470 points
1,095 views
0 votes
1 answer