spring常用注解使用解析
spring没有采用约定优于配置的策略,spring要求显示指定搜索哪些路径下的Java文件。spring将会把合适的java类全部注册成spring Bean。
<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">
<!-- 自动扫描指定包及其子包下的所有Bean类 -->
<context:component-scan
base-package="org.crazyit.app.service"/>
</beans>
<!-- 自动扫描指定包及其子包下的所有Bean类 -->
<context:component-scan
base-package="org.crazyit.app.service">
<!-- 只将以Chinese、Axe结尾的类当成Spring容器中的Bean -->
<context:include-filter type="regex"
expression=".*Chinese"/>
<context:include-filter type="regex"
expression=".*Axe"/>
</context:component-scan>
@Controller
public class demo {
@Resource(name="user")
private User user;
@Resource(name="user")
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
}
@Controller("demo")
@Scope("prototype")
public class demo {
}
@Component
public class SteelAxe
{
public SteelAxe()
{
System.out.println("创建SteelAxe类对象实例...");
}
}
@Component
public class Chinese
{
// 执行Field注入
@Resource(name="steelAxe")
private SteelAxe steeAxe; public Chinese() {
super();
System.out.println("创建Chinese类对象实例...");
}
@PostConstruct
public void init()
{
System.out.println("正在执行初始化的init方法...");
}
@PreDestroy
public void close()
{
System.out.println("正在执行销毁之前的close方法...");
}
}
// 创建Spring容器
AbstractApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
// 注册关闭钩子
ctx.registerShutdownHook();
打印:
创建Chinese类对象实例...创建SteelAxe类对象实例...正在执行初始化的init方法...正在执行销毁之前的close方法...@Component
public class Chinese
{
// 执行Field注入
//@Resource(name="steelAxe")
//private SteelAxe steeAxe;
public Chinese() {
super();
System.out.println("创建Chinese类对象实例...");
}
@PostConstruct
public void init()
{
System.out.println("正在执行初始化的init方法...");
}
@PreDestroy
public void close()
{
System.out.println("正在执行销毁之前的close方法...");
}
}
创建Chinese类对象实例...正在执行初始化的init方法...创建SteelAxe类对象实例...正在执行销毁之前的close方法...spring常用注解使用解析的更多相关文章
- SpringBoot+Spring常用注解总结
为什么要写这篇文章? 最近看到网上有一篇关于 SpringBoot 常用注解的文章被转载的比较多,我看了文章内容之后属实觉得质量有点低,并且有点会误导没有太多实际使用经验的人(这些人又占据了大多数). ...
- Spring系列之Spring常用注解总结
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
- Spring常用注解介绍【经典总结】
Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式. Spring注解方式减少了配置文件内容 ...
- Spring常用注解总结
转载自:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点 ...
- Spring注解 系列之Spring常用注解总结
参考:Spring系列之Spring常用注解总结 (1) Resource 默认是byName的方式进行bean配置,@AutoWired默认是按照byType的方式进行装配bean的:(2)Comp ...
- Spring系列之Spring常用注解总结 转载
Spring系列之Spring常用注解总结 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.x ...
- spring常用注解笔记
spring常用注解解释: 1. Mybatis的映射文件xxxMapper.xml中resultMap标签的作用 resultMap标签是为了映射select查询出来结果的集合,其主要 作用是将实体 ...
- Spring常用注解用法总结
转自http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Dispat ...
- Spring常用注解汇总
本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component- ...
随机推荐
- Supplemental Logging
Supplemental Logging分为两种:Database-Level Supplemental Logging和Table-Level Supplemental Logging,即数据库级别 ...
- [C] C语言中的布尔值
C不具备显示的布尔类型,所以使用整数来代替,规则是:零是假,任何非零值皆为真. 反过来说,如果逻辑表达式为真其值一定为真,若逻辑表达式为假其值一定为零.
- 【Android】[转] Android Handler应设为static
android开发中,使用Lint检测时会提示这么一句话 : This Handler class should be static or leaks might occur.意为handler应用s ...
- [logstash-input-redis]插件使用详解
Redis插件参数配置详解 最小化配置 input { redis { data_type => "list" #logstash redis插件工作方式 key => ...
- IE9父容器overflow:auto时,子容器状态更改导致滚动条下出现额外空间的问题探讨
IE的每次跟新都会有一些奇葩的bug,我们默默承受了. 这个问题在项目中出现困扰了我近一个星期,这里记录一下.看下面实例 <style> .panel{ width: 200px; ove ...
- 说说我在项目中为什么不用实体框架,如果说我在诋毁你所爱的EF,请进来.
1.坑多. 这一点没有人会否定.当然你可以说你很牛,但事实不会因为你牛就可以说不存在.从博客园中的博问中大家关于EF的提问量就问题的怪异程度就可以看出来. 1.Entity Framework 查询历 ...
- 【Android】Fragment的简单笔记
被虐了,做某公司笔试时,发现自己连个Fragment的生命周期都写不详细.平时敲代码,有开发工具的便利,有网上各大神的文章,就算忘了也很容易的可以查到,但当要自己不借助外界,却发现自己似乎对该知识点并 ...
- Auto Mapper02《demo》
学习这些基本上网上都有一些教程或者别人做的demo,我是按照这个方式去学习的.先做个demo,学会如何去使用它,接着去慢慢的了解它是如何的运行的,理解里面的一些基本的基础知识.我们不可以再像 ...
- 写给自己的 程序集&msil 扫盲
嘴上不说 心里却想MD 这家伙在博客园装了这么久的高手 竟然连这都不会 ,我去噢. 程序集签名 .net 下 “程序集” 什么东东 ,反正就是听着挺牛x的,其实就是指“一堆程序”从我们传统的C++封装 ...
- Visual Studio 2015正式企业(Enterprise)版
“7月20日 23:30 Visual Studio 2015正式版正式发布,作为微软新一代开发利器,在全地球乃至全宇宙乃至全太阳系中最强大 且没有之一的IDE(上述描述来自微博用户评论)跨平台支持成 ...
