一、目的
• 了解 Spring 环境配置。
• 掌握定义应用程序类。
• 掌握编辑配置文件创建 bean 实例实现依赖注入。
• 掌握编辑测试类创建 IoC 容器,通过容器获取 bean 实例。
二、内容
创建一个Spring项目,完成主题为“订单信息通知”项目,每收到一笔订单后,系统给客户发送订单信息的通知。通知发送由两种方式:一种是手机短信;另一种是微信。
方法二:基于注解的 Bean 装配,项目运行结果如图所示:
三、实现步骤
1、利用上一种方法创建的项目, 复制粘贴并重命名,按照后续步骤完成。
2、打开 src\main\java\Spring\Dao 目录中的 Order.Java 文件并添加注解。
3、打开 src\main\java\Spring\Dao\Impl 目录中的 NotifyDaoByCellPhoneImpl.Java 文件并添加注解。
4、打开 src\main\java\Spring\Dao\Impl 目录中的 NotifyDaoByWeixinImpl.Java 文件并添加注解。
5、打开 src\main\java\Spring\Service 目录中的 OrderNotify.Java 文件并添加注解。
6、打开 src/main/rescources 目录中的 applicationContext.xml 文件并添加相关命名内容。
7、打开 src\main\java\Spring\Test 目录中的 TestSpring.Java 文件并进行修改。
8、运行该项目查看运行结果。
四、遇到的问题:在进行partB部分时,使用注解@Value报红
出现该问题的原因:导包错误(原因是lombok里面也有Value,两者名字一样,eclipse没有识别到,导致错误。)
解决办法:正确手动导包
五、部分关键代码:
Order.java
package Spring.Dao;
import org.springframework.stereotype.Repository;
import lombok.*;
import org.springframework.beans.factory.annotation.Value;
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Repository
public class Order {
@Value("20210504557")
String id;
@Value("赵尚凌")
String clientName;
@Value("97.5")
float money;
public String toString() {
return "客户: "+clientName+",完成订单:"+id+"付款,共人民币:"+money+"元";
}
}
NotifyDaoByCellPhoneImpl.java
package Spring.Dao.Impl;
import org.springframework.stereotype.Repository;
import Spring.Dao.NotifyDao;
@Repository("NotifyByCellPhone")
public class NotifyDaoByCellPhoneImpl implements NotifyDao {
public void sendMessage(String message) {
System.out.println("发送手机短信: "+message);
}
}
NotifyDaoByWeixinImpl.java
package Spring.Dao.Impl;
import org.springframework.stereotype.Repository;
import Spring.Dao.NotifyDao;
@Repository("NotifyByWeixin")
public class NotifyDaoByWeixinImpl implements NotifyDao {
public void sendMessage( String message) {
System.out.println("发送微信短信: "+message) ;
}
}
OrderNotify.java
package Spring.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import Spring.Dao.NotifyDao;
import Spring.Dao.Order;
import lombok.*;
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Service("OrderNotify")
public class OrderNotify {
@Autowired
@Qualifier("NotifyByWeixin")
private NotifyDao notify;
@Autowired
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="赵尚凌" />
<property name="clientName" value="20210504557" />
<property name="money" value="97.5" />
</bean>
<bean id="order2" class="Spring.Dao.Order">
<constructor-arg index="0" value="刘瑞华" />
<constructor-arg index="1" value="20210504558" />
<constructor-arg index="2" value="157.5" />
</bean>
<!--3.声明"Spring.Dao.Impl.NotifyDaoByCellPhoneImpl”类的bean(id="cellPhone") -->
<bean id="cellPhone" class="Spring.Dao.Impl.NotifyDaoByCellPhoneImpl">
</bean>
<!--4.声明"Spring.Dao.Impl.NotifyDaoByweixinImpl”类的bean(id="weixin") -->
<bean id="weixin" class="Spring.Dao.Impl.NotifyDaoByWeixinImpl">
</bean>
<!--5.仿照上面设值注入格式使用设值注入声明Spring.Service.OrderNotify”类的
bean(id="orderNotify1") ,分别使用前面声明的bean: order1和cellPhone为其注入值-->
<bean id="orderNotify1" class="Spring.Service.OrderNotify">
<property name="order" ref="order1" />
<property name="notify" ref="cellPhone" />
</bean>
<!--6.仿照上面构造注入格式使用构造注入再声明"Spring.Service.0rderNotify”类的
bean(id="orderNotify2") ,分别使用前面声明的bean: order2和weixin为其注入值-->
<bean id="orderNotify2" class="Spring.Service.OrderNotify">
<constructor-arg ref="order2" />
<constructor-arg ref="weixin" />
</bean>
</beans>
六、心得
通过本次学习,巩固了创建Maven Java Web 项目,使用Maven 项目构建工具更加熟练,了解了Spring 环境配置,Spring贯穿了表现层、业务层、持久层,为企业的应用开发提供了一个轻量级的解决方案,包括了基于依赖注入的核心机制,基于AOP的声明式事务管理,与多种持久层技术的整合,以及优秀的Web MVC框架等等,Spring采用反射的方式,在运行时动态的生成相应的Bean。