Spring中,给对象属性赋值的方法:
构造方法通过配置文件中constructor-arg标签实现,使用时候需要注意要有带参的构造函数
通过配置文件中property标签实现,使用时候需要注意要有无参构造方法和Getter Setter函数
通过配置文件中p:XXX-ref=""标签实现,这里的引用也可换成使用实际值。
Spring 3.0以上版本支持
对于构造函数和Set方法,使用User类示例。p命名空间模拟开发过程
/**
* Created by liyue on 2016/11/10.
*/
public class User
{
private int userId;
private String userName;
@Override
public String toString()
{
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
'}';
}
public User()
{
}
public User(int userId, String userName)
{
this.userId = userId;
this.userName = userName;
}
public int getUserId()
{
return userId;
}
public void setUserId(int userId)
{
this.userId = userId;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
}
/**
* Created by liyue on 2016/11/10.
*/
public class UserDao
{
public void Save()
{
System.out.println("执行了Save方法保存数据");
}
}
/**
* Created by liyue on 2016/11/10.
*/
public class UserService
{
public UserDao getUserDao()
{
return userDao;
}
public void setUserDao(UserDao userDao)
{
this.userDao = userDao;
}
private UserDao userDao;
public void Save()
{
userDao.Save();
}
}
/**
* Created by liyue on 2016/11/10.
*/
public class UserAction
{
private UserService userService;
public String execute()
{
userService.Save();
return null;
}
public UserService getUserService()
{
return userService;
}
public void setUserService(UserService userService)
{
this.userService = userService;
}
}
<!--1.构造函数方法
注意要有对应的带参构造方法
-->
<bean id="userC" class="per.liyue.springlearing.structure.User" scope="prototype">
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="张三"></constructor-arg>
</bean>
<!--2.Set方法
注意要有无参构造方法和Getter Setter函数
-->
<bean id="userS" class="per.liyue.springlearing.structure.User" scope="prototype">
<property name="userId" value="2"></property>
<property name="userName" value="李四"></property>
</bean>
<!--p命名空间
注意要有Getter Setter函数
-->
<bean id="userDao" class="per.liyue.springlearing.structure.UserDao"></bean>
<bean id="userService" class="per.liyue.springlearing.structure.UserService" p:userDao-ref="userDao"></bean>
<bean id="userAction" class="per.liyue.springlearing.structure.UserAction" p:userService-ref="userService"></bean>
</beans>
package per.liyue.springlearing.structure;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by liyue on 2016/11/10.
*/
public class App
{
private ApplicationContext applicationContext = new ClassPathXmlApplicationContext("per/liyue/springlearing/bean.xml");
@Test
public void Main()
{
User user1 = applicationContext.getBean("userC", User.class);
System.out.println("1.构造方法方式为对象属性赋值" + user1);
User user2 = applicationContext.getBean("userS", User.class);
System.out.println("2.Set方法方式为对象属性赋值" + user2);
UserAction userAction = applicationContext.getBean("userAction", UserAction.class);
System.out.println("3.p命名空间方法执行:");
userAction.execute();
}
}
1.构造方法方式为对象属性赋值User{userId=1, userName='张三'}
2.Set方法方式为对象属性赋值User{userId=2, userName='李四'}
3.p命名空间方法执行:
执行了Save方法保存数据