Spring自动装配(基于注解)
从 Java 5 开始,Java 增加了对注解(Annotation)的支持,它是代码中的一种特殊标记,可以在编译、类加载和运行时被读取,执行相应的处理。开发人员可以通过注解在不改变原有代码和逻辑的情况下,在源代码中嵌入补充信息。
Spring 从 2.5 版本开始提供了对注解技术的全面支持,我们可以使用注解来实现自动装配,简化 Spring 的 XML 配置。
Spring 通过注解实现自动装配的步骤如下:
2. 将以下 jar 包导入到项目中。
3. 在 net.biancheng.c.dao 包下,创建一个名为 UserDao 的接口,代码如下。
4. 在 net.biancheng.c.dao.impl 包下,创建 UserDao 的实现类 UserDaoImpl,代码如下。
5. 在 net.biancheng.c.service 包下,创建一个名为 UserService 的接口,代码如下。
6. 在 net.biancheng.c.service.impl 包下,创建 UserService 的实现类 UserServiceImpl,代码如下。
7. 在 net.biancheng.c.controller 包下,创建一个名为 UserController 的类,代码如下。
8. 在 src 目录下,创建 Spring 配置文件 Beans.xml,配置内容如下。
9. 在 net.biancheng.c 包下,创建一个名为 MainApp 的类,代码如下。
10. 执行 MainApp 中的 main() 方法,控制台输出如下。
Spring 从 2.5 版本开始提供了对注解技术的全面支持,我们可以使用注解来实现自动装配,简化 Spring 的 XML 配置。
Spring 通过注解实现自动装配的步骤如下:
- 引入依赖
- 开启组件扫描
- 使用注解定义 Bean
- 依赖注入
1. 引入依赖
使用注解的第一步,就是要在项目中引入以下 Jar 包。- org.springframework.core-5.3.13.jar
- org.springframework.beans-5.3.13.jar
- spring-context-5.3.13.jar
- spring-expression-5.3.13.jar
- commons.logging-1.2.jar
- spring-aop-5.3.13.jar
注意,除了 spring 的四个基础 jar 包和 commons-logging-xxx.jar 外,想要使用注解实现 Spring 自动装配,还需要引入Spring 提供的 spring-aop 的 Jar 包。
2. 开启组件扫描
Spring 默认不使用注解装配 Bean,因此我们需要在 Spring 的 XML 配置中,通过 <context:component-scan> 元素开启 Spring Beans的自动扫描功能。开启此功能后,Spring 会自动从扫描指定的包(base-package 属性设置)及其子包下的所有类,如果类上使用了 @Component 注解,就将该类装配到容器中。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <!--开启组件扫描功能-->
- <context:component-scan base-package="net.biancheng.c"></context:component-scan>
- </beans>
注意:在使用 <context:component-scan> 元素开启自动扫描功能前,首先需要在 XML 配置的一级标签 <beans> 中添加 context 相关的约束。
3. 使用注解定义 Bean
Spring 提供了以下多个注解,这些注解可以直接标注在 Java 类上,将它们定义成 Spring Bean。注解 | 说明 |
---|---|
@Component |
该注解用于描述 Spring 中的 Bean,它是一个泛化的概念,仅仅表示容器中的一个组件(Bean),并且可以作用在应用的任何层次,例如 Service 层、Dao 层等。 使用时只需将该注解标注在相应类上即可。 |
@Repository | 该注解用于将数据访问层(Dao 层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。 |
@Service | 该注解通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。 |
@Controller | 该注解通常作用在控制层(如 Struts2 的 Action、SpringMVC 的 Controller),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。 |
4. 基于注解方式实现依赖注入
我们可以通过以下注解将定义好 Bean 装配到其它的 Bean 中。注解 | 说明 |
---|---|
@Autowired |
可以应用到 Bean 的属性变量、setter 方法、非 setter 方法及构造函数等,默认按照 Bean 的类型进行装配。 @Autowired 注解默认按照 Bean 的类型进行装配,默认情况下它要求依赖对象必须存在,如果允许 null 值,可以设置它的 required 属性为 false。如果我们想使用按照名称(byName)来装配,可以结合 @Qualifier 注解一起使用 |
@Resource |
作用与 Autowired 相同,区别在于 @Autowired 默认按照 Bean 类型装配,而 @Resource 默认按照 Bean 的名称进行装配。 @Resource 中有两个重要属性:name 和 type。
|
@Qualifier | 与 @Autowired 注解配合使用,会将默认的按 Bean 类型装配修改为按 Bean 的实例名称装配,Bean 的实例名称由 @Qualifier 注解的参数指定。 |
示例
1. 创建一个名为 my-spring-autowire-demo 的 Java 工程。2. 将以下 jar 包导入到项目中。
- org.springframework.core-5.3.13.jar
- org.springframework.beans-5.3.13.jar
- spring-context-5.3.13.jar
- spring-expression-5.3.13.jar
- commons.logging-1.2.jar
- spring-aop-5.3.13.jar
3. 在 net.biancheng.c.dao 包下,创建一个名为 UserDao 的接口,代码如下。
- package net.biancheng.c.dao;
- public interface UserDao {
- public void print();
- }
4. 在 net.biancheng.c.dao.impl 包下,创建 UserDao 的实现类 UserDaoImpl,代码如下。
- package net.biancheng.c.dao.impl;
- import net.biancheng.c.dao.UserDao;
- import org.springframework.stereotype.Repository;
- @Repository("userDao")
- public class UserDaoImpl implements UserDao {
- @Override
- public void print() {
- System.out.println("C语言中文网");
- }
- }
5. 在 net.biancheng.c.service 包下,创建一个名为 UserService 的接口,代码如下。
- package net.biancheng.c.service;
- public interface UserService {
- public void out();
- }
6. 在 net.biancheng.c.service.impl 包下,创建 UserService 的实现类 UserServiceImpl,代码如下。
- package net.biancheng.c.service.impl;
- import net.biancheng.c.dao.UserDao;
- import net.biancheng.c.service.UserService;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- @Service("userService")
- public class UserServiceImpl implements UserService {
- @Resource
- private UserDao userDao;
- public UserDao getUserDao() {
- return userDao;
- }
- public void setUserDao(UserDao userDao) {
- this.userDao = userDao;
- }
- @Override
- public void out() {
- userDao.print();
- System.out.println("一个精美而实用的网站");
- }
- }
7. 在 net.biancheng.c.controller 包下,创建一个名为 UserController 的类,代码如下。
- package net.biancheng.c.controller;
- import net.biancheng.c.service.UserService;
- import org.springframework.stereotype.Controller;
- import javax.annotation.Resource;
- @Controller("userController")
- public class UserController {
- @Resource
- private UserService userService;
- public UserService getUserService() {
- return userService;
- }
- public void setUserService(UserService userService) {
- this.userService = userService;
- }
- public void doStr() {
- userService.out();
- System.out.println("专注于分享优质编程教程。");
- }
- }
8. 在 src 目录下,创建 Spring 配置文件 Beans.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"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <!--开启组件扫描功能-->
- <context:component-scan base-package="net.biancheng.c"></context:component-scan>
- </beans>
9. 在 net.biancheng.c 包下,创建一个名为 MainApp 的类,代码如下。
- package net.biancheng.c;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import net.biancheng.c.controller.UserController;
- public class MainApp {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
- UserController userController = context.getBean("userController", UserController.class);
- userController.doStr();
- }
- }
10. 执行 MainApp 中的 main() 方法,控制台输出如下。
C语言中文网 一个精美而实用的网站 专注于分享优质编程教程。