您的位置:首页 > 运维架构

Get/Set a bean property

2014-01-22 09:30 369 查看
This is an example of how to get and set a bean property. We are using theStatement class.
A Statement object represents a primitive statement in which a single method is applied to a target and a set of arguments. In order to get and set a bean property you should:

Create a simple class,like
Bean
class in the example. It has two String properties and getters and
setters for the properties.
Create a new object of
Bean
class.
Create a new Statement object
for the specified object to invoke the
setProperty1
method and by a String array of arguments.
Call the
execute()
API method of Statement. It finds a method whose name is the same as the
methodName
property,
and invokes the method on the Bean object.
Create a new Expression.
An Expression object represents a primitive expression in which a single method is applied to a target and a set of arguments to return a result. The new Expression is created with the specified value for the specified
Bean
object
to invoke the
getProperty1
method by the array of arguments.
Call
execute()
API method of Expression. It finds a method whose name is the same as the
methodName
property,
and invokes the method on the
Bean
object.
Call
getValue()
API method of Expression to get the value of the property set in the Expression.

Let’s take a look at the code snippet that follows:

view
source

print?

01
package
com.javacodegeeks.snippets.core;
02
03
import
java.beans.Expression;
04
import
java.beans.Statement;
05
06
public
class
GetSetBeanProperty
{
07
08
public
static
void
main(String[]
args)
throws
Exception
{
09
10
Object
o =
new
Bean();
11
12
Statement
stmt;
13
Expression
expr;
14
15
//
Set the value of property1
16
stmt
=
new
Statement(o,
"setProperty1"
,
new
Object[]{
"My
Prop Value"
});
17
stmt.execute();
18
19
//
Get the value of property1
20
expr
=
new
Expression(o,
"getProperty1"
,
new
Object[
0
]);
21
expr.execute();
22
System.out.println(
"Property1
value: "
+
(String)expr.getValue());
23
24
/////////////////////////////////////////////
25
26
//
Set the value of property2
27
stmt
=
new
Statement(o,
"setProperty2"
,
new
Object[]{
new
Integer(
345
)});
28
stmt.execute();
29
30
//
Get the value of property2
31
expr
=
new
Expression(o,
"getProperty2"
,
new
Object[
0
]);
32
expr.execute();
33
System.out.println(
"Property2
value: "
+
(Integer)expr.getValue());
34
35
}
36
37
public
static
class
Bean
{
38
39
//
Property property1
40
private
String
property1;
41
//
Property property2
42
private
int
property2;
43
44
public
String
getProperty1() {
45
return
property1;
46
}
47
public
void
setProperty1(String
property1) {
48
this
.property1
= property1;
49
}
50
51
public
int
getProperty2()
{
52
return
property2;
53
}
54
public
void
setProperty2(
int
property2)
{
55
this
.property2
= property2;
56
}
57
58
}
59
60
}
Output:
Property1 value: My Prop Value Property2 value: 345


This was an example of how to get and set a bean property in Java.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: