昨天上午我结束了对mybatis内容的大致学习,今天开始对SpringIOC容器的学习,则反转控制,降低类与类之间的依赖关系
<?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>cn.kitey</groupId>
<artifactId>demo1_eesy_DI</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
</dependencies>
</project>
业务层接口:
package cn.kitey.service;
/**
* 账户业务层的接口
*/
public interface IAccountService {
/**
* 模拟保存账户
*/
void saveAccount();
}
业务层的实现类:
package cn.kitey.service.impl;
import cn.kitey.service.IAccountService;
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements IAccountService {
public AccountServiceImpl() {
System.out.println("AccountServiceImpl对象创建了!");
}
public void saveAccount(){
System.out.println("service中的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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--把对象的创建交给spring来管理-->
<!--
spring对bean 的管理细节
1.创建bean的三种方式
2.bean对象的作用范围
3.bean对象的生命周期
-->
<!--
bean的作用范围的调整
bean标签的scope属性:
作用:用于指定bean的作用范围
取值:
singleton:表示单例的(默认值)
prototype:多例的
request:作用域web的请求范围
session:作用于web应用的会话范围
global-session:作用去全局的会话范围
-->
<!--
bean对象的生命周期
单例对象
出生:容器创建对象时出生
活着:只要容器还在,对象一直活着
死亡:容器销毁,对象消亡
总结:单例对象的生命周期与容器相同
多例对象
出生:当我们使用对象时spring框架为我们创建
活着:对象只要在使用的时候一直存在
死亡:长时间不调用,由java垃圾回收器回收
-->
<bean id="accountService" class="cn.kitey.service.impl.AccountServiceImpl"
scope="singleton"
></bean>
<!--创建bean 的三种方式-->
<!--
1.使用默认构造函数进行创建,在spring配置文件中的使用了bean标签,配了id,class属性之后,且没有其他属性
标签时,采用的就是默认构造函数创建bean对象,如果此时没有默认构造函数,则无法创建
-->
<!--
2.使用普通工厂中的方法创建对象(使用莫个类中的方法创建对象,并存入spring容器中)
<bean id="instanceFactory" class="cn.kitey.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
-->
<!--
3.使用工厂中的静态方法创建对象
<bean id="accountService" class="cn.kitey.factory.staticFactory" factory-method="getAccountService"></bean>
-->
</beans>
package cn.kitey.ui;
import cn.kitey.service.impl.AccountServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
//1.获取核心容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
AccountServiceImpl service = context.getBean("accountService", AccountServiceImpl.class);
AccountServiceImpl service1 = context.getBean("accountService",AccountServiceImpl.class);
System.out.println(service1);
System.out.println(service);
System.out.println(service == service1);
}
}
其中我还在bean.xml中 bean中使用了scope:进行设定bean的使用范围
依赖注入:Dependency Injection。它是 spring 框架核心 ioc 的具体实现。 我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。 ioc 解耦只是降低他们的依赖关系,但不会消除。例如:我们的业务层仍会调用持久层的方法。 那这种业务层和持久层的依赖关系,在使用 spring 之后,就让 spring 来维护了。 简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。
package cn.kitey.service.impl;
import cn.kitey.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);
}
}
<bean id="accountService" class="cn.kitey.service.impl.AccountServiceImpl"
scope="singleton">
<constructor-arg name="name" value="赵云"></constructor-arg>
<constructor-arg name="age" value="12"></constructor-arg>
<constructor-arg name="birthday" ref="date"></constructor-arg>
</bean>
<!--配置一个日期对象-->
<bean id="date" class="java.util.Date"></bean>
package cn.kitey.ui;
import cn.kitey.service.impl.AccountServiceImFuzai;
import cn.kitey.service.impl.AccountServiceImpl;
import cn.kitey.service.impl.AccountServiceImplSet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
//1.获取核心容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
AccountServiceImpl service = context.getBean("accountService", AccountServiceImpl.class);
service.saveAccount();
}
}
package cn.kitey.service.impl;
import cn.kitey.service.IAccountService;
import java.util.Date;
/**
* 账户的业务层实现类
*/
public class AccountServiceImplSet 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);
}
}
set方法注入 常用的方法
使用标签:property
标签属性:
name:用于注入数据的set方法
value:给基本类型赋值
ref:进行引用赋值,则ioc用其中配置过的bean对象
<bean id="accountServiceSet" class="cn.kitey.service.impl.AccountServiceImplSet">
<property name="name" value="张飞"></property>
<property name="age" value="44"></property>
<property name="birthday" ref="date"></property>
</bean>
package cn.kitey.ui;
import cn.kitey.service.impl.AccountServiceImFuzai;
import cn.kitey.service.impl.AccountServiceImpl;
import cn.kitey.service.impl.AccountServiceImplSet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
//1.获取核心容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
AccountServiceImpl service = context.getBean("accountService", AccountServiceImpl.class);
AccountServiceImplSet service1 = context.getBean("accountServiceSet", AccountServiceImplSet.class);
//service.saveAccount();
service1.saveAccount();
}
}
运行结果图:
package cn.kitey.service.impl;
import cn.kitey.service.IAccountService;
import java.util.*;
/**
* 账户的业务层实现类
*/
public class AccountServiceImFuzai implements IAccountService {
private String[] myStr;
private List<String>myList;
private Set<String> mySet;
private Map<String, String> myMap;
private Properties myPro;
public void setMyStr(String[] myStr) {
this.myStr = myStr;
}
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 setMyPro(Properties myPro) {
this.myPro = myPro;
}
public void saveAccount(){
System.out.println(
"AccountServiceImFuzai{" +
"myStr=" + Arrays.toString(myStr) +
", myList=" + myList +
", mySet=" + mySet +
", myMap=" + myMap +
", myPro=" + myPro +
'}');
}
}
<bean id="accountServiceFuzai" class="cn.kitey.service.impl.AccountServiceImFuzai">
<property name="myStr">
<array>
<value>aaa</value>
<value>bba</value>
</array>
</property>
<property name="myList">
<list>
<value>qqqq</value>
<value>kite</value>
</list>
</property>
<property name="mySet">
<list>
<value>qqqq</value>
<value>kite</value>
</list>
</property>
<property name="myMap">
<map>
<entry key="11" value="状语"></entry>
<entry key="11" value="状语"></entry>
</map>
</property>
<property name="myPro">
<props>
<prop key="kee"> test</prop>
<prop key="kee11"> 1test</prop>
</props>
</property>
package cn.kitey.ui;
import cn.kitey.service.impl.AccountServiceImFuzai;
import cn.kitey.service.impl.AccountServiceImpl;
import cn.kitey.service.impl.AccountServiceImplSet;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
//1.获取核心容器对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
AccountServiceImpl service = context.getBean("accountService", AccountServiceImpl.class);
AccountServiceImplSet service1 = context.getBean("accountServiceSet", AccountServiceImplSet.class);
AccountServiceImFuzai fuzai = context.getBean("accountServiceFuzai", AccountServiceImFuzai.class);
//
fuzai.saveAccount();
//service.saveAccount();
// service1.saveAccount();
}
}
上午的时间学了DI依赖注入,还用IOC容器的相关知识
去吃螺狮粉了 哈哈
2020/6/13/12:22