组团学

Spring入门

阅读 (738853)

1、Spring概述

1.1、什么是Spring

Spring 是一个主流的 Java Web 开发框架,该框架是一个轻量级的应用框架,具有很高的凝聚力和吸引力。Spring 以 IoC(Inverse of Control,控制反转)和 AOP(Aspect Oriented Programming,面向切面编程)为内核。

在实际开发中,通常服务器端采用三层体系架构,分别为表现层(web)、业务逻辑层(service)、持久层(dao)。Spring 对每一层都提供了技术支持。

1.2、Spring优点

方便解耦,简化开发

Spring 是一个大的工厂,将所有对象的创建、依赖关系和维护都交给 Spring 管理。

方便集成各种优秀框架

Spring 不排斥各种优秀的开源框架,其内部对各种优秀框架都直接支持。

降低 Java EE API 的使用难度

Spring 封装了很多 Java EE 开发的 API都提供了封装,使API 应用的难度大大降低。

方便程序的测试

Spring 支持 JUnit单元测试,可以通过注解方便地测试程序。

AOP 编程的支持

Spring 提供面向切面编程,可以方便地实现对程序进行权限拦截和运行监控等功能。

声明式事务的支持

只需要通过配置就可以完成对事务的管理,而无须手动编程。

1.3、Spring的发展历程

1.3.1、Spring1.x 时代

在Spring1.x时代,都是通过xml文件配置bean,随着项目的不断扩大,需要将xml配置分放到不同的配置文件中,需要频繁的在java类和xml配置文件中切换。

1.3.2、Spring2.x时代

在Spring2.x时代,随着JDK 1.5带来的注解支持,Spring2.x可以使用注解对Bean进行申明和注入,大大的减少了xml配置文件,同时也大大简化了项目的开发。

在实际开发中:我们在使用应用的基本配置时用xml,比如:数据源、资源文件等;在业务开发用注解,比如:Service中注入bean等;

1.3.3、Spring3.x到Spring5.x(现在)

从Spring3.x开始提供了Java配置方式,使用Java配置方式可以更好的理解你配置的Bean。

1.3.4、Spring boot的出现

习惯优于java配置,完全取代的xml。

1.4、Spring体系结构

image20200402093755351.png

Data Access/Integration(数据访问/集成)

数据访问/集成层包括 JDBC、ORM、OXM、JMS 和 Transactions 模块,具体介绍如下。

JDBC 模块:提供了一个 JDBC 的抽象层,大幅度减少了在开发过程中对数据库操作的编码。

ORM 模块:对流行的对象关系映射 API,包括 JPA、JDO、Hibernate 和 MyBatis 提供了的集成层。

OXM 模块:提供了一个支持对象/XML 映射的抽象层实现,如 JAXB、Castor、XMLBeans、JiBX 和 XStream。

JMS 模块:指 Java 消息服务,包含的功能为生产和消费的信息。

Transactions 事务模块:支持编程和声明式事务管理实现特殊接口类,并为所有的 POJO。

Web 模块

Spring 的 Web 层包括 Web、Servlet、Struts 和 Portlet 组件,具体介绍如下。

Web 模块:提供了基本的 Web 开发集成特性,例如多文件上传功能、使用的 Servlet 监听器的 IoC 容器初始化以及 Web 应用上下文。

Servlet模块:包括 Spring 模型—视图—控制器(MVC)实现 Web 应用程序。

Struts 模块:包含支持类内的 Spring 应用程序,集成了经典的 Struts Web 层。

Portlet 模块:提供了在 Portlet 环境中使用 MV C实现,类似 Web-Servlet 模块的功能。

Core Container(核心容器)

Spring 的核心容器是其他模块建立的基础,由 Beans 模块、Core 核心模块、Context 上下文模块和 Expression Language 表达式语言模块组成,具体介绍如下。

Beans 模块:提供了 BeanFactory,是工厂模式的经典实现,Spring 将管理对象称为 Bean。

Core 核心模块:提供了 Spring 框架的基本组成部分,包括 IoC 和 DI 功能。

Context 上下文模块:建立在核心和 Beans 模块的基础之上,它是访问定义和配置任何对象的媒介。ApplicationContext 接口是上下文模块的焦点。

Expression Language 模块:是运行时查询和操作对象图的强大的表达式语言。

其他模块

Spring的其他模块还有 AOP、Aspects、Instrumentation 以及 Test 模块,具体介绍如下。

AOP 模块:提供了面向切面编程实现,允许定义方法拦截器和切入点,将代码按照功能进行分离,以降低耦合性。

Aspects 模块:提供与 AspectJ 的集成,是一个功能强大且成熟的面向切面编程(AOP)框架。

Instrumentation 模块:提供了类工具的支持和类加载器的实现,可以在特定的应用服务器中使用。

Messaging:对消息架构和协议的支持

Test 模块:支持 Spring 组件,使用 JUnit 或 TestNG 框架的测试。

1.5、Spring下载

https://mvnrepository.com/artifact/org.springframework/spring

https://repo.spring.io/release/org/springframework/spring/5.2.5.RELEASE/

image20200402140309305.png

spring依赖的jar包

image20200402101306930.png

1.6、Spring特点

**轻量:**从大小与开销两方面而言Spring都是轻量的。完整的Spring框架可以在一个大小只有1MB多的JAR文件里发布。并且Spring所需的处理开销也是微不足道的。

**控制反转:**Spring通过控制反转(IoC)的技术促进了低耦合。当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象。

**面向切面:**Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务进行内聚性的开发。将业务与系统分离各自完成自己的事情。

**容器:**Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,里面的内容由bean构成。

**框架:**Spring可以将简单的组件配置、组合成为复杂的应用。在xml文件中Spring也提供了很多基础功能(事务管理、持久化框架集成等等),将应用逻辑的开发留给了你。

**MVC:**Spring 使用了MVC的架构模式思想,将Web层进行指责解耦,并管理应用所需的生命周期,为简化日常开发,提供了很大便利。

2、Spring的第一个程序

通过maven创建一个Spring001项目

2.1、jar包导入

pom.xml

<?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.tyschool</groupId> <artifactId>spring001</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.13.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>

2.2、定义接口与实体类

IStudentsService.java

public interface IStudentsService { void some(); }

StudentsServiceImpl.java

public class StudentsServiceImpl implements IStudentsService { public void some() { System.out.println("执行Some方法"); } }

2.3、创建Spring配置文件

applicationContext.xml

<?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"> <bean id="studentsServiceImpl" class="com.tyschool.spring001.service.StudentsServiceImpl"></bean> </beans>

2.4、工厂类

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanFactory { private static ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");; private BeanFactory(){ } public static Object getBean(String snameBean) { Object iss = context.getBean(snameBean); return iss; } }

2.5、测试类

STest.java

package com.tyschoo.spring001.test; import com.tyschool.spring001.factory.BeanFactory; import com.tyschool.spring001.service.IStudentsService; import org.junit.Test; public class STest { @Test public void some(){ IStudentsService iss=(IStudentsService) BeanFactory.getBean("studentsService"); iss.some(); } }

3、内聚与耦合

3.1、模块

模块是由一组语句组成,并且被标识符组成的边界元素所界定。类,方法都是可以称之为一个模块。

3.2、内聚与耦合

内聚是指一个模块内的交互程度,耦合是指模块间的交互程度。我们需要尽力做到高内聚低耦合。

高内聚

比如:211和专科学校他们招生,211招的就是高尖子生,他们得共性就是学生里面的顶尖的一批,专科招的就是学生里面学习能力相对较弱的一批,如果把专科生放到211里面去,那不符合实际,毕竟专科生和211没有多大的关联,符合不了211。

低耦合

比如:你找了一个女朋友,那你们之间就有了联系,这就是耦合,然后呢,你们同居了,你们之间联系更加密切了,这样耦合就高了,然后突然有一天,你女朋友说要回老家了,你俩不同居了,你们的联系呢也少下去了,但是你俩还是男女朋友关系,只不过没有同居的时候联系那样亲密。

3.3、问题解读

3.3.1、业务层调用持久层

public class StudentsServiceImpl implements IStudentsService{ private IStudentsDao isd=new StudetnsDaoImpl(); }

业务层调用持久层,并且此时业务层在依赖持久层的接口和实现类。如果此时没有持久层实现类,编译将不能通过。这种编译期依赖关系,应该在我们开发中杜绝。我们需要优化代码解决。

3.3.2、驱动加载

//第一种方式: DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //第二种方式: Class.forName("com.mysql.jdbc.Driver");

我们的类依赖了数据库的具体驱动类(MySQL),如果这时候更换了数据库品牌(比如 Oracle),需要修改源码来重新数据库驱动。这显然不是我们想要的。

3.4、解耦

3.4.1、解耦思路

前面提到的JDBC连接,我们采用的是反射来注册我们的驱动 Class.forName("com.mysql.jdbc.Driver");//"com.mysql.jdbc.Driver"只是一窜字符串 好处:只是不会影响程序的运行,但不一定会运行成功(去掉mysql驱动jar包)。 问题:驱动字符串是在类中写死的,要想连接其他数据库得改我们的类。 问题解决方案:使用配置文件(.properties/.xml)。

3.4.2、工厂模式解耦

Spring中三层

spring框架中,分为三层dao,service和web层

**dao:**数据库访问层
**service:**业务逻辑层实例dao调用增删查改方法
**web:**前端交互层实例service调用业务逻辑方法

MySQL数据库,加入Oracle数据库

image20200402154111016.png

serivce与dao的耦合度太高

image20200402155456584.png

工厂的耦合太高

image20200402161433458.png

通过工厂模式+配置文件来解耦

4、IOC

4.1、IOC概述

控制反转(IoC,Inversion of Control),是一个概念,是一种思想。指将传统上由程序代码直接操控的对象调用权交给容器,通过容器来实现对象的装配和管理。控制反转就是对对象控制权的转移,从程序代码本身反转到了外部容器。

实现方式有两种: 依赖注入和依赖查找

4.2、依赖倒置

4.2.1、汽车设计-方式一:

image20200402165857902.png

如果我们修改了车轮,那么其他地方都需要修改。

4.2.2、汽车设计-方式二:

image20200402170137506.png

如果我们修改了车轮,那么其他地方都不需要修改。

这就是依赖倒置原则——把原本的上层依赖下层“倒置”过来,变成下层依赖上层。上层来确认需要什么,不用管下层如何实现。

4.3、依赖注入

依赖倒置原则的一种代码设计的思路。具体采用的方法就是所谓的依赖注入(Dependency Injection)。

依赖注入(DI)是指程序运行过程中,若需要调用另一个对象协助时,无须在代码中创建被调用者,而是依赖于外部容器,由外部容器创建后传递给程序。

image20200402171826772.png

4.4、IOC访问

image20200403100545477.png

image20200403100956184.png

4.5、Spring-IOC

image20200403093935974.png

5、Spring第二个程序

通过maven创建一个Spring002项目

5.1、原始方式

5.1.1、jar包导入

pom.xml

<?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.tyschool</groupId> <artifactId>spring002</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.13.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.2.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>

5.1.2、创建javabean

Students.java

import java.io.Serializable; public class Students implements Serializable { private int sid; private String sname; private String ssex; private int sage; private int cid; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public int getSage() { return sage; } public void setSage(int sage) { this.sage = sage; } public int getCid() { return cid; } public void setCid(int cid) { this.cid = cid; } @Override public String toString() { return "Students{" + "sid=" + sid + ", sname='" + sname + '\'' + ", ssex='" + ssex + '\'' + ", sage=" + sage + ", cid=" + cid + '}'; } }

5.1.3、创建持久层接口与实现类

IStudentsDao.java

import com.tyschool.spring002.javabean.Students; import java.util.List; public interface IStudentsDao { public List<Students> findAll(); }

StudentsDaoImpl.java

import com.tyschool.spring002.javabean.Students; import com.tyschool.spring002.students.dao.IStudentsDao; import java.util.List; public class StudentsDaoImpl implements IStudentsDao { public List<Students> findAll() { System.out.println("查询学生所有信息"); return null; } }

5.1.4、创建业务层接口与实现类

IStudentsService.java

import com.tyschool.spring002.javabean.Students; import java.util.List; public interface IStudentsService { public List<Students> findAll(); }

StudentsServiceImpl.java

import com.tyschool.spring002.javabean.Students; import com.tyschool.spring002.students.dao.IStudentsDao; import com.tyschool.spring002.students.dao.impl.StudentsDaoImpl; import com.tyschool.spring002.students.service.IStudentsService; import java.util.List; public class StudentsServiceImpl implements IStudentsService { private IStudentsDao isd=new StudentsDaoImpl(); public List<Students> findAll() { return isd.findAll(); } }

5.2、Spring解耦方式

5.2.1、创建配置文件

applicationContext.xml

<?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"> <!-- 配置Service--> <bean id="studentsServiceImpl" class="com.tyschool.spring002.students.servcie.impl.StudentsServiceImpl"/> <!-- 配置Dao--> <bean id="studentsDaoImpl" class="com.tyschool.spring002.students.dao.impl.StudentsDaoImpl"/> </beans>

image20200403105014634.png

5.2.2、创建工厂

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanFactory { private final static ApplicationContext context=new ClassPathXmlApplicationContext("applicationConfig.xml");; private BeanFactory(){ } public static Object getBean(String snameBean) { Object iss = context.getBean(snameBean); return iss; } }

5.2.3、创建测试类

import com.tyschool.spring002.factory.BeanFactory; import com.tyschool.spring002.students.dao.IStudentsDao; import com.tyschool.spring002.students.service.IStudentsService; public class STest { public static void main(String[] args) { IStudentsService iss= (IStudentsService) BeanFactory.getBean("studentsServiceImpl"); IStudentsDao isd= (IStudentsDao) BeanFactory.getBean("studentsDaoImpl"); System.out.println(iss); System.out.println(isd); } }

5.3、ApplicationContext结构

image20200403143845509.png

ClassPathXmlApplicationContext

它是从类的根路径下加载配置文件。推荐使用这种

FileSystemXmlApplicationContext

它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。

AnnotationConfigApplicationContext

当我们使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。

6、配置文件中的bean

6.1、bean标签

作为 Spring 核心机制的依赖注入,改变了传统的编程习惯,对组件的实例化不再由应用程序完成,转而交由 Spring 容器完成,在需要时注入应用程序中,从而对组件之间依赖关系进行了解耦。这一切都离不开 Spring 配置文件中使用的 元素。

Spring 配置文件支持两种不同的格式,分别是 XML 文件格式和 Properties 文件格式。

通常情况下,Spring 会以 XML 文件格式作为 Spring 的配置文件,这种配置方式通过 XML 文件注册并管理 Bean 之间的依赖关系。

XML 格式配置文件的根元素是 ,该元素包含了多个 子元素,每一个 子元素定义了一个 Bean,并描述了该 Bean 如何被装配到 Spring 容器中。

image20200403145811660.png

6.2、Bean标签作用

用于配置对象让spring来创建的。

默认情况下它调用的是类中的无参构造函数。如果没有无参构造函数则不能创建成功。

6.3、Bean标签属性

id:给对象在容器中提供一个唯一标识。用于获取对象。

class:指定类的权限定类名。用于反射创建对象。默认情况下调用无参构造函数。

scope:指定对象的作用范围。

  • singleton:默认值,单例的。

  • prototype:多例的

init-method:指定类中的初始化方法名称。

destory-method:指定类中销毁方法名称。

6.4、生命周期

6.4.1、singleton值

一个应用只有一个对象的实例。它的作用范围就是整个引用。

生命周期:

对象出生:当应用加载,创建容器时,对象就被创建了。

对象活着:只要容器在,对象一直活着。

对象死亡:当应用卸载,销毁容器时,对象就被销毁了。

6.4.2、prototype值

每次访问对象时,都会重新创建对象实例。

生命周期:

对象出生:当使用对象时,创建新的对象实例。

对象活着:只要对象在使用中,就一直活着。

对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。

6.4.3、测试

import com.tyschool.spring002.factory.BeanFactory; import com.tyschool.spring002.students.service.IStudentsService; public class STest { public static void main(String[] args) { IStudentsService iss= (IStudentsService) BeanFactory.getBean("studentsServiceImpl"); IStudentsService iss1= (IStudentsService) BeanFactory.getBean("studentsServiceImpl"); System.out.println(iss); System.out.println(iss1); } }

7、bean实例化

7.1、无参构造函数

默认情况,根据无参构造函数来创建类对象。如果实现类中没有默认无参构造函数,创建对象失败

<bean id="studentsServiceImpl" class="com.tyschool.spring002.students.service.impl.StudentsServiceImpl"/>

测试

package com.tyschool.test; import com.tyschool.spring002.factory.BeanFactory; import com.tyschool.spring002.students.service.IStudentsService; public class STest { public static void main(String[] args) { IStudentsService iss= (IStudentsService) BeanFactory.getBean("studentsServiceImpl"); System.out.println(iss); } }

7.2、静态工厂

创建一个静态工厂类,使用新的属性:factory-method

StaticBeanFactory.java

import com.tyschool.spring002.students.service.IStudentsService; import com.tyschool.spring002.students.service.impl.StudentsServiceImpl; public class StaticBeanFactory { public static IStudentsService createIStudentsService(){ return new StudentsServiceImpl(); } }
<bean id="studentsServiceImpl1" class="com.tyschool.spring002.factory.StaticBeanFactory" factory-method="createIStudentsService"/>

测试

import com.tyschool.spring002.factory.StaticBeanFactory; import com.tyschool.spring002.students.service.IStudentsService; public class STest { public static void main(String[] args) { IStudentsService iss= (IStudentsService) BeanFactory.getBean("studentsServiceImpl1"); System.out.println(iss); } }

7.3、实例工厂

创建一个实例工厂,使用新的属性:factory-method,factory-bean

ExampleBeanFactory.java

import com.tyschool.spring002.students.service.IStudentsService; import com.tyschool.spring002.students.service.impl.StudentsServiceImpl; public class ExampleBeanFactory { public IStudentsService createIStudentsService(){ return new StudentsServiceImpl(); } }
<bean id="exampleBeanFactory" class="com.tyschool.spring002.factory.ExampleBeanFactory"></bean> <bean id="studentsServiceImpl2" factory-bean="exampleBeanFactory" factory-method="createIStudentsService"></bean>

测试

import com.tyschool.spring002.factory.ExampleBeanFactory; import com.tyschool.spring002.students.service.IStudentsService; public class STest { public static void main(String[] args) { IStudentsService iss= (IStudentsService) BeanFactory.getBean("studentsServiceImpl2"); System.out.println(iss); } }

8、依赖注入实现方式-上

8.1、依赖注入概述

依赖注入(Dependency Injection,DI)和控制反转含义相同,它们是从两个角度描述的同一个概念。

当某个 Java 实例需要另一个 Java 实例时,传统的方法是由调用者创建被调用者的实例(例如,使用 new 关键字获得被调用者实例),而使用 Spring 框架后,被调用者的实例不再由调用者创建,而是由 Spring 容器创建,这称为控制反转。

Spring 容器在创建被调用者的实例时,会自动将调用者需要的对象实例注入给调用者,这样,调用者通过 Spring 容器获得被调用者实例,这称为依赖注入。

8.2、构造函数注入

在StudentsDaoImpl中加入构造方法,初始化属性

import com.tyschool.spring002.javabean.Students; import com.tyschool.spring002.students.dao.IStudentsDao; import java.util.List; public class StudentsDaoImpl implements IStudentsDao { private String sname; private String ssex; private int sage; public StudentsDaoImpl(){} public StudentsDaoImpl(String sname,String ssex,int sage){ this.sname=sname; this.ssex=ssex; this.sage=sage; } public List<Students> findAll() { System.out.println("查询学生所有信息"); System.out.println(sname+":"+ssex+":"+sage); return null; } }

constructor-arg标签:

指定赋值元素:

index:指定参数在构造函数参数列表的索引位置

type:指定参数在构造函数中的数据类型

name:指定参数在构造函数中的名称 用这个找给谁赋值

指定赋值元素值:

value:它能赋的值是基本数据类型和 String 类型

ref:让bean作为值进行赋值,bean必须是配置文件中出现过

<!-- 配置Dao--> <bean id="studentsDaoImpl" class="com.tyschool.spring002.students.dao.impl.StudentsDaoImpl" > <constructor-arg name="sname" value="张三"></constructor-arg> <constructor-arg name="ssex" value="男"></constructor-arg> <constructor-arg name="sage" value="19"></constructor-arg> </bean>
<!-- 配置Service--> <bean id="studentsServiceImpl" class="com.tyschool.spring002.students.service.impl.StudentsServiceImpl"> <property name="isd" ref="studentsDaoImpl"></property> </bean>

修改StudentsServiceImpl.java文件

import com.tyschool.spring002.javabean.Students; import com.tyschool.spring002.students.dao.IStudentsDao; import com.tyschool.spring002.students.service.IStudentsService; import java.util.List; public class StudentsServiceImpl implements IStudentsService { private IStudentsDao isd; public IStudentsDao getIsd() { return isd; } public void setIsd(IStudentsDao isd) { this.isd = isd; } //private IStudentsDao isd=new StudentsDaoImpl(); public List<Students> findAll() { return isd.findAll(); } }

测试

import com.tyschool.spring002.factory.BeanFactory; import com.tyschool.spring002.students.service.IStudentsService; public class STest { public static void main(String[] args) { IStudentsService iss= (IStudentsService) BeanFactory.getBean("studentsServiceImpl"); System.out.println(iss); iss.findAll(); } }

9、依赖注入实现方式-中

9.1、set注入

修改StudentsDaoImpl.java文件

import com.tyschool.spring002.javabean.Students; import com.tyschool.spring002.students.dao.IStudentsDao; import java.util.List; public class StudentsDaoImpl implements IStudentsDao { private String sname; private String ssex; private int sage; public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public int getSage() { return sage; } public void setSage(int sage) { this.sage = sage; } public List<Students> findAll() { System.out.println("查询学生所有信息"); System.out.println(sname+":"+ssex+":"+sage); return null; } }

property标签:

属性与值

name:找的是类中 set 方法后面的部分

ref:给属性赋值是其他 bean 类型的

value:给属性赋值是基本数据类型和 string 类型的

<bean id="studentsDaoImpl" class="com.tyschool.spring002.students.dao.impl.StudentsDaoImpl"> <property name="sname" value="张三"></property> <property name="ssex" value="女"></property> <property name="sage" value="19"></property> </bean>

测试类不变继续测试

9.2、空间注入

使用 p:propertyName 来注入数据,它是调用类中的set 方法实现注入功能

在beans里引入p

xmlns:p="http://www.springframework.org/schema/p" 

在bean里使用p

<bean id="studentsDaoImpl" class="com.tyschool.spring002.students.dao.impl.StudentsDaoImpl" p:sname="李四" p:ssex="女" p:sage="19"> </bean>

测试类不变继续测试

10、依赖注入实现方式-下

10.1、注入类、数组、集合、参数

修改StudentsDaoImpl.java文件

import com.tyschool.spring002.javabean.Students; import com.tyschool.spring002.students.dao.IStudentsDao; import java.util.*; public class StudentsDaoImpl implements IStudentsDao { private Date date; private String []s; private List<String> list; private Set<String> set; private Map<String,String> map; private Properties properties; public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String[] getS() { return s; } public void setS(String[] s) { this.s = s; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Set<String> getSet() { return set; } public void setSet(Set<String> set) { this.set = set; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public List<Students> findAll() { System.out.println("查询学生所有信息"); //System.out.println(sname+":"+ssex+":"+sage); System.out.println(date); System.out.println(s); System.out.println(list); System.out.println(set); System.out.println(map); System.out.println(properties); return null; } }

property标签:

子标签:array,list,set,map,entry,props,prop

<bean id="date" class="java.util.Date"></bean> <bean id="studentsDaoImpl" class="com.tyschool.spring002.students.dao.impl.StudentsDaoImpl"> <property name="date" ref="date"></property> <property name="s"> <array> <value>AAA</value> <value>BBB</value> <value>CCC</value> </array> </property> <property name="list"> <list> <value>DDD</value> <value>EEE</value> <value>FFF</value> </list> </property> <property name="set"> <set> <value>GGG</value> <value>HHH</value> <value>III</value> </set> </property> <property name="map"> <map> <entry key="j" value="JJJ"></entry> <entry key="k" value="KKK"></entry> <entry key="l" value="LLL"></entry> </map> </property> <property name="properties"> <props> <prop key="m">MMM</prop> <prop key="n">NNN</prop> <prop key="o">OOO</prop> </props> </property> </bean>

测试类不变继续测试

需要 登录 才可以提问哦