Skip to content

Commit 3a04a58

Browse files
committed
学习了依赖注入
1 parent 51ffe6d commit 3a04a58

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

SpringLearn_No_Web/src/beans.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@
55
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
66

77
<!-- bean definitions here -->
8-
<bean id = "userService" class = "edu.xpu.service.UserServiceImpl"></bean>
8+
<bean id = "userService" class = "edu.xpu.service.UserServiceImpl">
9+
10+
<!--依赖注入数据,调用属性的set方法-->
11+
<property name="name" value="zhangsan"></property>
12+
<!--相当于UserService service = new UserServiceImpl();service.setName("");-->
13+
14+
</bean>
915
</beans>
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
package edu.xpu.service;
22

33
public class UserServiceImpl implements IUserService {
4+
5+
private String name;
6+
7+
public String getName() {
8+
return name;
9+
}
10+
11+
public void setName(String name) {
12+
this.name = name;
13+
}
14+
415
@Override
516
public void add() {
6-
System.out.println("添加用户...");
17+
System.out.println("添加用户..."+name);
718
}
819
}

SpringLearn_No_Web/src/edu/xpu/test/Lesson02.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static void test1(){
1717
// 用userService 从spring的容器中获取对象
1818
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
1919
IUserService userService1 = (IUserService)context.getBean("userService");
20-
IUserService userService2 = (IUserService)context.getBean("userService");
20+
2121
userService1.add();
2222

2323
System.out.println(userService1);

0 commit comments

Comments
 (0)