使用注解配置spring
来源:互联网 发布:淘宝刀具规则 编辑:程序博客网 时间:2024/05/21 11:12
一、1、导包 4+2+spring-aop
2、引入约束(aop)
3、开启使用注解代替配置文件
<!--指定扫描com.mytest.bean包下的所有类中的注解注意:扫描包时会扫描指定包下的所有子孙包--><context:component-scan base-package="com.mytest.bean"></context:component-scan>
4、在类中使用注解完成配置
二、注解用途
1、将对象注册到容器
@Component("user") @Service("user")//service层 @controller("user")//web层 @Repository("user")//dao层
2、修改对象的作用范围
//指定对象的作用范围@Scope(scopeName="prototype")
3、值类型注入
@Value("Tom")private String name;
通过set方法赋值,推荐使用
@Value("Tom")public void setName(String name){ this.name=name;}
4、引用类型注入
@Autowired //自动装配private Car car;
@Autowired //自动装配//如果匹配多个类型一致的对象,将无法选择具体注入哪个对象,使用Qualifier注解告诉spring容器自动装配哪个名称的对象@Qualifier("car2")private Car car;
//手动注入,指定注入哪个名称的对象@Resource(name="car")private Car car;
5、初始化和销毁方法
//在对象创建后调用.init-method@PostConstrutpublic void init(){ System.out.println("我是初始化方法");}//在销毁之前调用.destory-method@PreDestorypublic void destory(){ System.out.println("我是销毁方法")}
阅读全文
0 0
- 使用注解配置spring
- 使用注解配置spring
- 使用注解配置spring aop
- 使用注解配置Spring AOP
- spring security3 配置使用注解
- 四.Spring之使用注解配置Spring
- Spring 使用注解配置使用ehcache
- spring使用注解@PostConstruct的xml配置
- CXF的Spring注解配置及使用
- CXF的Spring注解配置及使用
- spring使用注解时配置文件的配置
- 使用Spring 注解整合 Ehcache 配置
- Spring事务管理-使用注解配置事务
- 【Spring三】使用注解配置bean
- 【Spring五】AOP之使用注解配置
- spring—使用注解配置Bean
- 简化Spring XML配置---使用注解装配
- Spring配置使用注解注入bean
- Eclipse安装svn的几种方式
- 如何将excel中的数据导入到数据库
- 每日AC -数串
- 三大排序
- kafkaclient示例
- 使用注解配置spring
- 常用的十八大学术搜索引擎
- PHP封装的一个单例模式Mysql操作类
- 堆和栈的区别
- Fedora 26 为多种系统提供稳定保障!
- php 常用日期处理函数,时间戳处理函数等,如下讲解
- Jsp+Servlet对数据库的增删改查
- 【Spark 2.0官方文档】Spark SQL、DataFrames以及Datasets指南
- 金山词霸2009牛津版