[spring]Bean注入——使用注解代替xml配置
使用注解编程,主要是为了替代xml文件,使开发更加快速。
一、使用注解前提:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
//1:使用注解,需要在配置文件中增加命名空间和约束文件(Spring提供了一个context的命名空间,它提供了通过扫描类包以应用注解定义Bean的方式)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> //2:告诉框架那些类是使用注解的。
<!-- 激活组件扫描功能,扫描aop的相关组件组件 -->
<context:component-scan base-package="com.test.core.aop" />
...
</beans>
二、注解与xml配置的对应关系
注解
Package com.lsz.spring; @Repository
public class UserDao{
//。。。。
}
对应的xml配置文件
<bean id="userDao" class="com.lsz.spring.UserDao" />
[spring]Bean注入——使用注解代替xml配置的更多相关文章
- Spring Bean 注入 2 注解篇
1. 自动装配注解 配置applicationContext.xml开启注解 <?xml version="1.0" encoding="UTF-8"?& ...
- spring入门(二) 使用注解代替xml配置
1.导包(略) 2.applicationContext.xml如下: <?xml version="1.0" encoding="UTF-8"?> ...
- Spring使用AspectJ注解和XML配置实现AOP
本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...
- @ComponentScan注解及其XML配置
开发中会经常使用包扫描,只要标注了@Controller.@Service.@Repository,@Component 注解的类会自动加入到容器中,ComponentScan有注解和xml配置两种方 ...
- 实例化Bean的方法(基于xml配置)-http://blog.csdn.net/shymi1991/article/details/48153293
实例化Bean的方法(基于xml配置) 标签: spring framework 2015-09-01 13:43 918人阅读 评论(0) 收藏 举报 分类: Spring FrameWork(7 ...
- mybatis使用注解替代xml配置,动态生成Sql
mybatis使用注解替代xml配置时,遇到判断条件是否为null或者为空时,@Select很难搞定,不知道怎么办? mybatis3中增加了使用注解来配置Mapper的新特性,使用 SelectPr ...
- Spring基础篇——通过Java注解和XML配置装配bean
自动化装配的确有很大的便利性,但是却并不能适用在所有的应用场景,比如需要装配的组件类不是由自己的应用程序维护,而是引用了第三方的类库,这个时候自动装配便无法实现,Spring对此也提供了相应的解决方案 ...
- [spring]Bean注入——在XML中配置
Bean注入的方式有两种: 一.在XML中配置 属性注入 构造函数注入 工厂方法注入 二.使用注解的方式注入@Autowired,@Resource,@Required 本文首先讲解在XML中配置的注 ...
随机推荐
- mdf, ldf文件导入到sql server 2005的方法
mdf, ldf文件导入到sql server 2005的方法 在实际的工作中, 有很多涉及到数据库资料的备份,转移, 恢复等方面的工作, 但是并不是所有的资料都是以.bak格式存在的, 比如说, 你 ...
- NGS中的一些软件功能介绍
1.bowtie 短序列比对工具,blast也是短序列比对工具,速度快,结果易理解. 输入可以是fastq或者fasta文件. 生成比对结果文件sam格式的吧. 2.bwa 转自:https://ww ...
- Selenium2.0 Webdriver 随笔
Webdriver can't action the element when the element is out of view 1. Scroll to the element use Java ...
- xphrof出现502问题
This is an xhprof bug and not a devel bug, but I thought I'd throw the workaround up here in case pe ...
- struts.xml 配置文件的主要元素
1.package元素 作用: 在struts2的配置文件中引入了面向对象思想.分包管理,易于管理动作类,便于模块化开发动作类. 属性: name:包的名称.名称唯一. extends:一般情况下需要 ...
- Https之秘钥交换过程分析
一.概念回顾 A <------M------> B场景:A.B两个人之间通讯,A传输信息M给B,假定是在不安全的通路上传输. 1.明文传输 被中间人C拦截下来,可以随意篡改A发送给B的消 ...
- JVM内存—堆(heap)栈(stack)方法区(method) (转)
JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 堆区:1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令 ...
- Python+Appium API
1.contextscontexts(self): Returns the contexts within the current session. 返回当前会话中的上下文,使用后可以识别H5页面的控 ...
- C++ char float int string 之间的转换
string str = "123"; string 转 int int i = atoi( str.c_str() ); string 转 float float f = ato ...
- 安全SOCKET
导语 要使用安全Socket需要对密码学有一定的了解.在阅读本文之前最好能阅读一下下面几个网站的内容 http://kb.cnblogs.com/page/194742/ http://kb.cnbl ...