spring的依赖注入
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.learn</groupId><artifactId>day01_learn_05DI</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency></dependencies>
</project>
package com.learn.ui;import com.learn.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 模拟一个表现层,用于调用业务层*/
public class Client {/**** @param args*/public static void main(String[] args) {//1.获取核心容器对象ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.根据id获取Bean对象
// IAccountService as = (IAccountService)ac.getBean("accountService");
// as.saveAccount();// IAccountService as = (IAccountService)ac.getBean("accountService2");
// as.saveAccount();IAccountService as = (IAccountService)ac.getBean("accountService3");as.saveAccount();}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- spring中的依赖注入依赖注入:Dependency InjectionIOC的作用:降低程序间的耦合(依赖关系)依赖关系的管理:以后都交给spring来维护在当前类需要用到其他类的对象,由spring为我们提供,我们只需要在配置文件中说明依赖关系的维护:就称之为依赖注入。依赖注入:能注入的数据:有三类基本类型和String其他bean类型(在配置文件中或者注解配置过的bean)复杂类型/集合类型注入的方式:有三种第一种:使用构造函数提供第二种:使用set方法提供第三种:使用注解提供(明天的内容)--><!--构造函数注入:使用的标签:constructor-arg标签出现的位置:bean标签的内部标签中的属性type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从0开始name:用于指定给构造函数中指定名称的参数赋值 常用的=============以上三个用于指定给构造函数中哪个参数赋值===============================value:用于提供基本类型和String类型的数据ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象优势:在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。弊端:改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。--><bean id="accountService" class="com.learn.service.impl.AccountServiceImpl"><constructor-arg name="name" value="泰斯特"></constructor-arg><constructor-arg name="age" value="18"></constructor-arg><constructor-arg name="birthday" ref="now"></constructor-arg></bean><!-- 配置一个日期对象 --><bean id="now" class="java.util.Date"></bean><!-- set方法注入 更常用的方式涉及的标签:property出现的位置:bean标签的内部标签的属性name:用于指定注入时所调用的set方法名称value:用于提供基本类型和String类型的数据ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象优势:创建对象时没有明确的限制,可以直接使用默认构造函数弊端:如果有某个成员必须有值,则获取对象是有可能set方法没有执行。--><bean id="accountService2" class="com.learn.service.impl.AccountServiceImpl2"><property name="name" value="TEST" ></property><property name="age" value="21"></property><property name="birthday" ref="now"></property></bean><!-- 复杂类型的注入/集合类型的注入用于给List结构集合注入的标签:list array set用于个Map结构集合注入的标签:map props结构相同,标签可以互换--><bean id="accountService3" class="com.learn.service.impl.AccountServiceImpl3"><property name="myStrs"><set><value>AAA</value><value>BBB</value><value>CCC</value></set></property><property name="myList"><array><value>AAA</value><value>BBB</value><value>CCC</value></array></property><property name="mySet"><list><value>AAA</value><value>BBB</value><value>CCC</value></list></property><property name="myMap"><props><prop key="testC">ccc</prop><prop key="testD">ddd</prop></props></property><property name="myProps"><map><entry key="testA" value="aaa"></entry><entry key="testB"><value>BBB</value></entry></map></property></bean>
</beans>
package com.learn.service;/*** 账户业务层的接口*/
public interface IAccountService {/*** 模拟保存账户*/void saveAccount();
}
package com.learn.service.impl;import com.learn.service.IAccountService;import java.util.Date;/*** 账户的业务层实现类*/
public class AccountServiceImpl implements IAccountService {//如果是经常变化的数据,并不适用于注入的方式private String name;private Integer age;private Date birthday;public AccountServiceImpl(String name,Integer age,Date birthday){this.name = name;this.age = age;this.birthday = birthday;}public void saveAccount(){System.out.println("service中的saveAccount方法执行了。。。"+name+","+age+","+birthday);}}
package com.learn.service.impl;import com.learn.service.IAccountService;import java.util.Date;/*** 账户的业务层实现类*/
public class AccountServiceImpl2 implements IAccountService {//如果是经常变化的数据,并不适用于注入的方式private String name;private Integer age;private Date birthday;public void setName(String name) {this.name = name;}public void setAge(Integer age) {this.age = age;}public void setBirthday(Date birthday) {this.birthday = birthday;}public void saveAccount(){System.out.println("service中的saveAccount方法执行了。。。"+name+","+age+","+birthday);}}
package com.learn.service.impl;import com.learn.service.IAccountService;import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.Map;/*** 账户的业务层实现类*/
public class AccountServiceImpl3 implements IAccountService {private String[] myStrs;private List<String> myList;private Set<String> mySet;private Map<String,String> myMap;private Properties myProps;public void setMyStrs(String[] myStrs) {this.myStrs = myStrs;}public void setMyList(List<String> myList) {this.myList = myList;}public void setMySet(Set<String> mySet) {this.mySet = mySet;}public void setMyMap(Map<String, String> myMap) {this.myMap = myMap;}public void setMyProps(Properties myProps) {this.myProps = myProps;}public void saveAccount(){System.out.println(Arrays.toString(myStrs));System.out.println(myList);System.out.println(mySet);System.out.println(myMap);System.out.println(myProps);}}
spring的依赖注入相关推荐
- factorybean 代理类不能按照类型注入_《Spring入门经典》:使用Spring进行依赖注入
第二章:使用Spring进行依赖注入 重点:配置并使用Spring容器 使用不同类型的配置元数据来配置Spring容器 理解依赖解析 了解自动装配的优缺点 在容器中执行显式Bean查找 学习不同的Be ...
- 在ABAP里模拟实现Java Spring的依赖注入
Dependency Injection- 依赖注入,在Java Spring框架中有着广泛地应用.通过依赖注入,我们不必在应用代码里繁琐地初始化依赖的资源,非常方便. 那么ABAP能否从语言层面上也 ...
- Spring Setter依赖注入示例
学习如何编写Spring Setter依赖注入示例 . Setter注入是Spring依赖注入的一种 . Spring支持字段注入,Setter注入以及构造函数注入,以将依赖项注入Spring托管的b ...
- Spring字段依赖注入示例
学习如何编写Spring Field Injection示例 . 字段注入是Spring框架 依赖注入的一种 . 在本教程中,我们将编写几个类,并看一看现场注入工程. 有关Spring依赖注入的更多信 ...
- 据说,80%的人没有真正理解了Spring的依赖注入
前言 提起Spring,大家肯定不陌生,它是每一个Java开发者绕不过去的坎.Spring 框架为基于 java 的企业应用程序提供了一整套解决方案,方便开发人员在框架基础快速进行业务开发. 在官网中 ...
- Spring框架----Spring的依赖注入
1.spring的依赖注入的概念 依赖注入:dependency Injection IOC的作用:降低程序之间的依赖关系,但不是消除. 依赖关系的管理:以后都交给了spring来维护 在当前类中需要 ...
- Spring和依赖注入的价值
javaeye上看到有帖子,置疑spring和依赖注入的价值,回复内容整理如下: 依赖注入对设计有利,而spring则促进了依赖注入的使用. 如果业务处理类,它所使用的倚赖,都是依靠在这个类内部实现或 ...
- spring(一)依赖注入与 SPEL
Spring之依赖注入与 SPEL 一.控制反转与依赖注入 二.helloworld 三.构造注入 四.级联注入 五.单例与多例 六.工厂方法创建 Bean 七.包扫描管理 bean 八.SPEL与资 ...
- Spring实现依赖注入的几种方式
Spring实现依赖注入的几种方式 1.基于有参构造实现 <bean id="user" class="com.ccu.twj"><const ...
- 【Spring】依赖注入的几种方式
在上篇文章中我着重介绍了Spring的控制反转和依赖注入的概念,那么依赖注入有那几种方式呢?他们的优缺点分别是什么,我将在本章中详细讲解. Spring的依赖注入根据对象类型注入可以分为属性注入和对象 ...
最新文章
- IDEA创建springboot项目:Unable to import maven project: See logs for details
- 用户中心 - 查询用户信息
- SQLServer之函数简介 1
- QtCreator下使用c++标准输入cin输出cout没有阻塞等待输入
- 清楚浮动的方法和原理
- 携号转网时间明确了: 下半年在全国实施!
- android viewpager 详解,详解Android App中ViewPager使用PagerAdapter的方法
- linux下如何添加一个用户并且让用户获得root权限
- ai怎么画路线_AI换脸的本质是把颜值和表情分开
- spring中for循环中事务
- GPO组策略 权限处理之原则
- hudson--插件管理
- 三国志2017攻略 服务器维护,三国志2017新手攻略少走弯路
- C/C++编程学习 - 第19周 ③ 不与最大数相同的数字之和
- ViewPager中呈现前一页和后一页的内容
- android7.0 8.1 9.0 10.0 去掉屏幕锁屏(屏幕默认锁屏方式改成无)
- Objective C 常用代码片段制作(code snippet library)
- Node.js 运行.js文件出现错误找不到文件的解决办法
- calc():css简单的数学运算–加减乘除
- GOOGLE地图基站定位-Google Mobile Maps API