Spring IoC 注解配置
来源:互联网 发布:低价入手已备案域名 编辑:程序博客网 时间:2024/05/20 11:50
使用注解类:
需要在配置中添加context命名空间:如下
xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation=".. http://www.springframework.org/schema/contexthttp://springframework.org/schema/context/spring-context-3.0.xsd.."使用:
<context:component-scan base-package="包路径" ><context:include-fileter type="类型" expression="匹配" /> <!--包含匹配--><context:exclude-filter type="类型" expression="匹配路径" /> <!--排除匹配--></context:component-scan>过滤表达式: 详见过滤表达式篇(待添加= =)
基于注解的配置
标示:
@Component("xx")
public class Entity..
等价 <bean id="xx" class="com.bean.Entity" />
类似@Component的有@Repository @Service @Controller 对应mvc3个层次
自动装配:
@Autowired([requied=false|true])
requied属性标示 是否必须有此类型 true如果找不到这个类型则报出异常
@Qualifire("bean id");
指定要注入的类的类名,此注释可以注释在参数上
Bean的作用范围注释:
@Scope("范围名");
范围名:prototype singleton request session globalSession
检测属性是否设置:
@Required 设置此注解的属性必须初始化
生命周期
@PostConstruct
载入配置文件时运行
@PreDestory
关闭到时候运行
基于Java类的配置
@Configuration
在类上标注,为Spring容器提供Bean定义的信息
@bean
给方法标注,此方法提供Bean的实例化
Spring会对标注@bean的方法进行AOP改造,使之进行AOP注入操作
例如:
@Configurationpublic class A {@Beanpublic B getB(){return new B();}public Login logonService(){Login login=new Login();login.setB(getB());}}等同xml配置:
<bean id="a" class="xxxx.A" /> <bean id="logon" class="xxxx.Login" p:getB-ref="a" />
即A类提供对login类中的方法进行依赖注入
@Configruation有@Component注解的含义
即@Configruation会被context:component-scan所自动扫描入容器
基于注解的java属性配置
需要在配置文件中加入,导入的properties文件
<context:propterty-placeholder location="properties的位置" />
然后再类属性加入注解
@Value("${properties的key名}")
引用类的属性:
@Value("#{beanName.property}")
- Spring IoC 注解配置
- Spring IoC 注解配置
- Spring复习(二)IOC注解配置
- 注解方式配置Spring实现Ioc
- Spring-03-利用注解配置应用IOC
- 通过注解方式配置Spring的IoC
- spring中注解配置IOC失败异常
- Spring 基于注解的IOC配置
- 通过注解方式配置Spring实现Ioc
- Spring IOC 之注解配置、单元测试
- spring的配置及ioc注解总结
- Spring中IOC配置xml实现和IOC注解实现
- Spring IOC 常用注解
- Spring IOC 常用注解
- Spring ioc注解教程
- 关于Spring 注解 IOC
- Spring IoC常用注解
- spring ioc及注解
- ping检测整个网络的主机是否有响应
- 粒子生物学【经济学人】
- TCP与UDP协议
- 死锁
- SQL SERVER 批量插入记录
- Spring IoC 注解配置
- 深度Linux挂载盘
- 用C#来调用cmd命令程序,实现wifi控制的工具
- Java多线程——join方法
- subString的用法
- php手册之static
- Visual Studio 常用快捷键
- android开发笔记蓝牙
- 雪*C语言核心知识总结四