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,420 points
469 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,450 points

Related Questions In Java

+1 vote
0 answers
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
51 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 in Java by Avantika
• 1,500 points

edited Feb 28 by Omkar 113 views
0 votes
1 answer

How do I get the current date and time using Java?

To print current date and time using ...READ MORE

answered Apr 13, 2018 in Java by Akrati
• 3,150 points
55 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,320 points
65 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,450 points
1,489 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,840 points
587 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 parth295
• 4,600 points
53 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,450 points
303 views
0 votes
1 answer