Panda.Ioc - What can you inject
Where to inject?
There are two entries where you can inject values into the object.
- Constructor parameters
- Property
Inject parameters into the constructor
Your JSON configuration file will be like this:
{
tom : {
type : 'panda.demo.ioc.json.Pet',
args : ['Tom']
}
}
The value of args is an array where each element will correspond to a parameter of the constructor. Of course, you must ensure that you have such a constructor.
Injecting parameters into properties
Your JSON configuration file will be like this:
{
tom : {
type : 'panda.demo.ioc.json.Pet',
fields : {
name : 'Tom'
}
}
}
If you do not need to write type, then you can use shorthand mode:
{
tom: { name: 'Tom' }
}
The value can be more than just a string, it can also be of the following type.
Boolean
{
tom : { dead: true }
}
Number
{
tom : { age: 24 }
}
Anonymous Object
{
tom : {
friend: {
type : 'panda.demo.ioc.json.Pet',
fields : { name : 'Jerry' }
}
}
}
For anonymous objects, see here.
Reference
{
tom : { friend: '#jerry' }
}
friend: ‘#jerry’ will refer to another object named “jerry” in the container
IOC container ifself
{
tom : { myIoc : '#$Ioc' }
}
A special reference, case insensitive, the value is the Ioc container itself
Object Name
{
tom : { beanName : '#$Name'}
}
A special reference, case insensitive, the value is the name of the object, ie “tom”
IOC Container Context
{
tom : { myContext : '#$Ctx' }
}
A special reference, case insensitive, the value is the context (interface panda.ioc.IocContext) of the current IOC container
JSON Object/Array
If your object’s field is an array, collection, or Map, it’s natural to set its value using JSON, isn’t it?
{
tom : { favorites: [ 'banana', 'bug' ] }
}
or
{
tom : { favorites: "![ 'banana', 'bug' ]" }
}
EL Expression
This is an extremely flexible injection method that allows you to do almost anything. Because it allows you to directly call a Java method.
For a more detailed description, see EL Expression.
Here are just a few of the major ways of using:
Static Property
{
tom : { staticField : "${'com.my.SomeClass'@staticPropertyName}" }
}
Static Method
{
tom : { staticField : "${'com.my.SomeClass'@staticFunc()}" }
}
Static Method with parameters (parameters can be any type)
{
tom : { staticField : "${'com.my.SomeClass'@staticFunc('param', true)}" }
}
Object in the IOC container
{
tom : { friend: '${jerry}' },
jerry : { type : 'com.my.Pet' }
}
Property of the object in the IOC container
{
tom : { oneField : '${jerry.name}' } ,
jerry : { type: 'com.my.Pet'}
}
Method’s return value of the object in the IOC container
{
tom : { oneField : '${jerry.getXXX()}' } ,
jerry : { type: 'com.my.Pet'}
}
Method’s return value of the object in the IOC container (with parameters)
{
tom : { oneField : '${jerry.getXXX("some string", true, 34)}' } ,
jerry : { type: 'com.my.Pet'}
}