本文共 5646 字,大约阅读时间需要 18 分钟。
掌握Spring IoC(控制反转)的核心概念,熟悉常用注解的使用方法,并通过实例理解基于注解的依赖注入机制。同时,了解Java配置在Spring框架中的应用场景。
1. Spring IoC的基本概念
2. Spring的常用注解
3. 基于注解的依赖注入
4. Java配置
1. Spring IoC的基本概念
IoC(Inversion of Control)的核心思想是反转资源获取的控制权。传统开发模式下,组件需要主动寻找资源(如服务、资源等),而Spring IoC通过容器实现资源的主动注入,减少组件之间的耦合度。
Spring框架通过IoC容器(如ApplicationContext)管理对象的创建与注入。具体来说,Spring容器负责创建和注入所需的Bean实例,组件无需手动操作,只需通过注解或配置文件描述需求即可。
通过IoC,Spring实现了资源的“就近原则”和“组合原则”,提升了应用的可维护性和扩展性。
2. Spring的常用注解
Spring提供了多种注解,用于标注Bean以及实现依赖注入。常用的注解包括:
2.1 @Component
@Component是一个通用的注解,用于标注任何层次的组件Bean。它本身没有特定的作用,但可以结合其他注解(如@Repository、@Service、@Controller)使用。
2.2 @Repository
@Repository用于标注数据访问层(DAO)的Bean。它主要用于将DAO类注册为Spring管理Bean,类似于传统的DAO层实现。
2.3 @Service
@Service用于标注业务逻辑层(Service)的Bean。它定义了一个服务组件,通常包含对DAO、资源等的调用和业务逻辑的处理。
2.4 @Controller
@Controller用于标注Spring MVC的Controller组件。它定义了一个控制器Bean,负责处理Web请求并返回响应。
2.5 @Autowired
@Autowired是一个自动注入注解,可用于注入成员变量、方法参数或构造函数。它默认按Bean的类型进行注入,适用于简单的依赖注入场景。
2.6 @Resource
@Resource与@Autowired功能相似,但默认按Bean的名称进行注入。可以结合@Qualifier注解进一步指定注入的Bean名称。
2.7 @Qualifier
@Qualifier用于指定特定的Bean进行注入,通常与@Autowired配合使用。当需要按名称注入时,@Qualifier注解会指定具体的Bean名称。
3. 基于注解的依赖注入
Spring通过注解实现依赖注入,简化了Bean的管理和配置。开发者只需在目标类上添加注解,Spring容器会自动识别并完成注入工作。
基于注解的依赖注入具有以下优势:
• 简化了Bean的配置,只需添加注解即可。
• 提高了代码的可读性和可维护性。
• 减少了手动编写 setter 或 getter 方法的工作量。
基于注解的依赖注入常见场景包括:
• 组件之间的依赖关系注入。
• 资源(如数据库连接、文件资源)等的注入。
• 服务层与数据访问层的耦合度优化。
4. Java配置
Spring 4.x 推荐使用Java配置(基于@Configuration和@Bean注解)进行Bean的定义和管理。这种方式避免了传统的XML配置文件,代码更加简洁且易于维护。
Java配置的核心思路是通过注解定义Bean的创建逻辑。开发者只需编写配置类,注解方法返回Bean实例,Spring容器会自动注册这些Bean。
通过Java配置,可以实现以下功能:
• 定义自定义Bean。
• 注入环境参数(如数据库配置等)。
• 配置Spring的各个子框架(如MVC、数据访问等)。
【例1-2】基于注解的依赖注入
1. 创建DAO层:
在`ch1_2`应用的`src`中创建`annotation.dao`包,添加以下代码:
```javapackage annotation.dao;
import org.springframework.stereotype.Repository;
@Repository("testDao")public class TestDaoImpl implements TestDao {public void save() {System.out.println("testDao save");}}
2. 创建Service层:
在`ch1_2`应用的`src`中创建`annotation.service`包,添加以下代码:
```javapackage annotation.service;import javax.annotation.Resource;import org.springframework.stereotype.Service;import annotation.dao.TestDao;@Service("testService")public class TestServiceImpl implements TestService { @Resource(name = "testDao") private TestDao testDao; @Override public void save() { testDao.save(); System.out.println("testService save"); }}
3. 创建Controller层:
在`ch1_2`应用的`src`中创建`annotation.controller`包,添加以下代码:
```javapackage annotation.controller;
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import annotation.service.TestService;
@Controllerpublic class TestController {@Autowiredprivate TestService testService;
public void save() { testService.save(); System.out.println("testController save");} }
4. 创建Java配置类:
在`ch1_2`应用的`src`中创建`annotation`包,添加以下代码:
```javapackage annotation;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScan("annotation")public class ConfigAnnotation {}
5. 运行测试类:
在`ch1_2`应用的`src`中创建`annotation`包,添加以下代码:
```javapackage annotation;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;import annotation.controller.TestController;
public class TestAnnotation {public static void main(String[] args) {AnnotationConfigApplicationContext appCon = new AnnotationConfigApplicationContext(ConfigAnnotation.class);TestController tc = appCon.getBean(TestController.class);tc.save();appCon.close();}}
运行上述测试类,可观察到控制台输出“testController save”,表明依赖注入成功。
【例1-3】Java配置实例
1. 创建DAO类:
在`ch1_3`应用的`src`中创建`dao`包,添加以下代码:
```javapackage dao;public class TestDao { public void save() { System.out.println("TestDao save"); }}
2. 创建Service类:
在`ch1_3`应用的`src`中创建`service`包,添加以下代码:
```javapackage service;
import dao.TestDao;
public class TestService {private TestDao testDao;
public void setTestDao(TestDao testDao) { this.testDao = testDao;}public void save() { testDao.save(); System.out.println("TestService save");} }
3. 创建Controller类:
在`ch1_3`应用的`src`中创建`controller`包,添加以下代码:
```javapackage controller;import service.TestService;public class TestController { private TestService testService; public void setTestService(TestService testService) { this.testService = testService; } public void save() { testService.save(); System.out.println("TestController save"); }}
4. 创建Java配置类:
在`ch1_3`应用的`src`中创建`javaConfig`包,添加以下代码:
```javapackage javaConfig;
import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Bean;
@Configurationpublic class JavaConfig {@Beanpublic TestDao getTestDao() {return new TestDao();}
@Beanpublic TestService getTestService() { TestService ts = new TestService(); ts.setTestDao(getTestDao()); return ts;}@Beanpublic TestController getTestController() { TestController tc = new TestController(); tc.setTestService(getTestService()); return tc;} }
5. 运行测试类:
在`ch1_3`应用的`src`中创建`javaConfig`包,添加以下代码:
```javapackage javaConfig;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import controller.TestController;public class TestConfig { public static void main(String[] args) { AnnotationConfigApplicationContext appCon = new AnnotationConfigApplicationContext(JavaConfig.class); TestController tc = appCon.getBean(TestController.class); tc.save(); appCon.close(); }}
运行上述测试类,可观察到控制台输出“TestController save”,表明Java配置成功。
建议花费5-6小时完成本章内容的学习和实践。
1. CSDN 技术博客累计3篇
转载地址:http://sjrp.baihongyu.com/