在 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. atoi 和 atof (把数字字符串转化为数字储存)

    int atoi(char *s) 如果字符串内容是整数就返回该整数,否则返回0 double atof(char *s) 同上,不过返回浮点型 #include<iostream> #i ...

  2. 【UE4】二十六、Look at camera 蓝图

    如图,把BP_Cube替换为你需要的对象(如3DUI等)即可.

  3. 什么是OSS/BSS(电信业务)

    电信业务运营支持系统(BOSS),面对客户是统一的:面对电信运营商,它融合了业务支撑系统(BSS)与运营支撑系统(OSS),是一个综合的业务运营和管理平台,同时也是真正融合了传统IP数据业务与移动增值 ...

  4. Andrid 打印调用堆栈

    public static void printCallStatck() { Throwable ex = new Throwable(); StackTraceElement[] stackElem ...

  5. .NET中调用不安全代码

           .NET中是不允许不安全的代码的,比如指针等.但有些特殊场合还是需要用到指针,这时候就需要在你的代码块上加上unsafe标签.如: 1: unsafe static void Main( ...

  6. 【Spring实战】----开篇(包含系列目录链接)

    [Spring实战]----开篇(包含系列目录链接) 置顶2016年11月10日 11:12:56 阅读数:3617 终于还是要对Spring进行解剖,接下来Spring实战篇系列会以应用了Sprin ...

  7. 13-Mysql数据库----权限设置

    权限管理 我们知道我们的最高权限管理者是root用户,它拥有着最高的权限操作.包括select.update.delete.update.grant等操作.那么一般情况在公司之后DBA工程师会创建一个 ...

  8. 九度OJ--Q1168

    import java.util.Scanner; public class q1168 { public static void main(String[] args) { Scanner scan ...

  9. ThinkPHP5作业管理系统中处理学生未交作业与已交作业信息

    在作业管理系统中,学生登陆到个人中心后可以通过左侧的菜单查看自己已经提交的作业和未提交作业.那么在系统中如何实现这些数据的查询的呢?首先我们需要弄清楚学生(Student).班级(class).作业提 ...

  10. 机器学习 (三) 逻辑回归 Logistic Regression

    文章内容均来自斯坦福大学的Andrew Ng教授讲解的Machine Learning课程,本文是针对该课程的个人学习笔记,如有疏漏,请以原课程所讲述内容为准.感谢博主Rachel Zhang 的个人 ...