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

How to initialize log4j2 using log4j.configurationFile in java-vm-args of Java Web Start application (JNLP)
The Anonymous Visitor
How to specify multiple input paths to a Dataflow job
How to reduce response time while using hibernate
How to use version in REST API with spring mvc?
How to Group Documents by a Field and Calculate Count in mongodb using java [closed]
Java: Applying probability constraints on Random() [closed]
Testng Absent Code attribute in method that is not native or abstract in class file
convert “Nested ArrayList XML” to java Object using Two Custom Converters of XStream
Java 8 Stream - check if instanceof
elastic search not x content exception
php script to java for TrafficFactory api
Unable to get JDBC connection with PostgreSQL
Java Enum data long value being set to zero
java xuggler leads to fatal error
How to notify client-side of an Instant-Message (IM) app of updates?

Categories

HOME
maven
winforms
clips
keras
dictionary
nullpointerexception
smarty
relative-path
syntax
electron
octobercms
cvs
razor
spring-cloud-stream
amortized-analysis
ckeditor
autotools
mapserver
circular-dependency
designer
finite-automata
reactive-cocoa
interop
karma-jasmine
emulator
conemu
amazonsellercentral
telerik-reporting
language-agnostic
applozic
neo4j-spatial
log4js-node
r-raster
bluestacks
tinymce-4
stringtemplate
nssegmentedcontrol
android-ble
overlap
simplexml
dynamics-crm-2013
streamsets
c11
hilbert-curve
hue
trim
ncalc
jvm-languages
estimote
bootstrap-dialog
import-from-excel
netcdf4
auto-update
osx-mavericks
termination
dandelion
return-value
integrity
qtwebview
pillow
epson
mako
google-cdn
pintos
parallel-data-warehouse
deadbolt-2
color-picker
vmware-tools
mikroc
asp.net-4.5
transmitfile
nsviewcontroller
react-native-listview
pagerank
vhd
wso2cloud
reactive-banana
intel-fortran
endeca-workbench
icu4j
qdialog
content-length
tarjans-algorithm
iiviewdeckcontroller
remobjects
p2
codeigniter-routing
java-metro-framework
titanium-modules
openexr
has-many-through
elliptic-curve
rtmfp
bundles
ember-charts
intentservice
django-nonrel
xamlparseexception
pygit2
distutils
selected
simba
chuck
rdoc
funscript
ocx
buildr
dotnethighcharts
vdsp
infobox
table-footer
inotifycollectionchanged
jquery-ui-droppable
data-driven
nerddinner
defensive-programming
document-conversion
ntvdm.exe

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App