1.看图(三种注入数据方式)
2.IAccountService业务层接口
package com.itheima.service;
public interface IAccountService {
void saveAccount();
}
3.IAccountServiceImp带参数的构造函数注入,作用输出数据
package com.itheima.service.lmp;
import com.itheima.service.IAccountService;
import java.util.Date;
public class IAccountServiceImp implements IAccountService {
//业务层调用——持久层——调用控制层
private String name;
private Integer age;
private Date birth;
//构造函数IAccountServiceImp 一模一样
public IAccountServiceImp(String name,Integer age , Date birth) {
this.name = name;
this.age = age;
this.birth = birth;
}
public void saveAccount() {
System.out.println("service中的saveAccount被执行了......"+name+" "+age+" "+birth);
}
}
4.set方法依赖注入IAccountServiceImp2
package com.itheima.service.lmp;
import com.itheima.service.IAccountService;
import java.util.Date;
public class IAccountServiceImp2 implements IAccountService {
//业务层调用——持久层——调用控制层
private String name;
private Integer age;
private Date birth;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirth(Date birth) {
this.birth = birth;
}
//执行方法
public void saveAccount() {
System.out.println("service中的saveAccount被执行了......"+name+" "+age+" "+birth);
}
}
5.复杂类型的set注入IAccountServiceImp3
package com.itheima.service.lmp;
import com.itheima.service.IAccountService;
import java.util.*;
public class IAccountServiceImp3 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);
}
}
6.pom依赖导入
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
7.bean文件的创建对象
并且注入依赖
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- id:对象名(也是唯一标识)class:全限定类名-->
<!-- 传入的是每个层的实现类,因为使用实现类才能创建实例对象-->
<!-- <bean id="accountService" class="com.itheima.service.lmp.IAccountServiceImp"></bean>-->
<!--
spring中的依赖注入
依赖注入:
dependency Injection
IOC的作用:
降低程序间的耦合(依赖关系)
依赖关系的管理:
以后都交给spring来维护
在当前类需要用到其他类对象,统一由spring为我们提供,我们只需要在配置文件中说明(在配置文件中告诉spring,我们使用哪个类来创建对象)
依赖关系的维护:
就称之为依赖注入。
依赖注入:
能注入的数据:有三类
基本类型和string
其他bean类型(在配置文件中或者注解配置过的bean)
复杂类型/集合类型
注入的方式:有三种
第一种:使用构造函数提供
第二种:使用set方法提供
第三种:使用注解提供(明天的内容)
-->
<!--
第一种:使用构造函数注入:
使用的标签:constructor-org
标签出现的位置:bean标签的内部
标签中的属性
type: 用于指定要注入数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型 (不能唯一指定:如果两个string 就会解决不了这个问题)
index:用于指定要注入的数据给构造函数中指定的索引位置的参数赋值。索引是从开始 (使用数字索引开始)
name:用于指定给构造函数中指定名称的参数赋值 (经常使用)(自己写入构造函数中的名字name=“变量参数名”)
+++++++++++++++以上三个用于指定给构造函数中的哪个参数赋值+++++++++++++++++++++++++
value:用于提供基本数据类型和string类型的数据 (用于给参数赋值,但是有局限,date等其他类型不允许)
ref:用于指定其他的bean类型数据。它指的就是在spring的IOC核心容器中出现过的bean对象
优势:在获取bean对象的时候,注入数据是必须的操作,否则对象无法创建成功。
弊端:改变了bean对象的实例化方式,是我们在创建对象时,如果用不到这些数据,也必须提供
总结:也就是说,构造函数的依赖注入是需要带参数的,如果有数据的话。有数据 没有参数 就无法创建对象来使用
-->
<bean id="accountService" class="com.itheima.service.lmp.IAccountServiceImp">
<constructor-arg name="name" value="test"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="birth" ref="now"></constructor-arg>
</bean>
<!-- 使用jar创建对象now 配置一个日期对象 从java.util.Date里面创建一个now日期对象处理-->
<bean id="now" class="java.util.Date"></bean>
<!--
第二种:使用set方法注入(更常用)
涉及的标签:property
出现的位置:bean标签的内部
标签的属性:
name:用于指定注入时所调用的set方法名称(小写)
value:用于提供基本数据类型和string类型的数据 (用于给参数赋值,但是有局限,date等其他类型不允许)
ref:用于指定其他的bean类型数据。它指的就是在spring的IOC核心容器中出现过的bean对象
1.基本数据类型:
-->
<!-- <bean id="accountService2" class="com.itheima.service.lmp.IAccountServiceImp2">-->
<!--<!– name指定的为set方法名–>-->
<!-- <property name="name" value="test02"></property>-->
<!-- <property name="age" value="18"></property>-->
<!-- <property name="birth" value="now"></property>-->
<!-- </bean>-->
<!-- <bean id="now" class="java.util.Date"></bean>-->
<!--
优势:创建对象没有明确的限制,可以直接使用默认构造函数
弊端:如果某个成员必须有值,则获取的对象是可能set方法不执行
综合:也就是说,创建对象没有限制,有方法就有对象(不存在特定值),但是如果set方法不执行,那么就会就是有特定值的情况
综上:有没有例子
-->
<!--2.复杂类型的注入集合类型注入
实现类使用:
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
bean中:
用于给List结构集合注入的标签:
List array set
用于给Map结构集合注入的标签:
map props
结构相同,标签可以互换
-->
<bean id="accountService3" class="com.itheima.service.lmp.IAccountServiceImp3">
<!--array的-->
<property name="myStrs">
<array><!--可以用list作为标签-->
<value>AA</value>
<value>BB</value>
<value>CC</value>
</array>
</property>
<!--list的-->
<property name="myList">
<list>
<value>AA</value>
<value>BB</value>
<value>CC</value>
</list>
</property>
<!--Set的-->
<property name="mySet">
<set><!--可以用list作为标签-->
<value>AA</value>
<value>BB</value>
<value>CC</value>
</set>
</property>
<!--Map的-->
<property name="myMap">
<map>
<entry key="keyA" value="AAAA"></entry>
<entry key="keyB">
<value>BBBB</value>
</entry>
</map>
</property>
<!--myProps的-->
<property name="myProps">
<props>
<prop key="testc">CCCC</prop>
<prop key="tab">只能是标签体的内容</prop>
</props>
</property>
</bean>
</beans>
8.表现层Client
package com.itheima.ui;
import com.itheima.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// IAccountService as = (IAccountService) ac.getBean("accountService");
// IAccountService as = (IAccountService) ac.getBean("accountService2");
IAccountService as = (IAccountService) ac.getBean("accountService3");
as.saveAccount();
}
/*
*dao: Client调用IAccountServiceImp调用IAccountDaoImp(创建实例化对象的时候)
* service:业务返回:IAccountDao ——>IAccountService——>显示Client
*/
/*IAccountService as = new IAccountServiceImp();普通用法:独立就用spring容器*/
}
9.
把有依赖关系的类放到容器中,解析出这些类的实例,就是依赖注入。目的是实现类的解耦。
实例:Class A中用到了Class B的对象b,一般情况下,需要在A的代码中显式的new一个B的对象。采用依赖注入技术之后,A的代码只需要定义一个私有的B对象,不需要直接new来获得这个对象,而是通过相关的容器控制程序来将B对象在外部new出来并注入到A类里的引用中。这样做有什么好处呢?
解释:假如现在有N多个类,需要用到Class B,那就需要在类里面实例化N多次,这样对于后期的维护和管理都是不方便的,如果后期需求发生改变,那更改量有大很多。最后可以再改进,优化