Spring 的基本用法

该文详细介绍了如何创建一个Spring项目,实现订单信息通知功能,通过XML进行Bean装配,包括创建订单类、通知接口及其实现类,以及使用IoC容器测试。项目涉及Maven工程搭建,依赖管理,以及Spring的核心特性依赖注入。
摘要由CSDN通过智能技术生成

目的

了解 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完成");

}

}

Spring Cache是Spring框架提供的一个缓存抽象层,可以方便地在应用中添加缓存功能。下面是Spring Cache的基本使用方法: 1. 添加依赖:在项目的pom.xml文件中添加Spring Cache的依赖项。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> ``` 2. 配置缓存管理器:在配置类中添加@EnableCaching注解,并配置缓存管理器。 ```java @Configuration @EnableCaching public class CacheConfig { @Bean public CacheManager cacheManager() { SimpleCacheManager cacheManager = new SimpleCacheManager(); cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("cacheName"))); return cacheManager; } } ``` 3. 在需要缓存的方法上添加缓存注解:使用@Cacheable注解来标记需要进行缓存的方法。 ```java @Service public class MyService { @Cacheable("cacheName") public String getData(String key) { // 这里是从数据库或其他外部资源获取数据的逻辑 return data; } } ``` 4. 测试缓存功能:在测试类中调用被缓存的方法,多次调用可以看到第一次调用时会执行方法体,后续调用则从缓存中获取结果。 ```java @SpringBootTest class CacheTest { @Autowired private MyService myService; @Test public void testCache() { String result1 = myService.getData("key"); String result2 = myService.getData("key"); // result1和result2的值应该相同,第二次调用从缓存中获取结果 } } ``` 以上是Spring Cache的基本使用方法,通过配置缓存管理器和使用@Cacheable注解,可以轻松地在Spring应用中添加缓存功能。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值