博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring常用注解,自动扫描装配Bean
阅读量:4518 次
发布时间:2019-06-08

本文共 2215 字,大约阅读时间需要 7 分钟。

1 引入context命名空间(在Spring的配置文件中),配置文件如下:

Xml代码
  1. xmlns:context="http://www.springframework.org/schema/context"    
  2. http://www.springframework.org/schema/context
  3. http://www.springframework.org/schema/context/spring-context-2.5.xsd  
xmlns:context="http://www.springframework.org/schema/context"  http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd

 

 

打开配置 <context:component-scan base-package="包名(扫描本包及子包)"/>

spring 会自动扫描cn.pic包下面有注解的类,完成Bean的装配。

 

Xml代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>    
  2. <beansxmlns="http://www.springframework.org/schema/beans"    
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.        xmlns:context="http://www.springframework.org/schema/context"           
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans    
  6.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd    
  7.            http://www.springframework.org/schema/context   
  8.            http://www.springframework.org/schema/context/spring-context-2.5.xsd">    
  9.              
  10.           <context:component-scanbase-package="cn.pic"/>    
  11. </beans>    

 

2 在classPath中加入注解用的jar包

lib\j2ee\common-annotations.jar

 

 

 

Spring 的context:component-scan扫描支持扫描jar包的方法:

eclipse自带的jar打包程序,默认打包的时候有个选项<Add directory entries>没有勾选,只要勾选了,就可以了.

 

 

-----------常用注解--------

 

--定义Bean的注解

 

@Controller

@Controller("Bean的名称")

定义控制层Bean,如Action

 

@Service         

@Service("Bean的名称")

定义业务层Bean

 

@Repository  

@Repository("Bean的名称")

定义DAO层Bean

 

@Component 

定义Bean, 不好归类时使用.

 

--自动装配Bean (选用一种注解就可以)

@Autowired  (Srping提供的)

默认按类型匹配,自动装配(Srping提供的),可以写在成员属性上,或写在setter方法上

 

@Autowired(required=true) 

一定要找到匹配的Bean,否则抛异常。 默认值就是true

 

@Autowired

@Qualifier("bean的名字")

按名称装配Bean,与@Autowired组合使用,解决按类型匹配找到多个Bean问题。

 

@Resource   JSR-250提供的

默认按名称装配,当找不到名称匹配的bean再按类型装配.

可以写在成员属性上,或写在setter方法上

可以通过@Resource(name="beanName") 指定被注入的bean的名称, 要是未指定name属性, 默认使用成员属性的变量名,一般不用写name属性.

@Resource(name="beanName")指定了name属性,按名称注入但没找到bean, 就不会再按类型装配了.

 

@Inject   是JSR-330提供的

按类型装配,功能比@Autowired少,没有使用的必要。

 

--定义Bean的作用域和生命过程

@Scope("prototype")

值有:singleton,prototype,session,request,session,globalSession

 

@PostConstruct

相当于init-method,使用在方法上,当Bean初始化时执行。

 

@PreDestroy

相当于destory-method,使用在方法上,当Bean销毁时执行。

 

--声明式事务

@Transactional 

 

转载于:https://www.cnblogs.com/yaowen/archive/2013/04/02/2994987.html

你可能感兴趣的文章
js div拖动动画运行轨迹效果
查看>>
Recipe 1.9. Processing a String One Word at a Time
查看>>
Linux 下查看系统是32位 还是64 位的方法
查看>>
MySQL 引擎 和 InnoDB并发控制 简介
查看>>
Dave Python 练习二
查看>>
.net知识体系
查看>>
第二章 第五节 获取帮助
查看>>
关于源代码及其管理工具的总结
查看>>
此文对你人生会有莫大好处的,建议永久保存 2013-07-26 11:04 476人阅读 评论(0) ...
查看>>
JQuery怎样返回前一页
查看>>
Best Time to Buy and Sell Stock
查看>>
Web服务器的原理
查看>>
记录ok6410 jlink 命令行调试uboot
查看>>
ASP.net 内置对象
查看>>
QT使用mysql
查看>>
判断有无网
查看>>
ASP.NET简介
查看>>
php开发环境搭建
查看>>
select模型的原理、优点、缺点
查看>>
进程调度优先级
查看>>