java


Spring: How to inject a String bean to the constructor?


I have a class Config:
Config.java
public class Config {
private final String p = "Prop";
#Bean
public String getP(){return p;}
}
How do I inject this to some constructor, ie:
public class SomeC {
private String p;
public SomeC(String p) {
this. p = p;
}
}
I want this String p to have injected value from Config. Is that possible?
You will have to name the bean, and then use the #Qualifier annotation when autowiring referencing that name.
Example:
Config.java
public class Config {
private final String p = "Prop";
#Bean(name="p")
public String getP(){return p;}
}
SomeC.java
public class SomeC {
private String p;
#Autowired
public SomeC(#Qualifier("p") String p) {
this. p = p;
}
}
You should use #Autowired either like this:
#Autowired
public SomeC(String p){
this.p = p;
}
or like this:
public class SomeC {
#Autowired
private String p;
}
I prefer the second approach, but this makes the classes more difficult to test, since the dependencies are not visible from the outside, so use with caution.

Related Links

Does removing annotation can cause runtime crash?
How can I update Task status and the Actuals of a specific task?
using prolog & clpr for a system of constraints
Remove characters from consecutive runs of the same character in a String using Java 1.8
How to focus on currently selected item in Listview in android
Does equals apply on itself when looking an element in a list?
Java PriorityQueue behaviour with 3 elements in the Queue [duplicate]
Unable to resolve place holder in Mockito Unit test
WebParam.Mode.OUT meaning and documentation
Gradle runs all tests when only a few tests changes
Android Firabse Cloud Message GoogleSignatureVerifier: com.google.android.gms signature not valid
HtmlElement not showing child nodes but HtmlElement.asXml() method does
Spark steaming read from Kafka and apply Spark SQL aggregations in Java
Trouble understanding constructors and reasons for calling them in another class
simulated annealing for solving concentrator location prob
Is postfix self assignment behaviour strictly defined in C#/Java?

Categories

HOME
wso2-am
variables
date
gitlab
deployment
vbscript
push-notification
hashmap
read-eval-print-loop
microservices
ezpublish
yum
autotools
primary-key
ssl-client-authentication
kentor-authservices
modelica
static-libraries
aurigma
http-status-code-504
undefined
samoa
lucene.net
predix
physics-engine
orleans
google-search-api
restful-authentication
fgetcsv
vsts-build-task
protovis
overriding
neuroscience
squib
g-code
janrain
unixodbc
convertapi
avconv
scrollable
dotnetzip
nstextview
slickedit
worker-thread
media-player
zendesk-app
gcsfuse
imanage
jspdf-autotable
account-kit
query-performance
specrun
google-feed-api
py2app
qcustomplot
rhino-servicebus
xpath-1.0
wapiti
freedesktop.org
maven-tomcat-plugin
django-unittest
quicklisp
security-testing
nsight
ami
mmc
key-management
c++03
bigbluebutton
facebook-graph-api-v2.4
modalpopup
rake-task
typekit
coldbox
client-side-templating
mesa
access-rights
web2py-modules
ruby-datamapper
dataservice
html4
enterprisedb
twrequest
viewswitcher
jspinclude
buildr
bigcouch
self-extracting
table-footer
remember-me
ncqrs
ti-dsp
phonon
anti-piracy
pascal-fc
putchar
misv

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App