1.setter 注入 :
beans.xml:
<?
xml version="1.0" encoding="UTF-8"
?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<
beans
xmlns
="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop
="http://www.springframework.org/schema/aop"
xmlns:tx
="http://www.springframework.org/schema/tx"
xsi:schemaLocation
="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>
<
bean
name
="userDao"
class
="com.linying.dao.UserDao"
></
bean
>
<
bean
name
="userDomain"
class
="com.linying.domain.User"
></
bean
>
<
bean
name
="userAction"
class
="com.linying.service.UserAction"
>
< property name ="user" ref ="userDomain" ></ property >
< property name ="userDao" ref ="userDao" ></ property >
</
bean
>
</
beans
>
User.java:
package
com.linying.domain;
/**
* 领域对象
*
*
@author
Ying-er
* @time 2010-6-20 上午10:01:05
* @mail [email protected]
*
@version
1.00
*/
public
class
User {
private
int
id;
private
String name;
private
String password;
public
User() {
System.out.println(
"
User构造
"
);
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name
=
name;
}
public
String getPassword() {
return
password;
}
public
void
setPassword(String password) {
this
.password
=
password;
}
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id
=
id;
}
}
UserDao.java:
package
com.linying.dao;
import
com.linying.domain.User;
/**
* UserDao
*
*
@author
Ying-er
* @time 2010-6-20 上午10:17:55
* @mail [email protected]
*
@version
1.00
*/
public
class
UserDao {
public
UserDao() {
System.out.println(
"
UserDao构造
"
);
}
public
void
insert(User user) {
System.out.println(
"
执行insert方法
"
);
}
}
UserAction.java:
package
com.linying.service;
import
com.linying.dao.UserDao;
import
com.linying.domain.User;
/**
* UserAction
*
*
@author
Ying-er
* @time 2010-6-20 上午10:28:16
* @mail [email protected]
*
@version
1.00
*/
public
class
UserAction {
private
User user;
private
UserDao userDao;
public
UserAction() {
System.out.println(
"
UserAction构造
"
);
}
/**
* 保存User对象
*
*/
public
void
saveUser() {
userDao.insert(user);
System.out.println(
"
saveUser方法被调用
"
);
}
public void setUser(User user) {
this .user = user;
System.out.println( " setUser方法被调用 " );
}
public void setUserDao(UserDao userDao) {
this .userDao = userDao;
System.out.println( " setUserDao方法被调用 " );
}
}
测试——Main.java:
package
com.linying;
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
com.linying.service.UserAction;
/**
* 测试程序入口
*
*
@author
Ying-er
* @time 2010-6-20 上午11:20:24
* @mail [email protected]
*
@version
1.00
*/
public
class
Main {
/**
*
@param
args
*/
public
static
void
main(String[] args) {
//
TODO Auto-generated method stub
BeanFactory bf
=
(BeanFactory) (
new
ClassPathXmlApplicationContext(
"
beans.xml
"
));
UserAction userAction
=
(UserAction) bf.getBean(
"
userAction
"
);
userAction.saveUser();
}
}
控制台输出:
UserDao构造
User构造
UserAction构造
setUser方法被调用
setUserDao方法被调用
执行insert方法
saveUser方法被调用
2.静态工厂注入 :
修改beans.xml:
< bean name ="userDao" class ="com.linying.factory.DaoFactory" factory-method ="getUserDao" ></ bean >
<
bean
name
="userDomain"
class
="com.linying.domain.User"
></
bean
>
<
bean
name
="userAction"
class
="com.linying.service.UserAction"
>
<
property
name
="user"
ref
="userDomain"
></
property
>
<
property
name
="userDao"
ref
="userDao"
></
property
>
</
bean
>
增加工厂:
package
com.linying.factory;
import
com.linying.dao.UserDao;
/**
* Dao静态工厂
*
@author
Ying-er
* @time 2010-6-20 下午04:23:25
* @mail [email protected]
*
@version
1.00
*/
public
class
DaoFactory {
public
static
UserDao getUserDao(){
return
new
UserDao();
}
}
控制台输出:
UserDao构造
User构造
UserAction构造
setUser方法被调用
setUserDao方法被调用
执行insert方法
saveUser方法被调用
3.实例工厂注入 :
修改beans.xml:
< bean name ="daoFactory" class ="com.linying.factory.DaoFactory" ></ bean >
< bean name ="userDao" factory-bean ="daoFactory" factory-method ="getUserDao" ></ bean >
<
bean
name
="userDomain"
class
="com.linying.domain.User"
></
bean
>
<
bean
name
="userAction"
class
="com.linying.service.UserAction"
>
<
property
name
="user"
ref
="userDomain"
></
property
>
<
property
name
="userDao"
ref
="userDao"
></
property
>
</
bean
>
增加实例工厂:
package
com.linying.factory;
import
com.linying.dao.UserDao;
/**
* Dao实例工厂
*
@author
Ying-er
* @time 2010-6-20 下午04:23:25
* @mail [email protected]
*
@version
1.00
*/
public
class
DaoFactory {
public
UserDao getUserDao(){
return
new
UserDao();
}
}
控制台输出:
UserDao构造
User构造
UserAction构造
setUser方法被调用
setUserDao方法被调用
执行insert方法
saveUser方法被调用
4.构造器注入 :
修改beans.xml:
<
bean
name
="userDao"
class
="com.linying.dao.UserDao"
></
bean
>
<
bean
name
="userDomain"
class
="com.linying.domain.User"
></
bean
>
<
bean
name
="userAction"
class
="com.linying.service.UserAction"
>
< constructor-arg ref ="userDomain" ></ constructor-arg >
< constructor-arg ref ="userDao" ></ constructor-arg >
</
bean
>
修改UserAction.java:
package
com.linying.service;
import
com.linying.dao.UserDao;
import
com.linying.domain.User;
/**
* UserAction
*
*
@author
Ying-er
* @time 2010-6-20 上午10:28:16
* @mail [email protected]
*
@version
1.00
*/
public
class
UserAction {
private
User user;
private
UserDao userDao;
public
UserAction() {
System.out.println(
"
UserAction构造
"
);
}
public UserAction(User user,UserDao userDao){
System.out.println( " UserAction构造(有参构造) " );
this .user = user;
this .userDao = userDao;
}
/**
* 保存User对象
*
*/
public
void
saveUser() {
userDao.insert(user);
System.out.println(
"
saveUser方法被调用
"
);
}
//
public void setUser(User user) {
//
this.user = user;
//
System.out.println("setUser方法被调用");
//
}
//
//
public void setUserDao(UserDao userDao) {
//
this.userDao = userDao;
//
System.out.println("setUserDao方法被调用");
//
}
}
控制台输出:
UserDao构造
User构造
UserAction构造(有参构造)
执行insert方法
saveUser方法被调用
5.集合注入 :
beans.xml:
<
bean
name
="daoFactory"
class
="com.linying.factory.DaoFactory"
></
bean
>
<
bean
name
="userDao"
factory-bean
="daoFactory"
factory-method
="getUserDao"
>
</
bean
>
<
bean
name
="userDomain"
class
="com.linying.domain.User"
>
<
property
name
="id"
>
<
value
>
1
</
value
>
</
property
>
<
property
name
="name"
value
="admin"
></
property
>
<
property
name
="password"
value
="123"
></
property
>
<!--
property name="birthday" value="1987-06-20"></property>
-->
<
property
name
="listDemo"
>
< list >
< value > list-1 </ value >
< value > list-2 </ value >
</ list >
</
property
>
<
property
name
="setDemo"
>
< set >
< value > set-1 </ value >
< value > set-2 </ value >
</ set >
</
property
>
<
property
name
="mapDemo"
>
< map >
< entry key ="k1" value ="v1" ></ entry >
< entry key ="k2" value ="v2" ></ entry >
</ map >
</
property
>
</
bean
>
<
bean
name
="userAction"
class
="com.linying.service.UserAction"
>
<
property
name
="user"
ref
="userDomain"
></
property
>
<
property
name
="userDao"
ref
="userDao"
></
property
>
</
bean
>
User.java:
package
com.linying.domain;
import
java.util.Date;
import
java.util.List;
import
java.util.Map;
import
java.util.Set;
/**
* 领域对象
*
*
@author
Ying-er
* @time 2010-6-20 上午10:01:05
* @mail [email protected]
*
@version
1.00
*/
public
class
User {
private
int
id;
private
String name;
private
String password;
private
Date birthday;
private List < String > listDemo;
private Set < String > setDemo;
private Map < String, String > mapDemo;
public
User() {
System.out.println(
"
User构造
"
);
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name
=
name;
}
public
String getPassword() {
return
password;
}
public
void
setPassword(String password) {
this
.password
=
password;
}
public
int
getId() {
return
id;
}
public
void
setId(
int
id) {
this
.id
=
id;
}
public
Date getBirthday() {
return
birthday;
}
public
void
setBirthday(Date birthday) {
this
.birthday
=
birthday;
}
public
List
<
String
>
getListDemo() {
return
listDemo;
}
public
void
setListDemo(List
<
String
>
listDemo) {
this
.listDemo
=
listDemo;
}
public
Map
<
String, String
>
getMapDemo() {
return
mapDemo;
}
public
void
setMapDemo(Map
<
String, String
>
mapDemo) {
this
.mapDemo
=
mapDemo;
}
public
Set
<
String
>
getSetDemo() {
return
setDemo;
}
public
void
setSetDemo(Set
<
String
>
setDemo) {
this
.setDemo
=
setDemo;
}
}
Main.java:
package
com.linying;
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
com.linying.service.UserAction;
/**
* 测试程序入口
*
*
@author
Ying-er
* @time 2010-6-20 上午11:20:24
* @mail [email protected]
*
@version
1.00
*/
public
class
Main {
/**
*
@param
args
*/
public
static
void
main(String[] args) {
//
TODO Auto-generated method stub
BeanFactory bf
=
(BeanFactory) (
new
ClassPathXmlApplicationContext(
"
beans.xml
"
));
UserAction userAction
=
(UserAction) bf.getBean(
"
userAction
"
);
System.out.println(userAction.getUser().getId());
System.out.println(userAction.getUser().getName());
System.out.println(userAction.getUser().getPassword());
//
System.out.println(userAction.getUser().getBirthday());
System.out.println(userAction.getUser().getListDemo());
System.out.println(userAction.getUser().getSetDemo());
System.out.println(userAction.getUser().getMapDemo());
}
}
控制台输出:
UserDao构造
User构造
UserAction构造
setUser方法被调用
setUserDao方法被调用
1
admin
123
[list-1, list-2]
[set-1, set-2]
{k1=v1, k2=v2}
转自:http://www.blogjava.net/crazycoding/archive/2010/06/20/324002.html