<span style="font-size:18px;"><span style="font-family:SimSun;"><span style="background-color: rgb(255, 255, 255);"><span style="white-space: pre;"> </span> </span><span style="background-color: rgb(255, 255, 255);">Spring框架学习(2)--bean标签部分属性的学习</span></span></span>
MyEclipse环境
新建一个Java Project工程,配置Spring3.1版本。
bean标签的属性
(1)name-->bean标签的名字,唯一,命名规则松散,在测试类中调用getBean(name)时,
name必须和bean标签中的name相同。
(2)id-->bean标签的id,唯一,命名规则严谨
(3)class-->bean要指向的类的名称
(4)lazy-init-->懒加载,默认为false,在加载Spring的配置时,将bean中配置好的类也全部加载完毕
true,在使用到某个类时,再加载这个类。(常用,实际)
也可以在bean的开始处设置全局变量 default-lazy-init="true"
(5)scope-->范围,默认是singleton,单例,Spring中只有一份bean,每次请求时得到的都是同一个bean
prototype,原型,Spring中有无数个bean的副本,每次请求得到的都是一个新的bean(少用)
下面,我来演示一下lazy-init和scope使用不同的值时,产生怎样不同的效果。
目录构造如下:
1.lazy-init取值为false,不写时默认就是false
1).在src目录下新建com.etc.service包,在包下新建HelloService.java类
<span style="white-space:pre"> </span>package com.etc.service;
<span style="white-space:pre"> </span>public class HelloService {
<span style="white-space:pre"> </span>public HelloService()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>System.out.println("加载了构造方法");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public void sayHello()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>System.out.println("hello spring");
<span style="white-space:pre"> </span>}
2).配置applicationContext.xml文件
<span style="white-space:pre"> </span><?xml version="1.0" encoding="UTF-8"?>
<span> </span><beans
<span> </span>xmlns="http://www.springframework.org/schema/beans"
<span> </span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span> </span>xmlns:p="http://www.springframework.org/schema/p"
<span> </span>xsi:schemaLocation="http://www.springframework.org/schema/beans
<span> </span>http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
>
<bean name="hello" class="com.etc.service.HelloService" lazy-init="false"></bean>
<span> </span></beans>
3).右键工程名新建test源文件夹,在文件夹下新建com.etc.service包,在包下新建HelloServiceTest.java类
<span style="white-space:pre"> </span>public class HelloServiceTest {
<span style="white-space:pre"> </span>@Test
<span style="white-space:pre"> </span>public void test1()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>//1.配置applicationContext.xml文件
<span style="white-space:pre"> </span>ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
<span style="white-space:pre"> </span>System.out.println("加载spring的配置");
<span style="white-space:pre"> </span>HelloService hello = (HelloService) context.getBean("hello");
<span style="white-space:pre"> </span>System.out.println("加载bean中的类");
<span style="white-space:pre"> </span>hello.sayHello();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
4)运行结果:
加载了构造方法
加载spring的配置
<span style="white-space:pre"> </span> 加载bean中的类
2.lazy-init取值为true
applicationContext.xml中配置如下:
<span style="white-space:pre"> </span><?xml version="1.0" encoding="UTF-8"?>
<span> </span><beans
<span> </span>xmlns="http://www.springframework.org/schema/beans"
<span> </span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span> </span>xmlns:p="http://www.springframework.org/schema/p"
<span> </span>xsi:schemaLocation="http://www.springframework.org/schema/beans
<span> </span>http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
>
<bean name="hello" class="com.etc.service.HelloService" lazy-init="true"></bean>
<span> </span></beans>
其余代码与上面相同
运行结果如下:
加载spring的配置
加载了构造方法
加载了bean中的类
3.在applicationContext.xml文件的开始处<bean>标签中设置全局lazy-init的值
applicationContext.xml中配置如下:
<span style="white-space:pre"> </span><?xml version="1.0" encoding="UTF-8"?>
<span> </span><beans
<span> </span>xmlns="http://www.springframework.org/schema/beans"
<span> </span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<span> </span>xmlns:p="http://www.springframework.org/schema/p"
<span> </span>xsi:schemaLocation="http://www.springframework.org/schema/beans
<span> </span>http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
<span style="white-space:pre"> </span>default-lazy-init="true"
>
<bean name="hello" class="com.etc.service.HelloService" ></bean>
<span> </span></beans>
其余代码与上面相同
运行结果如下:
加载spring的配置
加载了构造方法
加载了bean中的类
4.设置scope的值为singleton,不写时默认就是singleton
与上面的目录布局相同,只是其中代码稍有改动,如下:
1)HelloService.java代码如下:
package com.etc.service;
public class HelloService {
private int count = 0;
public void sayHello()
{
count++;
System.out.println("hello spring,调用了"+count+"次");
}
}
2)applicationContext.xml中配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
>
<bean name="hello" class="com.etc.service.HelloService" scope="singleton"></bean>
</beans>
3)HelloServiceTest.java代码如下:
<span style="font-size:18px;">package com.etc.service;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloServiceTest {
@Test
public void test1()
{
//1.配置applicationContext.xml文件
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
HelloService hello = (HelloService) context.getBean("hello");
hello.sayHello();
HelloService hello2 = (HelloService) context.getBean("hello");
hello2.sayHello();
}
}</span>
4)运行结果如下:
hello spring,调用了1次
hello spring,调用了2次
5.设置scope的值为prototype
applicationContext.xml中配置如下:
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
>
<bean name="hello" class="com.etc.service.HelloService" scope="prototype"></bean>
</beans></span>
其余代码与上面相同
运行结果如下:
hello spring,调用了1次
hello spring,调用了1次