spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖。在Java代码中可以使用@Resource或者@Autowired注解方式来经行注入。虽然@Resource和@Autowired都可以来完成注入依赖,但它们之间是有区别的。首先来看一下:
a。@Resource默认是按照名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来装配注入;
b。@Autowired默认是按照类型装配注入的,如果想按照名称来转配注入,则需要结合@Qualifier一起使用;
c。@Resource注解是又J2EE提供,而@Autowired是由Spring提供,故减少系统对spring的依赖建议使用
@Resource的方式;
d。 @Resource和@Autowired都可以书写标注在字段或者该字段的setter方法之上
2、使用注解的方式,我们需要修改spring配置文件的头信息,修改部分红色标注,如下
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
</beans>
3、修改以上配置文件的头信息后,我们就可以在Java代码通过注解方式来注入bean,看下面代码
(1)@Resource
public class StudentService3 implements IStudentService {
//@Resource(name="studentDao")放在此处也是可行的
private IStudentDao studentDao;
private String id;
public void setId(String id) {
this.id = id;
}
@Resource(name="studentDao") //通过此注解完成从spring配置文件中查找名称为studentDao的bean来装配字段studentDao,如果spring配置文件中不存在studentDao名称的bean则转向按照bean类型经行查找
public void setStudentDao(IStudentDao studentDao) {
this.studentDao = studentDao;
}
public void saveStudent() {
studentDao.saveStudent();
System.out.print(",ID为:"+id);
}
}
配置文件添加如下信息
<bean id="studentDao" class="com.wch.dao.impl.StudentDao"></bean>
<bean id="studentService3" class="com.wch.service.impl.StudentService3" />
(2)@Autowired
public class StudentService3 implements IStudentService {
//@Autowired放在此处也是可行的
private IStudentDao studentDao;
private String id;
public void setId(String id) {
this.id = id;
}
@Autowired//通过此注解完成从spring配置文件中查找满足studentDao类型的bean
//@Qualifier("studentDao")则按照名称经行来查找转配的
public void setStudentDao(IStudentDao studentDao) {
this.studentDao = studentDao;
}
public void saveStudent() {
studentDao.saveStudent();
System.out.print(",ID为:"+id);
}
}
配置文件添加如下信息
<bean id="studentDao" class="com.wch.dao.impl.StudentDao"></bean>
<bean id="studentService3" class="com.wch.service.impl.StudentService3" />
4.spring2.5还提供了对受管组件的Classpath扫描,它可以使spring配置文件减少使用<bean></bean>来配置元数据bean
分享到:
相关推荐
spring依赖注入的最新技术实现 简单易懂,很实用的哟
还了解依赖注入如何与Spring,IoC容器,具有@ Component,@ Configuration和@Bean的bean定义,注入点,与@ Primary,@ Qualifier和自定义注释进行歧义消除一起工作。 使用Spring概要文件,按概要文件配置...
◆增强的绑定功能以及注释驱动的格式化:在标准的PropertyEditors之外增加了Converter以及Formatter SPI。 ◆全面的REST支持:在Spring MVC中天然支持REST,如REST式的请求映射,URI变量提取等。 ◆富Portlet 2.0...
Dart聚合物依赖注入这提供了使用配置的依赖项注入来定义可以注入到Autowired字段中的Bean。 这与使用批注和Java配置的Spring Dependency Injection非常相似。 通过搜索DOM,可以自动查找Bean。 这允许注入聚合物元素...
这些新特性包括:注解驱动的依赖性注入(annotation-driven dependency injection),使用注解而非XML元数据来自动侦测classpath上的Spring组件,注解对生命周期方法的支持,一个新的web控制器模型将请求映射到加...
它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为基础,将Spring.Java的核心概念与思想移植到了.NET平台上。 第一章 序言 第二章 ...
注解驱动的开发:MyBatis支持使用注解来配置和描述SQL语句,而Spring提供了注解驱动的开发方式。通过整合,可以灵活地使用MyBatis注解来定义SQL语句,同时享受Spring提供的依赖注入、AOP等功能。 模块化管理:通过...
这些新特性包括:注解驱动的依赖性注入(annotation-driven dependency injection),使用注解而非XML元数据来自动侦测classpath上的Spring组件,注解对生命周期方法的支持,一个新的web控制器模型将请求映射到加...
Ka Websocket 是一个注释驱动的框架,它允许开发人员以供应商中立和高效的方式使用服务器端 websocket 代码。 目标是在引入 websocket 支持后,为所有主要的 Servlet 容器/应用服务器提供支持。 还支持所有主要的...
6.4.4定义注释驱动事务 6.5小结 第7章保护spring 7.1springsecurity介绍 7.2验证用户身份 7.2.1配置providermanager 7.2.2根据数据库验证身份 7.2.3根据ldap仓库进行身份验证 7.3控制访问 7.3.1访问决策...
6.4.4 定义注释驱动事务 6.5 小结 第7章 保护Spring 7.1 Spring Security介绍 7.2 验证用户身份 7.2.1 配置Provider Manager 7.2.2 根据数据库验证身份 7.2.3 根据LDAP仓库进行身份验证 7.3 控制访问 ...
6.4.4 定义注释驱动事务 6.5 小结 第7章 保护Spring 7.1 Spring Security介绍 7.2 验证用户身份 7.2.1 配置Provider Manager 7.2.2 根据数据库验证身份 7.2.3 根据LDAP仓库进行身份验证 7.3 控制访问 ...
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
这些新特性包括:注解驱动的依赖性注入(annotation-driven dependency injection),使用注解而非XML元数据来自动侦测classpath上的Spring组件,注解对生命周期方法的支持,一个新的web控制器模型将请求映射到加...
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...
4.3 依赖注入 4.3.1 属性注入 4.3.2 构造函数注入 4.3.3 工厂方法注入 4.3.4 选择注入方式的考量 4.4 注入参数详解 4.4.1 字面值 4.4.2 引用其他Bean 4.4.3 内部Bean 4.4.4 null值 4.4.5 级联属性 4.4.6 集合类型...
Spring的核心思想是通过依赖注入(DI)和控制反转(IoC)来管理对象的生命周期、依赖关系等,从而简化了企业级应用的开发过程。 SpringMVC框架 SpringMVC是一个基于Java的实现MVC设计模式的请求驱动类型的轻量级Web...