目的
• 了解 Spring 环境配置。
• 掌握定义应用程序类。
• 掌握编辑配置文件创建 bean 实例实现依赖注入。
• 掌握编辑测试类创建 IoC 容器,通过容器获取 bean 实例。
内容
创建一个Spring项目,完成主题为“订单信息通知”项目,每收到一笔订单后,系统给客户发送订单信息的通知。通知发送由两种方式:一种是手机短信;另一种是微信。
要求采用两种 Bean 装配方式:
方式一:
A、基于 XML 的 Bean 装配,项目运行结果如图所示:
环境和工具
PC 机+Eclipse(企业版)+Maven
实现步骤
PartA:基于 XML 的 Bean 装配
1.、在 Eclipse 环境中创建一个项目名称为“Lab3Project” 的 Maven Project
2.、首先在 pom.xml 文件中先添加 spring-context 依赖,再添加 lombok依赖,具体内容见附录:源代码区域。
3、在 src\main\java 目录下创建 Spring\Dao 两级子文件夹。
4、在 src\main\java\Spring\Dao 目录下创建一个订单信息类的 Order.Java 文件,在其里面为order类三个成员变量赋值的Setter方法、无参构造方法和全参构造方法的定义。
5、在 src\main\java\Spring\Dao 目录下创建一个通知接口的 NotifyDao.Java 文件。
6、在 src\main\java\Spring\Dao 目录下创建 Impl 文件夹。
7、在 src\main\java\Spring\Dao\Impl 目录下创建一个通知接口的实现类的
NotifyDaoByCellPhoneImpl.Java 文件。
8、在 src\main\java\Spring\Dao\Impl 目录下创建一个通知接口的实现类的
NotifyDaoByWeixinImpl.Java 文件。
9、在 src\main\java\Spring 目录下创建 Service 文件夹。
10、在 src\main\java\Spring\Service 目录下创建一个通知接口的实现类的 OrderNotify.Java
文件,在其里面为OrderNotify类三个成员变量赋值的Setter方法、无参构造方法和全参构造方法的定义。
11、在 src/main 文件夹下新建一个 resources 文件夹。
12、在 src/main/resources 文件夹下新建一个 applicationContext.xml 文件。
13、在 applicationContext.xml 文件中创建 bean 实例,实现依赖注入,并声明。
14、在 src\main\java\Spring 目录下创建一个 Test 文件夹。
15、在 src\main\java\Spring\Test 目录下创建一个文件名称为 TestSpring.java 的文件,创建Spring 容器,实现控制反转(IoC)。
16、运行该项目查看运行结果。
心得
通过本次实验我学习巩固了创建Maven Java Web 项目,使用Maven 项目构建工具更加熟练,了解了Spring 环境配置,Spring贯穿了表现层、业务层、持久层,为企业的应用开发提供了一个轻量级的解决方案,包括了基于依赖注入的核心机制,基于AOP的声明式事务管理,与多种持久层技术的整合,以及优秀的Web MVC框架等等,Spring采用反射的方式,在运行时动态的生成相应的Bean。
Spring框架有很多优点,像低侵入式设计,代码的污染极低,是独立于各种应用服务器的,开放性良好,开发者可以自由选用Spring框架的部分或全部。Spring框架系统的结构中核心容器包括了Core、Bean、Expression Language、Context模块。Spring的使用流程一般为先编写一个Java类,再给Spring容器导入Bean,添加Spring Context依赖,最后编写测试程序并运行。Spring默认配置文件为applicationContext.xml文件。
部分关键代码:
PartA:基于 XML 的 Bean 装配
pom.xml
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
Order.java
package Spring.Dao;
import lombok.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order {
String id;
String clientName;
float money;
public String toString() {
return "客户: "+clientName+",完成订单:"+id+"付款,共人民币:"+money+"元";
}
}
NotifyDao.java
package Spring.Dao;
public interface NotifyDao {
void sendMessage(String message);
}
NotifyDaoByCellPhoneImpl.java
package Spring.Dao.Impl;
import Spring.Dao.NotifyDao;
public class NotifyDaoByCellPhoneImpl implements NotifyDao {
public void sendMessage(String message) {
System.out.println("发送手机短信: "+message);
}
}
NotifyDaoByWeixinImpl.java
package Spring.Dao.Impl;
import Spring.Dao.NotifyDao;
public class NotifyDaoByWeixinImpl implements NotifyDao {
public void sendMessage( String message) {
System.out.println("发送微信短信: "+message) ;
}
}
OrderNotify.java
package Spring.Service;
import Spring.Dao.NotifyDao;
import Spring.Dao.Order;
import lombok.*;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderNotify {
private NotifyDao notify;
private Order order;
public void PaySuccess(){
notify.sendMessage(order.toString());
}
}
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="order1" class="Spring.Dao.Order">
<!-- collaborators and configuration for this bean go here -->
<property name="id" value="20210504557" />
<property name="clientName" value="赵尚凌" />
<property name="money" value="97.5" />
</bean>
<bean id="order2" class="Spring.Dao.Order">
<constructor-arg index="0" value="20210504558" />
<constructor-arg index="1" value="刘瑞华" />
<constructor-arg index="2" value="157.5" />
</bean>
<bean id="cellPhone" class="Spring.Dao.Impl.NotifyDaoByCellPhoneImpl">
</bean>
<bean id="weixin" class="Spring.Dao.Impl.NotifyDaoByWeixinImpl">
</bean>
<bean id="orderNotify1" class="Spring.Service.OrderNotify">
<property name="order" ref="order1" />
<property name="notify" ref="cellPhone" />
</bean>
<bean id="orderNotify2" class="Spring.Service.OrderNotify">
<constructor-arg ref="order2" />
<constructor-arg ref="weixin" />
</bean>
</beans>
TestSpring.java
package Spring.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import Spring.Service.OrderNotify;
public class TestSpring {
public static void main(String[]args) {
ApplicationContext act= new ClassPathXmlApplicationContext("applicationContext.xml");
OrderNotify orderNotify1 =act.getBean("orderNotify1",OrderNotify.class);
orderNotify1.PaySuccess();
OrderNotify orderNotify2 =act.getBean("orderNotify2",OrderNotify.class);
orderNotify2.PaySuccess();
System.out.println("以上输出结果由:XXXXXXXXXXXXXXXX完成");
}
}