32.Spring-对象依赖.md

来源:互联网 时间:2016-11-10


[toc]

1.对象依赖的分类

Spring中,给对象属性赋值的方法:

  • 构造函数
  • Set方法
  • p命名空间

1.1构造函数

构造方法通过配置文件中constructor-arg标签实现,使用时候需要注意要有带参的构造函数

1.2.Set方法

通过配置文件中property标签实现,使用时候需要注意要有无参构造方法和Getter Setter函数

1.3p命名空间

通过配置文件中p:XXX-ref=""标签实现,这里的引用也可换成使用实际值。

Spring 3.0以上版本支持

2.实例

对于构造函数和Set方法,使用User类示例。p命名空间模拟开发过程

2.1先创建各对象,注意需要的构造和Getter Setter方法

package per.liyue.springlearing.structure;

/**

* 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;

}

}

package per.liyue.springlearing.structure;

/**

* Created by liyue on 2016/11/10.

*/

public class UserDao

{

public void Save()

{

System.out.println("执行了Save方法保存数据");

}

}

package per.liyue.springlearing.structure;

/**

* 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();

}

}

package per.liyue.springlearing.structure;

/**

* 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;

}

}

2.2配置xml文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

<!--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>

2.3模拟调用

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();

}

}

2.4输出

1.构造方法方式为对象属性赋值User{userId=1, userName='张三'}

2.Set方法方式为对象属性赋值User{userId=2, userName='李四'}

3.p命名空间方法执行:

执行了Save方法保存数据

相关阅读:
Top