在 classpath 中扫描组件:

组件扫描(component scanning): Spring 能够从 classpath 下自动扫描 , 侦测和实例化具有特定注解的组件。

特定组件包括:

  • @Component:基本注解 , 标识了一个受 Spring 管理的组件。
  • @Repository:标识持久层组件。
  • @Service:标识服务层(业务层)组件。
  • @Controller:标识表现层组件。

对于扫描到的组件 , Spring 有默认的命名策略:使用非限定类名 , 第一个字母小写。也可以通过 value 属性值标识组件的名称。

 <?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"> <!-- 指定 Spring IOC 容器扫描的包-->
<context:component-scan base-package="com.itdjx.spring.annotation"> </context:component-scan> </beans>

注意:一定要添加 context 命名空间。

 package com.itdjx.spring.annotation;

 import org.springframework.stereotype.Component;

 /**
* @author Wáng Chéng Dá
* @create 2017-03-03 8:44
*/
@Component
public class TestObject {
}
 package com.itdjx.spring.annotation.controller;

 import org.springframework.stereotype.Controller;

 /**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:28
*/
@Controller
public class UserController { public void execute() {
System.out.println("I am UserController's execute method...");
}
}
 package com.itdjx.spring.annotation.service;

 import org.springframework.stereotype.Service;

 /**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:26
*/
@Service
public class UserService { public void add() {
System.out.println("I am UserService's add method...");
}
}
 package com.itdjx.spring.annotation.repository;

 import org.springframework.stereotype.Repository;

 /**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:24
*/
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository { @Override
public void sava() {
System.out.println("I am UserRepositoryImpl's sava method...");
}
}
 package com.itdjx.spring.annotation.repository;

 /**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:23
*/
public interface UserRepository { void sava(); }
 package com.itdjx.spring.annotation;

 import com.itdjx.spring.annotation.controller.UserController;
import com.itdjx.spring.annotation.repository.UserRepository;
import com.itdjx.spring.annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:34
*/
public class Main { public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans-annotation.xml");
TestObject testObject = (TestObject) cxt.getBean("testObject");
System.out.println(testObject); UserController userController = (UserController) cxt.getBean("userController");
System.out.println(userController); UserService userService = (UserService) cxt.getBean("userService");
System.out.println(userService); UserRepository userRepository = (UserRepository) cxt.getBean("userRepository");
System.out.println(userRepository); }
}

控制台输出:

com.itdjx.spring.annotation.TestObject@27ba32
com.itdjx.spring.annotation.controller.UserController@f82753
com.itdjx.spring.annotation.service.UserService@10fe47a
com.itdjx.spring.annotation.repository.UserRepositoryImpl@2b0582

注意:Spring 默认的命名策略:使用非限定类名 , 第一个字母小写。也可以通过 value 属性值标识组件的名称。

若不修改 @Repository("userRepository") 默认的 value 值 , bean 的 id 值是 userRepositoryImpl , UserRepository userRepository = (UserRepository) cxt.getBean("userRepository"); 会抛出找不到 userRepository 这个 bean 节点。

异常:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userRepository' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
at com.itdjx.spring.annotation.Main.main(Main.java:26)

Spring学习--通过注解配置 Bean (一)的更多相关文章

  1. Spring学习--通过注解配置 Bean (三)

    组件装配: <context:component-sacan> 元素还会自动注册 AutowiredAnnotationBeanPostProcesser 实例 , 该实例可以自动装配具有 ...

  2. Spring学习--通过注解配置 Bean (二)

    在 classpath 中扫描组件: 当在组件类上使用了特定的注解之后 , 还需要在 Spring 的配置文件中声明 <context:component-scan>: base-pack ...

  3. Spring学习之xml配置Bean总结

    学习Spring时,我用的是Maven来管理jar包,先看看maven的pom.xml: pom.xml <project xmlns="http://maven.apache.org ...

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

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

  5. Spring框架学习之注解配置与AOP思想

         上篇我们介绍了Spring中有关高级依赖关系配置的内容,也可以调用任意方法的返回值作为属性注入的值,它解决了Spring配置文件的动态性不足的缺点.而本篇,我们将介绍Spring的又一大核心 ...

  6. Spring学习(七)bean装配详解之 【通过注解装配 Bean】【自动装配的歧义解决】

    自动装配 1.歧义性 我们知道用@Autowired可以对bean进行注入(按照type注入),但如果有两个相同类型的bean在IOC容器中注册了,要怎么去区分对哪一个Bean进行注入呢? 如下情况, ...

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

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

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

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

  9. 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 ...

随机推荐

  1. AHOI2018 (暨HNOI2018)编外滚粗记

    Day0: 向老师打了声报告就偷偷摸摸溜出了学校……感谢门卫师傅没把我当贼抓起来 车上背了一遍FFT,SAM的板子.嘴巴ac了两道CC水题.离线刷了一波知乎. 酒店好评. Day1: 不知不觉就开考了 ...

  2. P1823 音乐会的等待(单调栈)

    P1823 音乐会的等待 题目描述 N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人.队列中任意两个人A和B,如果他们是相邻或他们之间没有人比A或B高,那么 ...

  3. 剁了xp,醉了win7

    装完win7,安装各种软件完毕,重启,然并卵.  cpu,内存飙升!! svchost.exe这个进程内存发疯了一样往上飙升 从 几十兆  到占用1个多G, 纳尼, 总共物理内存才2G. ╮(╯▽╰) ...

  4. 第十七篇 Python函数之闭包与装饰器

    一. 装饰器 装饰器:可以拆解来看,器本质就是函数,装饰就是修饰的意思,所以装饰器的功能就是为其他函数添加附加功能. 装饰器的两个原则: 1. 不修改被修饰函数的源代码 2. 不修改被修饰函数的调用方 ...

  5. 输出1-n的全排(递归C++)

    [问题描述] 输出1到n之间所有不重复的排列,即1到n的全排,要求所产生的任一数列不含有重复的数字. [代码展示] #include<iostream>using namespace st ...

  6. Module安装

    利用pip3 install numpy,代表安装了数学模块 pip3 install -U numpy,代表升级数学模块 有的时候需要升级pip本身,如上所示,直接执行pip3 install -U ...

  7. AtomicIntegerFieldUpdater使用

    假设现在有这样的一个场景: 一百个线程同时对一个int对象进行修改,要求只能有一个线程可以修改. 看看下面程序是否正确: private static int a = 100; private sta ...

  8. 软工实践 - 第二十九次作业 Beta 冲刺(7/7)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10159251.html 作业博客:[班级博客本次作业的链接] (https://edu.cnb ...

  9. js定时器实现图片轮播

    效果展示如下: setInterval(moverleft,3000);定时器设置为3秒,而且实现图片下方的小圆点序号跟图片对应,点击小圆点也能切换图片. 代码如下: <!DOCTYPE htm ...

  10. ActiveMQ使用详解---相关概念

    一.前言 公司之前使用activeMQ做过一款用于系统之间传递信息的工具,最近才正式投入生产使用,这期间出现了一些比较奇怪的问题,最终发现是没有清晰的了解activeMQ的相关配置以及一些相关概念,借 ...