Spring 的“零配置”支持“ 的学习
来源:互联网 发布:汉王扫描软件下载 编辑:程序博客网 时间:2024/05/17 04:53
原文链接:http://www.cnblogs.com/jbelial/archive/2012/06/05/2537087.html
1、搜索Bean类
在不实用Spring配置文件来配置任何Bean实例,那么我们只能指望Spring会自动搜索某些路径下的Java类,并将这些java类注册为Bean实例。
Spring会合适的将显示指定路径下的类全部注册成Spring Bean。 Spring通过使用特殊的Annotation来标注Bean类。
>@Component :标注一个普通的Spring Bean类。
>@Controller : 标注一个控制器组件类。
>@Service: 标注一个业务逻辑组件类。
>@Repository : 标注一个DAO组件类。
使用@Component 来标注一个类:
package org.service.imp;import org.service.Person;import org.springframework.stereotype.*;@Componentpublic class Chinese implements Person { public String sayHello(String name) { // TODO Auto-generated method stub return name + ":你好!"; } public void eat(String food) { // TODO Auto-generated method stub System.out.println(”我喜欢吃:“+food) ; }}
接下来需要在Spring的配置文件中指定搜索路径,Spring将会自动搜索该路径下的所有JAva类,并根据这些类创建Bean实例:
<?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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 自动搜索指定包及其子包下的所有Bean类--> <context:component-scan base-package="org.service"> </context:component-scan></beans>
主程序:
package org.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test { public static void main(String[] args) { //创建Spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); // 获取Spring 容器中的所有Bean实例的名 System.out.println("---------------------" +java.util.Arrays.toString(ctx.getBeanDefinitionNames())) ; }}
通过运行主程序;可以得到 Spring中的Bean实例的名字是:chinese。Bean实例的名字默认是Bean类的首字母小写,其他地方不变。当然,Spring也允许在使用@Component 标注时指定Bean实例的名称。格式如:@Component(”Bean实例名字")
在默认情况下,Spring会自动搜索所有以@Component、@Controller 、@Service 和 @Repository 标注的Java类,并将他们当作Spring Bean来处理。
我们还可以通过为<component-scan .. /> 元素添加 <include-filter .. /> 或 <exclude-filter ../>子元素来指定Spring Bean类 , 只要位于指定的路径下的Java类满足这种重柜子,即使Java类没有使用Annotation 标注,Spring一样可以把它们当成 Bean类来处理。
<include-filter ... /> 元素使用与指定满足该规则的Java类 会被当成Bean类,而<exclude-filter .../>相反。
> type : 指定过滤器类型。
> expression : 指定过滤器所需要的表达式。
Spring 内建支持如下过滤器。
> annotation : Annotation 过滤器 ,该过滤器需要指定一个Annotation名.
> assignable : 类名过滤器, 该过滤器直接指定一个Java类
> regex : 正则表达式过滤器,该过滤器指定一个正则表达式 , 匹配该正则表达式的Java类将满足过滤规则。
> aspectj : AspectJ过滤器。
2、指定Bean的作用域:
采用零配置方式来管理Bean 实例时,可以使用@Scope Annotation 在指定Bean 的作用域。
3、使用@Resource配置依赖:
@Resource 有一个name属性,在默认情况下,Spring将这个值解释为需要被注入的Bean实例的名字。相当于<property../>元素的 ref 属性的效果 。
4、使用@PostConstruct 和 @PreDestroy 制定生命周期行为
前者是修饰的方法是Bean的初始化方法,后者修饰的方法是Bean 销毁之前的方法。这与Spring 提供的<bean .. /> 元素指定的 init-method 、destroy-method 属性的作用差不多。
- Spring 的“零配置”支持“ 的学习
- spring 的"零配置"支持
- Spring-----14、Spring的“零配置”支持
- 八、Spring的零配置支持
- Spring基于注解的零配置支持
- 【Spring笔记】(四) 8.2 Spring的零配置支持
- Spring的“零配置”支持,用annotation替代xml
- spring的零配置
- spring的零配置-----注解规约配置
- Spring的零配置(Annotation)
- 码农小汪-spring框架学习之7-spring的零配置 @Component @Controller @Service
- Spring 的HibernateTemplate 支持配置
- spring 配置Spring对Cache的支持
- Spring AOP Annotation 的“零配置” 的学习方法(一)
- spring零配置(Annotation)学习笔记
- 配置spring对乱码的支持
- Spring 的HibernateTemplate 支持两种配置
- spring 基于XML配置的Cache支持
- gridview 编辑,删除,更新的用法
- VS远程调试方法
- 代码优化学习笔记
- 配置本地环境之phpmyadmin安装
- XML Schema和XML DTD
- Spring 的“零配置”支持“ 的学习
- [Qt教程] 第43篇 进阶(三)对象树与拥有权
- 二次排序原理
- java如何使Double类型保留两位小数
- Recall Sent Message in Outlook2010
- Android使用Application总结
- CodeProject每日精选: Grid controls(转)
- phpscan是php程序木马扫描器
- TX视频真实地址解析及OC编码实现