Bean的基于注解的配置方式
Boss.class
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import org.springframework.stereotype.Component; @Component
public class Boss {
public Boss(){
System.out.println("construct...");
} @PostConstruct
private void init1(){
System.out.println("execute in init1");
} @PostConstruct
private void init2(){
System.out.println("execute in init1");
} @PreDestroy
private void destory1(){
System.out.println("execute in destory1");
} @PreDestroy
private void destory2(){
System.out.println("execute in destory2");
} }
配置文件:
<?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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"
>
<context:component-scan base-package="com.smart.anno"/> 扫描类包以应用注解定义的Bean
</beans>
测试:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.*;
import static org.testng.Assert.*; public class AnnoAutowireTest{
public ApplicationContext factory = null;
private static String[] CONFIG_FILES = { "com/smart/anno/beans.xml" }; @BeforeMethod
public void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext(CONFIG_FILES);
} @Test
public void testAutoByName(){
Boss boss = (Boss) factory.getBean("boss");
assertNotNull(boss); }
}
Bean的基于注解的配置方式的更多相关文章
- Spring IoC — 基于注解的配置
基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...
- Spring框架bean的配置(3):基于注解的配置
1.基于注解的配置: @Component: 基本注解, 标识了一个受 Spring 管理的组件 @Respository: 标识持久层组件 @Service: 标识服务层(业务层)组件 @Contr ...
- (spring-第4回【IoC基础篇】)spring基于注解的配置
基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...
- Spring注解和配置方式
Spring提供了一个org.springframework.beans.factory.FactoryBean工厂类接口,用户可以通过实现该接口定制实例化Bean的逻辑. 从Spring3.0开始, ...
- Spring基于注解@Required配置
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
- Spring 基于注解的配置 简介
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
- Spring 基于注解零配置开发
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...
- Entity Framework入门教程(18)---EF6中基于代码进行配置方式
EF6中基于代码进行配置方式 我们以前对EF进行配置时是在app.config/web.config下的<entityframework>节点下进行配置的,EF6引进了基于代码的配置方法. ...
- Spring基于注解的配置概述
以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration.html: 从Spring 2.5开始 ...
随机推荐
- 手把手教你nginx/linux下如何增加网站
先进入到nginx的配置文件目录请输入以下命令 cd /alidata/server/nginx/conf/vhosts 再输入 ll 看看是不是像下面截图的一样 用神器xftp将default. ...
- Spark技术内幕: Task向Executor提交的源代码解析
在上文<Spark技术内幕:Stage划分及提交源代码分析>中,我们分析了Stage的生成和提交.可是Stage的提交,仅仅是DAGScheduler完毕了对DAG的划分,生成了一个计算拓 ...
- HDU1009_FatMouse' Trade【贪心】【水题】
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Webview页面的控件元素定位
前言 现在有很多App都是Hybrid的,即有原生的页面又有Webview的页面,元素的可以通过uiautomatorviewer工具 进行控件元素的定位,Webview页面的则无法通过此方式定位,而 ...
- EasyDarwin开源流媒体服务器性能优化之Work-stealing优化方案
本文转自EasyDarwin开源团队成员Alex的博客:http://blog.csdn.net/cai6811376/article/details/52400226 EasyDarwin团队的Ba ...
- libcurl理解和使用
1 libcurl是一个很好的客户端库 2 CURLOPT_URL 就是普通的url. 3 CURLOPT_HTTPHEADER 3.1 http get 4 CURLOPT_WRITEFUNCTIO ...
- appium(12)-The starting of an app
Steps: you have to prepare environment for Android. Details are provided here: http://appium.io/slat ...
- Python序列——Unicode
Unicode是什么 Python中的Unicode 编码与解码 在应用中使用Unicode的建议 1. Unicode是什么 Unicode是对字符进行编码的一种标准.而utf8或者utf-8是根据 ...
- 《unix环境高级编程》学习笔记【原创】
本文基于unix环境高级编程的学习的笔记,写的比较简如有不对,欢迎指点. 简单的描述下面函数的功能改变ctr+c信号原本的作用终止程序,在按下中断键的时候输出一句话. while循环主要读取用户的输入 ...
- javascript学习的思维导图
今天逛师父的博客园,发现了好东西~~~~我给偷过来了~~~那就是javascript学习的思维导图,比自己整理更快速. 思维导图小tips: 思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ...