1 package com.lsz.spring.action; 2 3 public class User { 4 /** 5 * set注入 6 */ 7 private String username; 8 public void setUsername(String username) { 9 this.username=username; 10 } 11 public String getUsername() { 12 return username; 13 } 14 /* 15 <bean id="userAction"class="com.lsz.spring.action.User" > 16 <span style="white-space:pre"> </span><property name="username" value="admin"></property> 17 </bean> 18 */ 19 /** 20 * 给对象赋值 21 */ 22 23 private UserService ; 24 public UserServicegetUserservice() { 25 return userservice; 26 } 27 public void setUserservice(UserService userservice){ 28 this.userservice= userservice; 29 } 30 31 /* 32 * <!--对象的声明--> 33 <bean id="userService" class="com.lsz.spring.service.UserService"></bean> 34 35 <bean id="userAction"class="com.lsz.spring.action.User" > 36 <property name="userservice" ref="userService"></property> 37 </bean> 38 */ 39 40 41 }
备注:http://blog.csdn.net/lishuangzhe7047/article/category/2069855 这个博客有一篇依赖注入的文章不错。spring的依赖注入那篇