Spring入门(7)-自动检测Bean

本文介绍如何自动检测Bean。

0. 目录

  1. 使用component-scan自动扫描
  2. 为自动检测标注Bean

1. 使用component-scan自动扫描

<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>
<context:annotation-config/>
<context:component-scan base-package="com.chzhao.springtest"/>
</beans>

注:<context:annotation-config/>也可以去掉

package com.chzhao.springtest;

public interface IPersonBll {
void show();
}
package com.chzhao.springtest;

import org.springframework.stereotype.Service;

@Service
public class PersonBll implements IPersonBll {
public void show() {
System.out.println("show message");
}
}
package com.chzhao.springtest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class App { @Autowired
private IPersonBll personBll; public IPersonBll getPersonBll() {
return personBll;
} public void setPersonBll(IPersonBll personBll) {
this.personBll = personBll;
} public void showMsg() {
this.personBll.show();
} }
package com.chzhao.springtest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
ApplicationContext act = new ClassPathXmlApplicationContext(
"applicationContext.xml"); App a = (App) act.getBean(App.class);
a.showMsg();
}
}

2. 为自动检测标注Bean

context:component-scan会自动检测如下注解:

  • @Component:通用的构造型注解,标识该类为Spring组件
  • @Controller:标识该类为Spring MVC controller
  • @Repository:标识该类为数据仓库
  • @Service:标识此类定义为服务

@Component可以标注任意自定义注解,同时也可以命名Bean的ID。

package com.chzhao.springtest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component("app1")
public class App { @Autowired
private IPersonBll personBll; public IPersonBll getPersonBll() {
return personBll;
} public void setPersonBll(IPersonBll personBll) {
this.personBll = personBll;
} public void showMsg() {
this.personBll.show();
} }
package com.chzhao.springtest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext act = new ClassPathXmlApplicationContext(
"applicationContext.xml"); App a = (App) act.getBean("app1");
a.showMsg();
}
}

Spring入门(7)-自动检测Bean的更多相关文章

  1. spring的自动装配Bean与自动检测Bean

    spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean. 1.自动装配与自动检测: 自动装配:让spring自动识别如何装配bean的依赖关系,减少对<pr ...

  2. Spring学习笔记--自动检测

    要使用自动检测,我们需要用到<context:annotation-scan>标签.<context:annotation-scan>元素除了完成与<context:an ...

  3. Spring学习笔记--自动装配Bean属性

    Spring提供了四种类型的自动装配策略: byName – 把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中. byType – 把与Bean的属性具有相同类型 ...

  4. 解决Spring+Quartz无法自动注入bean问题

    问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...

  5. Spring 注解Autowired自动注入bean异常解决

      错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined ...

  6. IDEA去除自动检测bean是否存在

    操作步骤如下图所示:

  7. spring实战五之Bean的自动检测

    在spring实战四中,使用在Spring中增加<context:annotation-config>的方式告诉Spring,我们打算使用基于注解的自动装配,希望Spring特殊对待我们所 ...

  8. 【译】Spring 4 自动装配、自动检测、组件扫描示例

    前言 译文链接:http://websystique.com/spring/spring-auto-detection-autowire-component-scanning-example-with ...

  9. Spring 入门知识点笔记整理

    一.Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Sprin ...

随机推荐

  1. BZOJ 3207 花神的嘲讽计划Ⅰ(函数式线段树)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3207 题意:给出一个数列,若干询问.每个询问查询[L,R]区间内是否存在某个长度为K的子 ...

  2. 使用 Oracle GoldenGate 在 Microsoft SQL Server 和 Oracle Database 之间复制事务

    使用 Oracle GoldenGate 在 Microsoft SQL Server 和 Oracle Database 之间复制事务 作者:Nikolay Manchev 分步构建一个跨这些平台的 ...

  3. TUXEDO错误解决方案

    错误1: root@tfjus:/opt/tuxedo/simpapp# buildclient -f simpcl.c -o simpcl simpcl.c: In function 'main': ...

  4. 我的MYSQL学习心得

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

  5. EF4.0和EF5.0增删改查的写法区别及执行Sql的方法

    EF4.0和EF5.0增删改查的写法区别 public T AddEntity(T entity) { //EF4.0的写法 添加实体 //db.CreateObjectSet<T>(). ...

  6. java分层架构概念

    转自:http://www.cnblogs.com/bdqnbenet/p/4924778.html service是业务层 DAO (Data Access Object) 数据访问 1.JAVA中 ...

  7. datatables 参数详解(转)

    //@translator codepiano //@blog codepiano //@email codepiano.li@gmail.com //尝试着翻译了一下,难免有错误的地方,欢迎发邮件告 ...

  8. shell 的判断与比较

    1  shell 的$! ,$?, $$,$@ $n        $1 the first parameter,$2 the second... $#        The number of co ...

  9. 零基础编程指南(By Turtle)

    [零.基础] 1.看文章:<程序猿搜索的技巧>(未完成) [一.入门] 学习语言:VB 安装:下载VB6即可 教程:<李天生vb从入门到精通>http://www.xin372 ...

  10. hdu 3938 Portal(并查集+离线+kruskal)2011 Multi-University Training Contest 10

    搜了题解才把题搞明白.明白之后发现其实题意很清晰,解题思路也很清晰,只是题目表述的很不清晰…… 大意如下—— 给你一个无向图,图中任意两点的距离是两点间所有路径上的某一条边,这条边需要满足两个条件:1 ...