[分享]Spring中使用注解的步骤_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2012 | 回复: 0   主题: [分享]Spring中使用注解的步骤        下一篇 
shellyC1314
注册用户
等级:少校
经验:1334
发帖:75
精华:0
注册:2015-7-14
状态:离线
发送短消息息给shellyC1314 加好友    发送短消息息给shellyC1314 发消息
发表于: IP:您无权察看 2015-7-17 10:40:53 | [全部帖] [楼主帖] 楼主

引入jar包并在src中创建核心配置文件applicationContext.xml

在applicationContext.xml中声明使用注解形式进行Spring配置

*在applicationContext.xml中引入spring-context-2.5.xsd模式

<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.5xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd”>


*声明使用注解

<context:annotation-config/>


*声明注解使用的范围

<context:component-scan base-package=”com.softeem.pojos”/>


在对应的范围的类中进行注解配置

@Component[(“id”)]:用在类名之前,用来声明这个类被Spring管理,相当于xml中的bean标签 ;如果不指定id,则id值默认为类名首字母改小写
@Autowired[ (required=false) ]:用在类中属性或者属性对应的set方法前面。


给类中的自定义对象属性完成自动装配,相当于xml中bean标签的autowire属性。当设置了required=false时,如果没有找到对应类型的bean则将属性设置为null

@Qualifier(value="g2"):@Autowired默认是根据类型进行自动装配,当添加了@Qualifier之后就变成了根据名字进行自动装配
@Scope("prototype"):用在类名之前,用来指定当前bean的作用域
@PostConstruct、@PreDestroy:用在类中方法之前,用来指定初始化方法和销毁方法,相当于bean标签中的init-method属性和destory-method属性


该贴被shellyC1314编辑于2015-7-17 10:41:51




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论