发表于: 2020-05-07 21:09:44

1 1405


今天完成的事情:继续Spring的学习

一、IOC的环境搭建

1、创建Maven项目,在POM上添加依赖

<dependencies>
   <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
       <version>5.2.5.RELEASE</version>
   </dependency>
</dependencies>

 手动下载jar包地址(第一次打开的速度非常慢,以至于让我认为网页不存在)

 https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring

项目所需要用到的jar包

2、在resources目录下创建xxx.xml配置文件

配置文件内容如下 id任意,class为包名

<?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
       https://www.springframework.org/schema/beans/spring-beans.xsd">
   <!--把对象的创建交给Spring管理-->
   <bean id = "accountService" class="com.itxzy.service.impl.AccountServiceImpl"></bean>

   <bean id = "accountDao" class="com.itxzy.dao.impl.AccountDaoImpl"></bean>
</beans>

3、在ui/Client目录下创建类

步骤:①、获取核心容器对象

          ②、根据配置文件id 获取Bean 对象

public class Client {
public static void main(String[] args) {
//1.获取核心容器对象
       ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
       IAccountService as = (IAccountService)ac.getBean("accountService");
       IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);
       System.out.println(as);
       System.out.println(adao);

4、 ApplicationContext的三个常用实现类

          ClassPathXmlApplicationContext:可加载路径下的配置文件


       FilesSystemXmlApplicationContext:可加载磁盘任意路径下的配置文件,一定要有访问权限


   AnnotationConfigApplicationContext:用于读取注解创建容器

核心容器的两个接口引发的问题:


①、ApplicationContext(单例对象使用)

      在构建核心容器时,创建对象采用的策略是采用立即加载的方式(只要读取文件就马上创建配置文件中的配置的对象)

②、BeanFactory:(多例对象使用)

     在创建核心容器时,采用对象采采取的策略时采用延迟加载的方式(也就是说,什么时候根据id获取对象了,什么时候才真正的创建         对 象)

二、依赖注入(Dependency Injection)

1、概念

IOC的作用是降低依赖关系,依赖关系的管理都交给spring维护,在当前类需要用到其他类的对象,由spring提供,开发者只需要在配置文件中说明依赖关系的维护,这个过程叫做依赖注入

2、能注入的数据类型,有三类

①、基本类型和String

②、其他bean类型(在配置文件中或者注解配置过的Bean)

③、复杂类型/集合类型


3、注入方式有三种

①、使用构造函数提供

②、使用set方法提供

③、使用注解提供


构造函数的注入:     使用的标签:constructor-arg

                                 标签出现的位置:bean标签的内部

                                 标签中属性

                                 type  :用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型

                                 index:用于指定要注入的数据结构构造函数中指定索引位置的参数赋值,索引的位置是从0开始
                                 name:用于指定给构造函数中指定名称的参数赋值(常用)


                                 value: 用于提供基本类型和String类型的数据

                                 ref:  用于指定其他的bean类型数据。它值得就是在Spring的IOC核心容器中出现过的bean对象

配置文件:

    <bean id = "accountService" class="com.itxzy.service.impl.AccountServiceImpl">
       <constructor-arg name="name" value="test"></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>

</beans>

读取一个全限定类名反射创建一个对象并存储到Spring的 核心容器中,可通过id now 来吧对象取出来

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对象时,注入数据时必须的操作,否则对象无法创建成功。

弊端:改变了bean对象的实例化方式,使开发者在创建对象时,如果用不到这些数据,也必须提供



注入只需要set方法,不需要get方法     

set方法的注入使用的标签:property(常用)

                                 标签出现的位置:bean标签的内部

                                 标签的属性:

                                  name:用于指定注入时所调用的set方法名称(常用)


                                 value:用于提供基本类型和String类型的数据

                                 ref:用于指定其他的bean类型数据。它值得就是在Spring的IOC核心容器中出现过的bean对象

配置文件

<bean id = "accountService2" class="com.itxzy.service.impl.AccountServiceImpl2">
   <property name="name" value="TEST"></property>
   <property name="age" value="21"></property>
   <property name="birthday" ref="now"></property>
</bean>

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);
   }
}

结果如下


优势:创建对象时没有明确的限制,可以直接使用默认构造函数

弊端:如果有某个成员必须有值,则获取对象是有可能set方法没有执行 


构造函数注入与set方法注入的优势与弊端刚好相反,在实际的开发中,常用set方法注入 

明天计划的事情:继续Spring的学习


返回列表 返回列表
评论

    分享到