• 中文
    • English
  • 注册
  • 查看作者
    • Spring:基于注解的配置和组件扫描过滤器

      一.  添加依赖

      《Spring:简单使用》一文中,我们通过applicationContext.xml为其赋值,除了通过xml的,我们还可以使用注解的方式,首先添加以下依赖:

      • commons-logging-1.2.jar

      • druid-1.1.6.jar

      • mysql-connector-java-5.1.47.jar

      • spring-aop-5.1.8.RELEASE.jar

      • spring-beans-5.1.8.RELEASE.jar

      • spring-context-5.1.8.RELEASE.jar

      • spring-core-5.1.8.RELEASE.jar

      • spring-expression-5.1.8.RELEASE.jar

      • spring-jdbc-5.1.8.RELEASE.jar

      • spring-tx-5.1.8.RELEASE.jar

      二.  开启组件扫描

      我们只需要在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"
             xmlns:content="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 https://www.springframework.org/schema/context/spring-context.xsd">
          <content:component-scan base-package="io.zhangjia.spring" />
      </beans>

      注意,这里的package只给到了io.zhangjia.spring,而没有具体到entity

      三.  添加注解

      接下来只需要在需要配置到IOC容器的类添加注解即可,注解有以下几种:

      • @Component:标记普通组件

      • @Repository:标记数据层(相当于之前的Dao)

      • @Service:标记业务层

      • @Controller:标记控制层(相当于之前的Servlet)

      添加注解后,我们便可以使用@Value为属性赋值:

      @Component
      public class Book  {
          @Value("3")
          private Integer bookId;
          @Value("红高粱")
          private String name;
          @Value("莫言")
          private String author;
          @Value("12.3")
          private Double price;
          ...
      }

      四.  测试类

      在测试类中,我们可以根据id获取bean,因为我们为Book类添加@Component后,Spring就会默认为其取一个类名小写的Id,即book

      public class Test {
          public static void main(String[] args) {
      //        初始化Spring的IOC容器
              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
              Object book = context.getBean("book");
      
      //        Book bean = context.getBean(Book.class); //也可以采用这种方法获取
              System.out.println("book = " + book);
          }
      }
      输出:
      book = Book{bookId=3, name='红高粱', author='莫言', price=12.3}

      五.  组件扫描过滤器

      在上面的例子中,我们使用了@Component 注解,接下来我们使用一下其他的几个注解,首先创建对应的类:

      @Component:标记普通组件:Book类添加无参构造方法:

      package io.zhangjia.spring.entity;
      import org.springframework.stereotype.Component;
      @Component
      public class Book  {
          public Book() {
              System.out.println("Book.Book");
          }
      ...
      }

      @Repository:标记数据层:

      package io.zhangjia.spring.dao;
      import org.springframework.stereotype.Repository;
      
      @Repository
      public class BookDao{
          public BookDao() {
              System.out.println("BookDao.BookDao");
          }
      }

      @Service:标记业务层

      package io.zhangjia.spring.service;
      import org.springframework.stereotype.Service;
      
      @Service
      public class BookService {
          public BookService() {
              System.out.println("BookService.BookService");
          }
      }

      @Controller:标记控制层(相当于之前的Servlet)

      package io.zhangjia.spring.controller;
      import org.springframework.stereotype.Controller;
      
      @Controller
      public class BookController {
          public BookController() {
              System.out.println("BookController.BookController");
          }
      }

      接下来我们在测试类中只初始化Spring的IOC容器,看一下控制台的输出:

      public class Test {
          public static void main(String[] args) {
      //        初始化Spring的IOC容器
              ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
          }
      }
      
      输出:
      BookController.BookController
      BookDao.BookDao
      Book.Book
      BookService.BookService

      可以看到,Spring的IOC容器在初始化的时候,其四个注解组件都会被执行。

      在后面的学习中,我们会将Spring整合SpringMVC,届时Spring的IOC容器将会关注与管理除控制层之外的组件(即@Component,@Repository,@Service),而SpringMVC的IOC容器将会专注于只管理控制层组件(即@Controller)。

      所以我们可以通过下面的配置不扫描Controller组件

      <?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:content="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 https://www.springframework.org/schema/context/spring-context.xsd">
          <content:component-scan base-package="io.zhangjia.spring" >
              <content:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
          </content:component-scan>
       
      </beans>

      此时的测试类输出:

      BookDao.BookDao
      Book.Book
      BookService.BookService

      同理,如果我们模拟SpringMVC,设置只扫描Controller组件,则需要先关闭默认的过滤器,即use-default-filters=”false”

      <?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:content="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 https://www.springframework.org/schema/context/spring-context.xsd">
          
          <content:component-scan base-package="io.zhangjia.spring" use-default-filters="false">
              <content:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
          </content:component-scan>
      
      </beans>

      此时的测试类输出:

      BookController.BookController


    • 0
    • 0
    • 0
    • 2.5k
    • 请登录之后再进行评论

      登录

      赞助本站

      • 支付宝
      • 微信
      • QQ

      感谢一直支持本站的所有人!

      单栏布局 侧栏位置: