鏈接地址:http://www.cnblogs.com/cyjch/archive/2012/02/06/2340415.html
Spring學習筆記(
1
)----簡單的實例
---------------------------------
首先需要準備Spring包,可從官方網站上下載。
下載解壓後,必須的兩個包是spring.jar和commons-logging.jar。此外爲了便於測試加入了JUnit包。
在Myeclipse中創建Java項目。
編寫一個接口類,爲了簡單,只加入了一個方法。
Java代碼
1
.
package
com.szy.spring.interfacebean;
2
.
3
.
public
interface
PersonBean
4
.{
5
.
void
show();
6
.}
然後寫一個類實現這個接口。
Java代碼
1
.
package
com.szy.spring.implbean;
2
.
import
com.szy.spring.interfacebean.PersonBean;
3
.
4
.
public
class
UserBean
implements
PersonBean
5
.{
6
.
7
.
public
void
show()
8
.
{
9
.
System.out.println(
"Hello
Kuka"
);
10
.
}
11
.
12
.}
以上的過程我們再熟悉不過了,下面開始加入Spring的內容了。首先從下載的Sping包中找到配置文件,刪除不需要的,找到最原始的部分:
Xml代碼
1
.<?xml
version=
"1.0"
encoding=
"UTF-8"
?>
2
.<beans
xmlns=
"http://www.springframework.org/schema/beans"
3
.
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
4
.
xmlns:context=
"http://www.springframework.org/schema/context"
5
.
xmlns:tx=
"http://www.springframework.org/schema/tx"
6
.
xsi:schemaLocation="http:
//www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7
.
http:
//www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
8
.
http:
//www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
9
.
10
.</beans>
我們在配置文件中加入我們的bean信息
Xml代碼
1
.<bean
id=
"userBean"
class
=
"com.szy.spring.implbean.UserBean"
/>
其中id作爲標識符,
class
爲類的包路徑。
這樣我們的配置文件就寫好了,完整的配置文件呢如下。
Xml代碼
1
.<?xml
version=
"1.0"
encoding=
"UTF-8"
?>
2
.<beans
xmlns=
"http://www.springframework.org/schema/beans"
3
.
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
4
.
xmlns:context=
"http://www.springframework.org/schema/context"
5
.
xmlns:tx=
"http://www.springframework.org/schema/tx"
6
.
xsi:schemaLocation="http:
//www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7
.
http:
//www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
8
.
http:
//www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
9
.
10
.
<bean id=
"userBean"
class
=
"com.szy.spring.implbean.UserBean"
/>
11
.</beans>
最後我們創建一個測試類測試:
Java代碼
1
.
package
com.szy.spring.test;
2
.
3
.
import
org.junit.Test;
4
.
import
org.springframework.context.ApplicationContext;
5
.
import
org.springframework.context.support.ClassPathXmlApplicationContext;
6
.
import
com.szy.spring.interfacebean.PersonBean;
7
.
8
.
9
.
public
class
TestClass
10
.{
11
.
@Test
12
.
public
void
testMethod()
throws
Exception
13
.
{
14
.
//讀取配置文件
15
.
ApplicationContext ctx=
new
ClassPathXmlApplicationContext(
"applicationContext.xml"
);
16
.
//獲取UserBean的實例
17
.
PersonBean bean=(PersonBean)ctx.getBean(
"userBean"
);
18
.
//調用方法
19
.
bean.show();
20
.
}
21
.}
運行,輸入如下結果:
結果代碼
1
.Hello
Kuka
Ok,我們的第一個Spring程序成功運行。
Sping學習筆記(
2
)----實例化Bean的三種方式
-------------------------------------------
Spring的實例化Bean有三種方式:
使用類構造器直接實例化
使用靜態工廠的方法實例化
使用實例工廠方法實例化
三種方式對應的配置如下
Xml代碼
1
.<?xml
version=
"1.0"
encoding=
"UTF-8"
?>
2
.<beans
xmlns=
"http://www.springframework.org/schema/beans"
3
.
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
4
.
xmlns:context=
"http://www.springframework.org/schema/context"
5
.
xmlns:tx=
"http://www.springframework.org/schema/tx"
6
.
xsi:schemaLocation="http:
//www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7
.
http:
//www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
8
.
http:
//www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
9
.
<!-- 使用類構造器直接實例化 -->
10
.
<bean id=
"userBean1"
class
=
"com.szy.spring.implbean.UserBean"
/>
11
.
<!-- 使用靜態工廠的方法實例化 -->
12
.
<bean id=
"userBean2"
class
=
"com.szy.spring.factory.BeanFactory"
factory-method=
"UserBeanService"
/>
13
.
<!-- 使用實例工廠方法實例化 -->
14
.
<bean id=
"factory"
class
=
"com.szy.spring.factory.BeanFactory"
/>
15
.
<bean id=
"userBean3"
factory-bean=
"factory"
factory-method=
"getUserBeanService"
/>
16
.</beans>
其中BeanFactory類的代碼如下
Java代碼
1
.
package
com.szy.spring.factory;
2
.
3
.
import
com.szy.spring.implbean.UserBean;
4
.
import
com.szy.spring.interfacebean.PersonBean;
5
.
6
.
public
class
BeanFactory
7
.{
8
.
//使用靜態工廠的方法實例化使用
9
.
public
static
PersonBean
UserBeanService()
10
.
{
11
.
return
new
UserBean();
12
.
}
13
.
14
.
public
PersonBean
getUserBeanService()
15
.
{
16
.
return
new
UserBean();
17
.
}
18
.}
在這三種方式中我們最常用的還是第一種。
Spring學習筆記(
3
)----編碼剖析Spring管理Bean的原理
--------------------------------------------------
Xml代碼
1
.<?xml
version=
"1.0"
encoding=
"UTF-8"
?>
2
.<beans
xmlns=
"http://www.springframework.org/schema/beans"
3
.
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
4
.
xmlns:context=
"http://www.springframework.org/schema/context"
5
.
xmlns:tx=
"http://www.springframework.org/schema/tx"
6
.
xsi:schemaLocation="http:
//www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7
.
http:
//www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
8
.
http:
//www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
9
.
<bean id=
"userBean"
class
=
"com.szy.spring.implbean.UserBean"
/>
10
.</beans>
Spring的配置文件中記錄了類的包路徑,因此我們首先是要讀入配置文件。在配置文件中Bean有id和
class
兩個屬性,我們首先定義一個Bean類,包含這兩個屬性:
Java代碼
1
.
package
com.szy.spring.implbean;
2
.
3
.
public
class
Bean
4
.{
5
.
private
String
id;
6
.
private
String
className;
7
.
public
String
getId()
8
.
{
9
.
return
id;
10
.
}
11
.
public
void
setId(String
id)
12
.
{
13
.
this
.id
= id;
14
.
}
15
.
public
String
getClassName()
16
.
{
17
.
return
className;
18
.
}
19
.
public
void
setClassName(String
className)
20
.
{
21
.
this
.className
= className;
22
.
}
23
.
24
.}
25
.
由於配置文件是xml文件,在這裏使用Jdom包操作xml文件,讀入配置文件中的Bean信息。
Java代碼
1
.
/**
2.
* 讀取xml配置文件
3.
* @param fileName 配置文件名
4.
*/
5
.
private
void
readXML(String
fileName)
6
.
{
7
.
//
尋找配置文件
8
.
URL xmlPath =
this
.getClass().getClassLoader().getResource(fileName);
9
.
Document doc =
null
;
10
.
Element root =
null
;
11
.
try
12
.
{
13
.
SAXBuilder sb =
new
SAXBuilder(
false
);
14
.
doc = sb.build(
new
FileInputStream(
new
File(xmlPath.toURI())));
15
.
//
設置命名空間
16
.
Namespace xhtml = Namespace.getNamespace(
"xhtml"
,
17
.
"http://www.springframework.org/schema/beans"
);
18
.
root = doc.getRootElement();
//
獲取根元素
19
.
List<Element> list = root.getChildren(
"bean"
,
xhtml);
//獲取全部bean節點
20
.
for
(Element
element : list)
//
遍歷節點,取得每個節點的屬性
21
.
{
22
.
String id = element.getAttributeValue(
"id"
);
23
.
String className = element.getAttributeValue(
"class"
);
24
.
Bean bean =
new
Bean();
25
.
bean.setId(id);
26
.
bean.setClassName(className);
27
.
beanList.add(bean);
28
.
}
29
.
}
catch
(Exception
e)
30
.
{
31
.
e.printStackTrace();
32
.
}
33
.
34
.
}
其中beanList是一個List對象,因爲在配置文件中存在很多Bean。
得到了所有的Bean對象後,下面就實例化每個Bean對象,結果存放在Map對象中。
Java代碼
1
.
/**
2.
* bean的實例化
3.
*/
4
.
private
void
instanceBeans()
5
.
{
6
.
for
(Bean
bean : beanList)
7
.
{
8
.
try
9
.
{
10
.
if
(bean.getClassName()
!=
null
&&
!
""
.equals(bean.getClassName().trim()))
11
.
beanObject.put(bean.getId(), Class.forName(bean.getClassName()).newInstance());
12
.
}
catch
(Exception
e)
13
.
{
14
.
e.printStackTrace();
15
.
}
16
.
}
17
.
18
.
}
其中beanObject爲Map對象。
最後再加入一個方法,方便外部能獲取Map中的對象
Java代碼
1
.
/**
2.
* 獲取bean實例
3.
* @param beanName 配置文件中bean的Id
4.
* @return
5.
*/
6
.
public
Object
getBean(String beanName)
7
.
{
8
.
return
this
.beanObject.get(beanName);
9
.
}
完整的MyClassPathXMLApplicationContext見附件中的代碼。
下面測試MyClassPathXMLApplicationContext類。
Java代碼
1
.
@Test
2
.
public
void
testMethod()
throws
Exception
3
.
{
4
.
//讀取配置文件
5
.
MyClassPathXMLApplicationContext ctx=
new
MyClassPathXMLApplicationContext(
"applicationContext.xml"
);
6
.
//獲取UserBean的實例
7
.
PersonBean bean=(PersonBean)ctx.getBean(
"userBean"
);
8
.
//調用方法
9
.
bean.show();
10
.
}
輸出結果
結果代碼
1
.Hello
Kuka
成功。
上面僅是簡單的演示了Spring管理Bean的原理,但是在實際操作中還需要考慮很對其它因素。