spring注解功能小记
2015-12-03 14:10
232 查看
想要引用注解功能,首先需要在applicationContext.xml中进行配置,beans头如下:
<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" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">[/code]
然后再在下面添加一行代码:<context:component-scan base-package="cn.com.test,cn.com.mv"></context:component-scan>
这其中base-package中指向的包就是可以使用注释的包,可以向这段代码中一样添加多个包,对于添加在这里的包,spring容器启动时会自动从中寻找添加了注解的类,然后注入,进行对象的初始化。
寻找的过程中寻找的注解是@Repository、@Service和@Controller,分别对应原来的DAO类、service类以及action类,非常的清晰。
添加了注解的类,诸如@Service(value="userService") public class UserService { @Autowired private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
就好比是在xml文件中添加了如下两行:<bean id="userService" class="cn.com....UserService" > <property name="user" ref="user"></property> <bean>
这样就实现了注解注入。
注入是spring容器中xml就可以实现的功能,而注解实现注入更多的则是为了简化xml文件。
相关文章推荐
- Java 计算返回当前第n个月
- 基于注解的Spring多数据源配置和使用
- super和this的用法JAVA
- 成为JavaGC专家(1)—深入浅出Java垃圾回收机制
- 在Spring Controller中将数据缓存到session
- java中关于try、catch、finally中的细节分析
- java中的数组
- 【原创】java NIO FileChannel 学习笔记 新建一个FileChannel
- JavaWeb开发之简单布局一个Servlet
- ubuntu java开发环境安装
- java解析json
- Java命令学习系列(四)——jstat
- Java命令学习系列(三)——Jmap
- MyEclipse 2014 对于java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I错误解决
- Java命令学习系列(二)——Jstack
- Java命令学习系列(一)——Jps
- Java命令学习系列(零)——常见命令及Java Dump介绍
- mysql用java写的步骤
- JAVA 教程推荐
- Java 运算符