Spring中,如何给对象的属性赋值?  【DI, 依赖注入】

1) 通过构造函数

2) 通过set方法给属性注入值

3) p名称空间

    4)自动装配(了解)

5) 注解


package loaderman.c_property;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { // 创建容器对象
private ApplicationContext ac = new ClassPathXmlApplicationContext("loaderman/c_property/bean.xml"); @Test
public void testSet() {
// 从容器中获取
User user = (User) ac.getBean("user"); System.out.println(user);
} @Test
public void testExecuteAction() {
// 从容器中获取Action
UserAction userAction = (UserAction) ac.getBean("userAction");
userAction.execute(); }
}
package loaderman.c_property;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App_p { // 创建容器对象
private ApplicationContext ac = new ClassPathXmlApplicationContext("loaderman/c_property/bean_p.xml"); @Test
public void testExecuteAction() {
// 从容器中获取Action
UserAction userAction = (UserAction) ac.getBean("userAction");
userAction.execute(); System.out.println(ac.getBean("user"));
}
}
package loaderman.c_property;

public class User {

    private int id;
private String name; ////////////////// --> 通过容器注入属性值
public void setId(int id) {
this.id = id;
}
// //--> 通过容器注入属性值
public void setName(String name) {
this.name = name;
} ////////////////
public int getId() {
return id;
} public String getName() {
return name;
} @Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
} public User() {
super();
System.out.println("------User对象创建【无参数构造器】------");
} public User(int id, String name) {
System.out.println("-----User对象创建【带参数构造器】--------");
this.id = id;
this.name = name;
} public void init_user() {
System.out.println("创建对象之后,初始化");
}
public void destroy_user() {
System.out.println("IOC容器销毁,user对象回收!");
} }
package loaderman.c_property;

public class UserAction {

    // Service: springIOC容器注入
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
} public String execute() {
userService.save();
return null;
}
}
package loaderman.c_property;

public class UserDao {

    public void save() {
System.out.println("DB:保存用户");
}
}
package loaderman.c_property;

public class UserService {

    private UserDao userDao; // = new UserDao();

    // IOC:对象的创建交给spring的外部容器完成
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void save() {
userDao.save();
}
}
<?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="user1" class="loaderman.c_property.User" scope="prototype">
<constructor-arg value="100"></constructor-arg>
<constructor-arg value="Tom"></constructor-arg>
</bean> <!-- 2) 通过set方法给属性注入值 -->
<bean id="user" class="loaderman.c_property.User" scope="prototype">
<property name="id" value="101"></property>
<property name="name" value="Jack"></property>
</bean> <!--
案例:
action/service/dao
-->
<!-- dao instance -->
<bean id="userDao" class="loaderman.c_property.UserDao"></bean> <!-- service instance -->
<bean id="userService" class="loaderman.c_property.UserService">
<property name="userDao" ref="userDao"></property>
</bean> <!-- action instance -->
<bean id="userAction1" class="loaderman.c_property.UserAction">
<property name="userService" ref="userService"></property>
</bean> <!-- ##############内部bean############## -->
<bean id="userAction2" class="loaderman.c_property.UserAction">
<property name="userService">
<bean class="loaderman.c_property.UserService">
<property name="userDao">
<bean class="loaderman.c_property.UserDao"></bean>
</property>
</bean>
</property>
</bean>
<!--
给对象属性注入值:
# p 名称空间给对象的属性注入值
(spring3.0以上版本才支持)
-->
</beans>
<?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"> <!-- ###############对象属性赋值############### --> <!--
给对象属性注入值:
# p 名称空间给对象的属性注入值
(spring3.0以上版本才支持)
-->
<bean id="userDao" class="loaderman.c_property.UserDao"></bean> <bean id="userService" class="loaderman.c_property.UserService" p:userDao-ref="userDao"></bean> <bean id="userAction" class="loaderman.c_property.UserAction" p:userService-ref="userService"></bean> <!-- 传统的注入:
<bean id="user" class="cn.loaderman.c_property.User" >
<property name="name" value="xxx"></property>
</bean>
-->
<!-- p名称空间优化后 -->
<bean id="user" class="loaderman.c_property.User" p:name="Jack0001"></bean> </beans>

Spring对象依赖关系的更多相关文章

  1. Spring对象依赖关系处理

    Spring中给对象属性赋值 1.通过set方法给属性注入值 2.p名称空间 3.自动装配 4.注解 编写MVCModel调用userAction MVCModel public class MVCM ...

  2. Java进阶知识18 Spring对象依赖关系的几种写法

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. Spring IOC、对象依赖关系

    Spring IOC.对象依赖关系   2016-09-21 01:36 414人阅读 评论(0) 收藏 举报 本文章已收录于: 版权声明:本文为博主原创文章,未经博主允许不得转载. 引入 Strut ...

  4. 在SQL Server中查看对象依赖关系

    原文 在SQL Server中查看对象依赖关系 Viewing object dependencies in SQL Server   Deleting or changing objects may ...

  5. Spring之对象依赖关系(依赖注入Dependency Injection)

    承接上篇: Spring中,如何给对象的属性赋值: 1:通过构造函数,如下所示: <!-- 1:构造函数赋初始值 --><bean id="user1" clas ...

  6. Spring第三篇【Core模块之对象依赖】

    前言 在Spring的第二篇中主要讲解了Spring Core模块的使用IOC容器创建对象的问题,Spring Core模块主要是解决对象的创建和对象之间的依赖关系,因此本博文主要讲解如何使用IOC容 ...

  7. spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象

    相关 知识 >>> 相关 练习 >>> 实现要求: 在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXm ...

  8. Spring 3.x jar 包详解 与 依赖关系

    以下的内容我会持续更新(当然是我有新发现的时候); 以下内容是我在网上搜索.整理.修改的而成的内容.由于很多内容都是转载了,无法追溯到源头,因此无法一一对原作者进行道谢. 这几天,我查阅大量的官方的文 ...

  9. Spring框架学习之高级依赖关系配置(一)

    上篇文章我们对Spring做了初步的学习,了解了基本的依赖注入思想.学会简单的配置bean.能够使用Spring容器管理我们的bean实例等.但这还只是相对较浅显的内容,本篇将介绍bean的相关更高级 ...

随机推荐

  1. CentOS系统 Amoeba+MySql主从读写分离配置 适合新手傻瓜式教程!-----仅供参考!

    废话不说,直接开始: 一.安装mysql的三种方式,这里采用第2种(安装方式不再详解,请参照) http://www.cnblogs.com/babywaa/articles/4837946.html ...

  2. pytorch转onnx问题

    Fail to export the model in PyTorch https://github.com/onnx/tutorials/blob/master/tutorials/PytorchA ...

  3. 03_ Flume采集(监听)目录到HDFS案例

    采集需求:某服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,就需要把文件采集到HDFS中去 根据需求,首先定义以下3大要素 l.采集数据源,即source——监控文件目录 :  spool ...

  4. 03_Redis_String命令

    一:Redis命令---String命令:Redis 字符串数据类型的相关命令用于管理 redis 字符串值 字符串类型是Redis中最为基础.常用的数据存储类型,字符串在Redis中是二进制安全的, ...

  5. Mybatis入门配置及第一个Mybatis程序

    目的:使用mybatis来进行对数据库表的操作 第一步:引入jar包 我这里是创建的maven工程 第二步:创建数据表user 第三步:创建实体类 实体类放在包 com.xxx.pojo 下,包名可自 ...

  6. Make It Connected CodeForces - 1095F (建图+最小生成树)

    Make It Connected CodeForces - 1095F You are given an undirected graph consisting of nn vertices. A ...

  7. Selenium&Appium四种等待方式

    一.摘要 本博文主要介绍自动化测试中,无论是selenium或是Appium的四种等待方式,合理的使用等待对代码的稳定性,测试效率都有很大的提高 隐式等待:是在尝试发现某个元素的时候,如果没能立刻发现 ...

  8. MNPR--造福人类的人值得被感激

    https://artineering.io/research/MNPR/ https://mnpr.artineering.io https://pdfs.semanticscholar.org/0 ...

  9. PHP即时通讯设计实现

    详解即时通讯设计实现(PHP+GatewayWorker+Redis) 需要实现的功能 一对一聊天(私聊) 一对多聊天(群聊) 类似QQ,微信等聊天列表 实时消息 显示 工具选择 GatewayWor ...

  10. 正整数n拆分成几个不同的平方数——DFS&&打表

    考虑将正整数n拆分成几个不同的平方数之和,比如30=1^2 + 2^2 + 5^2=1^2 + 2^2 + 3^2 + 4^2,而8不存在这样的拆分. #include<bits/stdc++. ...