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
720 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
127 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
122 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 1,908 views
0 votes
2 answers

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

If you require a time stamp in ...READ MORE

answered Aug 23, 2019 in Java by Sirajul
• 44,760 points
144 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,660 points
131 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
3,674 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,880 points
640 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,640 points
96 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
643 views
0 votes
1 answer