File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 11package edu .xpu .service ;
22
33public 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}
Original file line number Diff line number Diff 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 );
You can’t perform that action at this time.
0 commit comments