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. css3 去掉点击高光(移动端)

    在我们用手机浏览网页时,当点击某个链接或者点击事件时 会出现一层蓝色半透明的高光显示, 但在开发webapp时,由于网页是被native load去的,在app里面点击的时候如果出现高光则显得很不和谐 ...

  2. BZOJ 2005 能量采集(容斥原理)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2005 题意:给定n和m,求 思路:本题主要是解决对于给定的t,有多少对(i,j)满足x= ...

  3. js中的this怎么理解

    本博客供自己学习备忘, js中的this感觉很混乱,目前还有不少地方搞得不是很清楚,看到一篇不错的文章,先摘下来 this是Javascript语言的一个关键字它代表函数运行时,自动生成的一个内部对象 ...

  4. OGG-00782 - OGG 11.2.1.0.2 FOR Windows x64 Microsoft SQL Server

    OS ENV:主机名:           WIN-NO42QRNPMFAOS 名称:          Microsoft Windows Server 2008 R2 Datacenter OS ...

  5. TCSRM 593 div2(1000)(dp)

    Problem Statement      The pony Rainbow Dash wants to choose her pet. There are N animals who want t ...

  6. tomcat发布web service教程

    这几天一直在准备找工作,自学了关于web service的一些基本的内容,也遇到了不少问题.现在就把我自己学到的知识和大家分享一下,由于是初学,所以有什么错误的地方请大家帮忙指正,感激不尽~~!! 1 ...

  7. Android SharedPreferences 权限设置

    说明: 由于目前打算采用两个app来完成一件事,采用SharedPreferences来做数据交换,于是突然想验证一下Java层的权限设置会不会就是设置Linux下文件的权限,验证的结果是这样的. T ...

  8. oracle 回收站管理

    oracle10g,在pl/sql中选中删除后会出现类似:BIN$nJ5JuP9cQmqPaArFei384g==$0的表. 1.查看回收站 select * from user_recyclebin ...

  9. oracle查询某个用户下的所有视图

    SYS>select view_name from dba_views where owner = 'NS_RAS3'; VIEW_NAME--------------------------- ...

  10. Iwpriv工作流程及常用命令使用

    iwpriv工具通过ioctl动态获取相应无线网卡驱动的private_args所有扩展参数 iwpriv是处理下面的wlan_private_args的所有扩展命令,iwpriv的实现上,是这样的, ...