第一种方式:方法注入
方法注入的最好用途之一就是处理单态、无状态对象需要调用非单态、有状态或者非线程安全对象的情况。
1
2
3
4
5
|
package
com.mzsx.spring.notsingleton;
public
interface
IUserDao {
public
void
add();
public
void
delete();
public
void
load();
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package
com.mzsx.spring.notsingleton;
public
class
UserDaoImp
implements
IUserDao {
@Override
public
void
add() {
System.out.println(
"dao添加用户。。。。"
);
}
@Override
public
void
delete() {
System.out.println(
"dao***用户。。。。"
);
}
@Override
public
void
load() {
System.out.println(
"dao加载用户。。。。"
);
}
}
|
1
2
3
4
5
6
7
8
|
package
com.mzsx.spring.notsingleton;
public
abstract
class
UserService {
protected
abstract
IUserDao getUserDao();
public
void
add() {
getUserDao().add();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns="
http://www.springframework.org/schema/beans
"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance
"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<
bean
id
=
"userDao"
class
=
"com.mzsx.spring.notsingleton.UserDaoImp"
scope
=
"prototype"
></
bean
>
<
bean
id
=
"userService"
class
=
"com.mzsx.spring.notsingleton.UserService"
>
<
lookup-method
name
=
"getUserDao"
bean
=
"userDao"
/>
</
bean
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package
com.mzsx.spring.notsingleton;
import
org.junit.Test;
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
TestMain {
private
BeanFactory factory=
new
ClassPathXmlApplicationContext(
"beans1.xml"
);
@Test
public
void
notSingleton(){
UserService users=(UserService)factory.getBean(
"userService"
);
System.out.println(users.getUserDao());
UserService users1=(UserService)factory.getBean(
"userService"
);
System.out.println(users1.getUserDao());
}
}
|
结果:
1
2
3
4
5
6
7
8
9
|
2014
-
6
-
3
22
:
00
:
10
org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory
@5bb966
: defining beans [userDao,userService]; root of factory hierarchy
com.mzsx.spring.notsingleton.UserDaoImp
@caf6c1
com.mzsx.spring.notsingleton.UserDaoImp
@10e35d5
|
需要使用cgLib。
第二种方式:ApplicationContextAware
1
2
3
4
5
|
package
com.mzsx.spring.notsingleton2;
public
interface
IUserDao {
public
void
add();
public
void
delete();
public
void
load();
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package
com.mzsx.spring.notsingleton2;
import
org.springframework.beans.BeansException;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.ApplicationContextAware;
public
class
UserDaoImp
implements
IUserDao,ApplicationContextAware {
private
ApplicationContext app;
@Override
public
void
add() {
System.out.println((IUserDao)app.getBean(
"userDao"
));
System.out.println(
"dao添加用户。。。。"
);
}
@Override
public
void
delete() {
System.out.println(
"dao***用户。。。。"
);
}
@Override
public
void
load() {
System.out.println(
"dao加载用户。。。。"
);
}
@Override
public
void
setApplicationContext(ApplicationContext app)
throws
BeansException {
// TODO Auto-generated method stub
this
.app=app;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
package
com.mzsx.spring.notsingleton2;
public
class
UserService {
private
IUserDao userDao;
public
IUserDao getUserDao() {
return
userDao;
}
public
void
setUserDao(IUserDao userDao) {
this
.userDao = userDao;
}
public
void
add() {
userDao.add();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
beans
xmlns="
http://www.springframework.org/schema/beans
"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance
"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<
bean
id
=
"userDao"
class
=
"com.mzsx.spring.notsingleton2.UserDaoImp"
scope
=
"prototype"
></
bean
>
<
bean
id
=
"userService"
class
=
"com.mzsx.spring.notsingleton2.UserService"
>
<
property
name
=
"userDao"
ref
=
"userDao"
></
property
>
</
bean
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package
com.mzsx.spring.notsingleton2;
import
org.junit.Test;
import
org.springframework.beans.factory.BeanFactory;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public
class
TestMain {
private
BeanFactory factory=
new
ClassPathXmlApplicationContext(
"beans2.xml"
);
@Test
public
void
notSingleton(){
UserService users=(UserService)factory.getBean(
"userService"
);
users.add();
UserService users1=(UserService)factory.getBean(
"userService"
);
users1.add();
}
}
|
结果:d
1
2
3
4
5
6
7
8
9
10
11
12
13
|
2014
-
6
-
3
22
:
03
:
20
org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory
@1e903d5
: defining beans [userDao,userService]; root of factory hierarchy
com.mzsx.spring.notsingleton2.UserDaoImp
@1554d32
dao添加用户。。。。
com.mzsx.spring.notsingleton2.UserDaoImp
@13f136e
dao添加用户。。。。
|
ApplicationContextAware接口,此接口定义了一个方法setApplicationContext()方法,此方法在Spring初始化类以后,调用实现了ApplicationContextAware接口的类的setApplicationContext()方法,将自身的一个引用传递给此实例。
我们将这两种单例调用非单例的例子对比一下,不难看出,第一种方式:方法注入要好于第二种方式:实现接口。原因是,第二种方式与Spring绑定了。而第一种方式则没有这种情况,在Service中,我们根本看不出Spring的影子。
本文转自 梦朝思夕 51CTO博客,原文链接:http://blog.51cto.com/qiangmzsx/1421705