Spring入门(二)——DI
1. DI
Dependency Injection,依赖注入。当对象里有属性或对象的时候,就需要为这些属性或对象赋值
2. 流程
这里介绍两种方式
- set方法
- 注解方式
2.1 set方法
Bean准备
package bean;
import bean.Question;
public class User {
private String name;
private String email;
private String password;
private Question question;
/**
* 省略了getters/setters
* @author Howl
*/
//set方法
public void setQuestion(Question question) {
this.question = question;
}
//构造函数
public User(String name, String email, String password) {
super();
this.name = name;
this.email = email;
this.password = password;
}
}
package bean;
import java.sql.Timestamp;
import java.util.Date;
public class Question {
private int id;
private Timestamp time;
private String content;
/**
* 省略了getters/setters
* @author Howl
*/
//构造函数
public Question(int id, Timestamp time, String content) {
super();
this.id = id;
this.time = time;
this.content = content;
}
}
applictionContext.xml配置
<!--创建User对象-->
<bean id="User" class="User">
<!-- 依赖注入,setting自动注入 -->
<property name="Question" ref="Question"/>
</bean>
<!--创建Question对象-->
<bean id="Question" class="Question"></bean>
获取对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//直接获取
User user = ac.getBean("User");
2.2 注解方式
注解准备
package bean;
import bean.Question;
@Component
public class User {
private String name;
private String email;
private String password;
private Question question;
/**
* 省略了getters/setters
* @author Howl
*/
//set方法
@Autowired
public void setQuestion(Question question) {
this.question = question;
}
//构造函数
public User(String name, String email, String password) {
super();
this.name = name;
this.email = email;
this.password = password;
}
}
获取对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//直接获取
User user = ac.getBean("User");
Spring入门(二)——DI的更多相关文章
- Spring入门(二):自动化装配bean
Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...
- Spring入门二:整合mybatis
一.SM思路分析 1.引入核心依赖及相关依赖: spring(略).mybatis.mysql.mybatis-spring(减少自己实现FactoryBean接口).druid <depen ...
- spring入门(二) 使用注解代替xml配置
1.导包(略) 2.applicationContext.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...
- Spring IoC、DI入门小程序
Alt+/智能提示xml配置文件节点及属性:在接口上使用Ctrl+T可以提示其实现类 一.IoC控制反转(将创建对象的权利交给spring)入门小程序 1.引入jar包 2.工程基本结构 3.新建Us ...
- Spring入门(一)— IOC、DI
一.Spring介绍 Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成 ...
- Spring入门详细教程(二)
前言 本篇紧接着spring入门详细教程(一),建议阅读本篇前,先阅读第一篇.链接如下: Spring入门详细教程(一) https://www.cnblogs.com/jichi/p/1016553 ...
- Spring(二)--Spring入门案例
Spring入门案例 1.需要的实体类 2.需要的接口和实现类 3.需要的service和实现类 /** * service层的作用 * 在不改变dao层代码的前提下,增加业务逻辑操作 */ publ ...
- Spring入门一:IOC、DI、AOP基本思想
Spring框架是一个集众多涉及模式于一身的开源的.轻量级的项目管理框架,致力于javaee轻量级解决方案.相对于原来学过的框架而言,spring框架和之前学习的struts2.mybatis框架有了 ...
- Spring Boot入门(二):获取配置文件值
本篇博客主要讲解下在Spring Boot中如何获取配置文件的值. 1. 使用yaml配置文件 Spring Boot默认生成的配置文件为application.properties,不过它也支持ya ...
- Spring IoC 和 DI 简介(二)
Spring IoC 和 DI 简介 IoC:Inverse of Control(控制反转) 读作“反转控制”,更好理解,不是什么技术,而是一种设计思想,就是将原本在程序中手动创建对象的控制权,交由 ...
随机推荐
- Postman 基本使用
Postman 基本使用 Postman主界面 工具栏 New: 新建,可以新建Request请求,Collection请求集,环境等等 Import: 导入,可以导入别人导出的请求集 Runne ...
- 转 让NET C# 程序独立运行(脱离 .NET Framework运行,绿色运行) 未验证
但是.net版本众多.而且.NET Framework框架很大.拖着一个大大的.net Framework总是让人很郁闷. 在网上找呀找呀.找到另一个.NET Framework 替代方案.Mono. ...
- 前端开发 ECMAScript-1概述
https://www.cnblogs.com/gaoya666/p/8560745.html ECMAScript是一种由Ecma国际(前身为欧洲计算机制造商协会,英文名称是European Com ...
- YII 的SPA 写法
'use strict'; var findToolbar = function () { return document.querySelector('#yii-debug-toolbar'); } ...
- 5_PHP数组_3_数组处理函数及其应用_7_数组排列函数
以下为学习孔祥盛主编的<PHP编程基础与实例教程>(第二版)所做的笔记. 数组排列函数 1. sort() 函数 程序: <?php $array = array("img ...
- dubbo源码阅读之自适应扩展
自适应扩展机制 刚开始看代码,其实并不能很好地理解dubbo的自适应扩展机制的作用,我们不妨先把代码的主要逻辑过一遍,梳理一下,在了解了代码细节之后,回过头再来思考自适应扩展的作用,dubbo为什么要 ...
- iOS - app 进行安全加固
研究了大半年逆向工程了,没在博客做记录,最近看到篇,跟自己的想法不谋而合,摘要下: 运行在越狱设备上的 iOS app,非常容易遭到破解分析,这里我列举一些可以加大破解难度的方法,希望有所帮助. 一些 ...
- C# 后台获取GridView列表的值
int rowIndex = ((GridViewRow)((Button)sender).NamingContainer).RowIndex;//获取gridview中的行号 ...
- 解决Vivado XSDK在Ubuntu系统上自带UART Terminal Crash问题
在Ubuntu 18.04 LTS系统上使用某些版本的Vivado XSDK的Eclipse IDE中自带的串口Terminal会有Crash的问题.Xilinx的XSDK的Terminal插件是用的 ...
- oracle 设置归档日模式
首先关闭ORACLE SQL> shutdown immediate 把ORACLE启动为MOUNT模式 SQL:>startup mount sql:> alter databas ...