Spring自动装配(基于注解)

11个月前 (04-27)
从 Java 5 开始,Java 增加了对注解(Annotation)的支持,它是代码中的一种特殊标记,可以在编译、类加载和运行时被读取,执行相应的处理。开发人员可以通过注解在不改变原有代码和逻辑的情况下,在源代码中嵌入补充信息。

Spring 从 2.5 版本开始提供了对注解技术的全面支持,我们可以使用注解来实现自动装配,简化 Spring 的 XML 配置。

Spring 通过注解实现自动装配的步骤如下:

  1. 引入依赖

  2. 开启组件扫描

  3. 使用注解定义 Bean

  4. 依赖注入

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- x.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网站站点" rel="nofollow" />

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网站站点" rel="nofollow" />

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语言中文网

一个精美而实用的网站

专注于分享优质编程教程。