Spring4---通过注解配置Bean

版权声明:--------------------------新手上路,如有不正确的地方,还请多多指出不足之处--------------------------- https://blog.csdn.net/qq_43386754/article/details/88083561

什么是注解?

  • 在jdk1.5的时候就引入了注解,最为常见的就是复写方法的时候在方法上写上"@Override",以及在使用JUNIT4做测试的时候,常常会使用到"@Test","@Before"m"@After"等等符号,这样符号就称之为注解,
  • 但注解不仅仅是一个符号而已,每个注解都有一个特定的功能,例如在编写Servlet程序类的时候,使用"@WebServlet"注解就可以轻松指定该Servlet的访问路径,而不需要到web.xml文件中再进行配置.
  • 实际上每个注解其实就是一个接口,java通过反射来访问相关API的信息,被注解的类,根据这些信息来进行某些特定的行为.

为什么使用注解?

  • 在使用spring框架的时候,往往需要为大量的Bean配置xml文件,随着时间的推移,这些xml文件会越来越庞大,当时候会很难维护.
  • 而通过注解的方式,就可以通过简单的实现对Bean的配置,使代码更加的简洁.

使用注解的缺点

  • 注解也不是万能的,虽然使用注解配置Bean会更加的方便,但也会有以下几个问题
    1. 使用注解的方式没有直接使用xml文件的方式执行效率更快,这是因为,使用注解的时候,还需要IOC容器对指定的包进行扫描,而xml文件不需要直接通过反射全类名的方式实例化.
    2. 过于分布的注解,使用不当,还可能导致配置混.

Spring中使用注解配置Bean

  • 以下的四个注解可以混用,这是因为Spring还无法分辨注解的Bean是属于DAO层还是服务层
  • 图片转载自尚硅谷的教学ppt
    在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

示例:Spring注解使用

  • 模拟一个分层程序设计,DAO层,服务层,业务层,控制层
  1. 定义Employe.java程序类,描述雇员表中的信息传输类
@Repository
public class Employee {
    private Integer id;
    private String name;
    private Integer age;
    private String job;
//getter和setter方法

}
  1. 编写 EmployeeDAO类,模拟一个持续化操作类
@Component
public class EmployeeDAO {
    public boolean doCreate(Employee employee){
        return true;
    }
}
  1. 编写EmployeeService程序类,描述服务层
@Service
public class EmployeeService {
    public boolean add(Employee vo){
        EmployeeDAO employeeDAO = new EmployeeDAO();
        return employeeDAO.doCreate(vo);
    }
}

  1. 编写一个EmpAction程序类,描述控制层
@Controller
public class EmpAction {
    private Employee vo = new Employee();

    public Employee getVo() {
        return vo;
    }

    public void setVo(Employee vo) {
        this.vo = vo;
    }


    public String add(){
        EmployeeService employeeService = new EmployeeService();
        if(employeeService.add(this.vo)){
            return "add_list.jsp";
        }else{
            return "error.page";
        }
    }
}

  • 此时每一个注解都是用默认的名称,也就是说在IOC容器中,Bean的id都是类名称首个字母小写,例如:"“empAction”

  • 配置annotation_employee.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--配置制定扫描的基类包-->
    <context:component-scan base-package="mao.shu.spring.annotation"></context:component-scan>
</beans>
  • 测试类
package mao.shu.spring.annotation;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.*;

public class EmpActionTest {
    private ApplicationContext app;
    @Before
    public void before(){
        this.app = new ClassPathXmlApplicationContext("mao/shu/spring/annotation/annotation.xml");

    }
    @Test
    public void test(){
        //获取持续化类Bean
        Employee employee = this.app.getBean("employee",Employee.class);

        //获取DAO操作类实例Bean
        EmployeeDAO employeeDAO = this.app.getBean("employeeDAO",EmployeeDAO.class);
        //获取服务层操作类Bean
        EmployeeService employeeService = this.app.getBean("employeeService",EmployeeService.class);

        //获取控制层Bean
        EmpAction empAction = this.app.getBean("empAction",EmpAction.class);


        System.out.println(employee.getClass());
        System.out.println(employeeDAO.getClass());
        System.out.println(employeeService.getClass());
        System.out.println(empAction.getClass());

    }

}
  • 输出结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43386754/article/details/88083561