1.在application.xml中配置annotation的根路径
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="platform.xml" />
<!-- 自动扫描beans -->
<context:component-scan base-package="xxx.server" />
</beans>
2.常用的注释说明:
1.bean对象自动注入
@Component("deviceDao")//注释其他组件
@Service("accountService")//注释业务层组件
@Controller用于标注控制层组件(如struts中的action)//这个没有暂时用到
@Repository用于标注数据访问组件,即DAO组件
2.配置文件中的键值对注入
@Value("${file.alarm.head}")对一些如xxx.properties文件中的文件,进行键值对的注入:在xxx.properties文件中一定有:file.alarm.head=....
3.使用
//1.作为类的属性定义使用
@Resource(name="deviceDao")
private IDeviceDao deviceDao;
//2.方法中使用,但是没有在该类中注入,直接从当前spring容器中拿bean对象
IDBDao dbDao =(IDBDao) SpringContext.getInstance().getAc().getBean("dbDao");
//3.Junit里使用(在Java代码中不建议这么使用,因为会新建一个spring容器)
ApplicationContext ac= new ClassPathXmlApplicationContext(new String[] {"appserver.xml"});
IDeviceService ds = (IDeviceService) ac.getBean("deviceService");
分享到:
相关推荐
Spring annotation 样例
Spring Annotation
spring annotation注解 使用Spring注解来注入属性
NULL 博文链接:https://cyhcheng.iteye.com/blog/390551
关于Spring annotation的详细介绍,包括 @Component,@Autowired 等,感觉还不错。
花了些时间做了一个实验,彻底弄懂了spring Annotation注入的方式。凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处...
详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。
spring的Annotation注解.
Spring的Annotation配置相关讲义
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/548314
spring annotation 从入门到精通
spring annotation example source code java maven
Spring注解开发代码+脑图+源码分析+spingmvc源码分析
SpringMVC Spring MyBatis 框架整合 Annotation Maven Project
NULL 博文链接:https://wolfsoft.iteye.com/blog/799226
SpringAnnotation