之前说的三种配置方式,都是使用XML配置,现在我们说说使用注解配置Bean。

这部分内容主要分为两个部分:使用注解配置Bean,使用注解配置Bean属性。

在classpath中扫描组件

组件扫描:Spring能够从ClassPath下自动扫描,侦测和实例化具有特定注释的组件

特定组件包括:

@Component

@Respository 持久层

@Service 业务层

@Controller 控制层

这四个标签可以混用,暂时没有什么区别,不过建议在不同层用不同的注释,方便阅读。

创建一个新的包com.figsprite.spring.beans_annotation,新的类com.figsprite.spring.beans_annotation.TestObject:

在TestObject.java里我们先放上注释,

现在Bean还没有被IOC容器管理,我们再来建一个子包com.figsprite.spring.beans_annotation.repository,写一个接口com.figsprite.spring.beans_annotation.repository.UserRepository和实现类com.figsprite.spring.beans_annotation.repository.UserRepositoryImpl,接下来写业务层和控制层,分别创建一个包,然后写上相应代码,并写上注释:

  

UserController.java:

package com.figsprite.spring.beans_annotation.controller;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {
public void execute(){
System.out.println("Controller execute");
}
}

  UserService.java:

package com.figsprite.spring.beans_annotation.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
public void add(){
System.out.println("Service Add");
}
}

  UserRepository.java

package com.figsprite.spring.beans_annotation.repository;

public interface UserRepository {

    void save();
}

  UserRepositoryImpl.java

package com.figsprite.spring.beans_annotation.repository;

import org.springframework.stereotype.Repository;

@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository{ @Override
public void save() {
System.out.println("SAVE");
}
}

做完这些,我们可以创建一个配置文件beans-annotation.xml,然后我们在这个配置文件里,通过<context:component-scan>标签扫描具有这些注释的类的包:

<context:component-scan base-package="com.figsprite.spring.beans_annotation"/>

接下来做一个测试,创建Main.java

在我们没有指明Bean对象的id时,在注释配置下,默认id为类名的驼峰命名。

如何命名呢?只要在我们刚刚写的标签里加个括号指定就行:

@Repository("userRepository")

Main.java:

  1. package com.figsprite.spring.beans_annotation;  
  2.     
  3. import com.figsprite.spring.beans_annotation.controller.UserController;  
  4. import com.figsprite.spring.beans_annotation.repository.UserRepositoryImpl;  
  5. import com.figsprite.spring.beans_annotation.service.UserService;  
  6. import org.springframework.context.ApplicationContext;  
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  8.     
  9. public class Main {  
  10.     public static void main(String[] args) {  
  11.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");  
  12.         TestObject to = (TestObject) ctx.getBean("testObject")  ;  
  13.         System.out.println(to);  
  14.     
  15.         UserController userController = (UserController) ctx.getBean("userController");  
  16.         System.out.println(userController);  
  17.     
  18.     
  19.         UserRepositoryImpl userRepository = (UserRepositoryImpl) ctx.getBean("userRepository");  
  20.         System.out.println(userRepository);  
  21.     
  22.         UserService userService = (UserService) ctx.getBean("userService");  
  23.         System.out.println(userService);  
  24.     }  
  25. }

接下来回到<context:compomenr-scan>标签,再来说说里面的其他属性resource-pattern

,这个属性可以过滤特定类,比如我只想扫描repository底下的类,

<context:component-scan
base-package="com.figsprite.spring.beans_annotation"
resource-pattern="repository/*.class"
/>

这样在Main里面,我们只会得到UserRepository的对象,其他的就得不到了,肯定报错,

<context:compomenr-scan>标签还有两个子标签

<context:include-filtter>包含哪些指定表达式的组件

< context:exclude-filtter >排除哪些指定表达式的组件

type属性:这里就介绍两种常用的annotation根据注解锁定目标,

比如不包含Repository注解

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>

对于<context:include-filtter>,由于<context:compomenr-scan>默认是扫描包下的所有指定表达式组件,所有我们要加上use-default-filters="false",这样就不会自动扫描了,<context:include-filtter>与< context:exclude-filtter >使用方法一致,

另外一种是使用assignable方式,根据类名锁定目标。这里就再演示了

通过注解配置Bean的更多相关文章

  1. [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等

    实验1:配置通过静态工厂方法创建的bean  [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory { private st ...

  3. Spring(十五):通过注解配置 Bean

    在ClassPath中扫描组件 1)组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件: 2)特定组件包含: --- @C ...

  4. IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置

    1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...

  5. Spring IOC机制之使用注解配置bean

    一. 通过注解配置bean 1.1       概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2       ...

  6. 14Spring通过注解配置Bean(2)

    下面将对13Spring通过注解配置Bean(1)的中Repository.Service.Controller通过注解方式来建立关联. <context:component-scan>元 ...

  7. 13Spring通过注解配置Bean(1)

    配置Bean的形式:基于XML文件的方式:基于注解的方式(基于注解配置Bean:基于注解来装配Bean的属性) 下面介绍基于注解的方式来配置Bean. ——组件扫描(component scannin ...

  8. Spring框架入门之基于Java注解配置bean

    Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...

  9. Spring 注解配置Bean

    一.使用注解配置Bean 1.注解 在类定义.方法定义.成员变量定义前使用.其简化<bean>标签,功能同<bean>标签.格式为: @注解标记名. 2.组件扫描 Spring ...

随机推荐

  1. Mybatis框架基础支持层——反射工具箱之对象工厂ObjectFactory&DefaultObjectFactory(5)

    ObjectFactory官方简介:MyBatis每次创建结果集对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成. 默认的对象工厂需要做的仅仅是实例化目标类,要么通过默认 ...

  2. 转:从输入url到显示网页发生了什么

    在浏览器中输入url到显示网页主要包含两个部分: 网络通信和页面渲染 互联网内各网络设备间的通信都遵循TCP/IP协议,利用TCP/IP协议族进行网络通信时,会通过分层顺序与对方进行通信.分层由高到低 ...

  3. WebGIS中以version方式实现代码更新后前端自动读取更新代码的方法

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1. 前言 GIS代码进行更新后,由于用户前端已有缓存,导致更新的功能不 ...

  4. Hibernate从入门到了解

    目录 Hibernate的介绍与执行流程 运行流程: Hibernate运行环境搭建 Hibernate的基础示例 持久类的编写 持久类的介绍 几个考虑遵守的规则: 补充: Hibernate核心文件 ...

  5. IE8环境下的上传图片预览

    今天做一个需要在IE浏览器上使用的信息录入项目,遇到了图片上传预览的问题,找了一些资料,最后使用了IE自带的滤镜做到了 <!--HTML IE8不支持opacity,只能使用双层,一层背景半透明 ...

  6. SQL语句中不同的连接JOIN

    为了从两个表中获取数据,我们有时会用JOIN将两个表连接起来.通常有以下几种连接方式: JOIN  or  INNER JOIN(内连接) : 这两个是相同的,要求两边表同时有对应的数据,返回行,任何 ...

  7. [RHEL 6]GPT分区--parted

    对于2T以上的硬盘,划分分区表需要GPT分区,RHEL 6中使用parted进行分区 用法:parted [选项]... [设备 [命令 [参数]...]...] 将带有“参数”的命令应用于“设备”. ...

  8. Javascrip 入门第三节课

    一.location对象 location.href 获取当前网页的URLlocation.search() 获取?之后的请求信息 location.href="URL" // 跳 ...

  9. flink Standalone Cluster

    Requirements Software Requirements Flink runs on all UNIX-like environments, e.g. Linux, Mac OS X, a ...

  10. Storm入门(三)HelloWorld示例

    一.配置开发环境 storm有两种操作模式: 本地模式和远程模式.使用本地模式的时候,你可以在你的本地机器上开发测试你的topology, 一切都在你的本地机器上模拟出来; 用远程模式的时候你提交的t ...