Spring入门(7)-自动检测Bean
Spring入门(7)-自动检测Bean
本文介绍如何自动检测Bean。
0. 目录
- 使用component-scan自动扫描
- 为自动检测标注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的更多相关文章
- spring的自动装配Bean与自动检测Bean
spring可以通过编写XML来配置Bean,也可以通过使用spring的注解来装配Bean. 1.自动装配与自动检测: 自动装配:让spring自动识别如何装配bean的依赖关系,减少对<pr ...
- Spring学习笔记--自动检测
要使用自动检测,我们需要用到<context:annotation-scan>标签.<context:annotation-scan>元素除了完成与<context:an ...
- Spring学习笔记--自动装配Bean属性
Spring提供了四种类型的自动装配策略: byName – 把与Bean的属性具有相同名字(或者ID)的其他Bean自动装配到Bean的对应属性中. byType – 把与Bean的属性具有相同类型 ...
- 解决Spring+Quartz无法自动注入bean问题
问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...
- Spring 注解Autowired自动注入bean异常解决
错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined ...
- IDEA去除自动检测bean是否存在
操作步骤如下图所示:
- spring实战五之Bean的自动检测
在spring实战四中,使用在Spring中增加<context:annotation-config>的方式告诉Spring,我们打算使用基于注解的自动装配,希望Spring特殊对待我们所 ...
- 【译】Spring 4 自动装配、自动检测、组件扫描示例
前言 译文链接:http://websystique.com/spring/spring-auto-detection-autowire-component-scanning-example-with ...
- Spring 入门知识点笔记整理
一.Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Sprin ...
随机推荐
- 在XML里的XSD和DTD以及standalone的使用3----具体使用详解
本人亲自写的一个简单的测试例子 1.xsd定义 <?xml version="1.0" encoding="utf-8"?><xs:schem ...
- JAVA中获取工程路径的方法
在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProp ...
- 纯tarjan poj2186
tarjan,叙叙旧咯 #include<cstdio>#define maxn 50005int e[maxn],ne[maxn],be[maxn],all;int DFN[maxn], ...
- WebClient+Fiddler2完美搭配下载远程页面信息
WebClient可以下载远程页面信息,这个大家应该都知道,核心代码如下: WebClient web = new WebClient(); string url = String.Format(&q ...
- fancybox 点击 js脚本判断验证,fancybox的宽度高度设置
当我们在使用fancybox做弹出窗口的时候,可能在弹窗之前就需要判断一些验证条件,例如我这里有个案例,用户必须先得勾选一个 那么怎么做呢?我们用到fancybox的一个onStart方法就可以了 $ ...
- c++ 完成端口资料
文章地址: http://blog.csdn.net/piggyxp/article/details/6922277 附件如下: word文档 PiggyIOCPServer_2008.rar Pig ...
- Android精美的日历控件
网上看到的精美日历控件,谨以此文记录一下,用到的时候再来翻翻 源码地址 : http://download.csdn.net/detail/abc13939746593/7265459
- 移动对meta的定义
以下是meta每个属性详解 尤其要注意的是content里多个属性的设置一定要用分号+空格来隔开,如果不规范将不会起作用. 一.<meta http-equiv="Content-Ty ...
- Delphi 自带的那个 Hand 光标很难看?没关系,一行代码解决问题:
Delphi 自带的那个 Hand 光标很难看?没关系,一行代码解决问题: Screen.Cursors[crHandPoint] := LoadCursor(0, IDC_HAND);放在主窗体 O ...
- [Papers]NSE, $\p_3u$, Lebesgue space [Cao, DCDSA, 2010]
$$\bex \p_3\bbu\in L^p(0,T;L^q(\bbR^3)),\quad \frac{2}{p}+\frac{3}{q}=2,\quad \frac{27}{16}\leq q\le ...