Skip to content

Commit f0c3400

Browse files
committed
bean的作用域
1 parent 80b75da commit f0c3400

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

SpringLearn_No_Web/SpringLearn_No_Web.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<fileset id="fileset" name="Spring Application Context" removed="false">
77
<file>file://$MODULE_DIR$/src/beans.xml</file>
88
<file>file://$MODULE_DIR$/src/beans3.xml</file>
9+
<file>file://$MODULE_DIR$/src/beans4.xml</file>
910
</fileset>
1011
</configuration>
1112
</facet>

SpringLearn_No_Web/src/beans4.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
xsi:schemaLocation="
5+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
6+
7+
8+
<!--Bean的作用域
9+
10+
singleton: 单例 ,默认为单例
11+
12+
prototype: 多例
13+
-->
14+
15+
<bean id="userService" class="edu.xpu.service.UserServiceImpl" scope="prototype"></bean>
16+
17+
</beans>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
package edu.xpu.test;
3+
4+
import edu.xpu.service.IUserService;
5+
import edu.xpu.service.UserServiceFactory1;
6+
import edu.xpu.service.UserServiceFactory2;
7+
import org.springframework.beans.factory.BeanFactory;
8+
import org.springframework.beans.factory.xml.XmlBeanFactory;
9+
import org.springframework.context.ApplicationContext;
10+
import org.springframework.context.support.ClassPathXmlApplicationContext;
11+
import org.springframework.core.io.FileSystemResource;
12+
13+
public class Lesson04 {
14+
15+
public static void test1(){
16+
17+
18+
19+
ApplicationContext context = new ClassPathXmlApplicationContext("beans4.xml");
20+
21+
22+
// 从容器获取两个Bean
23+
24+
// singaton 单例 对象地址一样说明为同一个对象
25+
26+
// prototype 多例 为不同的对象
27+
IUserService userService1 = (IUserService)context.getBean("userService");
28+
29+
IUserService userService2 = (IUserService)context.getBean("userService");
30+
31+
System.out.println(userService1);
32+
33+
System.out.println(userService2);
34+
35+
}
36+
37+
public static void main(String[] args) {
38+
39+
Lesson04.test1();
40+
41+
}
42+
}

0 commit comments

Comments
 (0)