温故知新:spring_03基于annotation配置IOC

相比较使用XML配置,基于annotation的IOC配置会极大的简化配置文件的内容,所以在beans.xml中无需再配置bean的设置,只需要开启组件扫描即可。
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="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-3.0.xsd
 7                         http://www.springframework.org/schema/context
 8                         http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 9 
10     <!-- 开启注解 -->
11     <context:annotation-config/>
12     <!-- 指定哪些需要加入扫描 -->
13     <context:component-scan base-package="org.duyt.*"/>
14 
15 </beans>
拿出一个Dao组件的注入为例
1 package org.duyt.dao;
2 public interface IUserDao {
3     public void add();
4     public void delete();
5     public void update();
6     public void load();
7 }
Dao的实现
 1 package org.duyt.dao.impl;
 2 
 3 import org.duyt.dao.IUserDao;
 4 import org.springframework.context.annotation.Scope;
 5 import org.springframework.stereotype.Repository;
 6 
 7 //使用Repository("userDao")标注UserDao类相当于在配置文件中写作
 8 //<bean id="userDao" class="org.duyt.dao.impl.UserDao"/>
 9 @Repository("userDao")
10 //相应的,Scope("singleton")标注就相当于的配置scope="singleton"
11 @Scope("singleton")
12 public class UserDao implements IUserDao {
13     public void add() {
14         System.out.println("用户增加方法");
15     }
16     public void delete() {
17         System.out.println("用户删除方法");
18     }
19     public void update() {
20         System.out.println("用户更新方法");
21     }
22     public void load() {
23         System.out.println("用户查询方法");
24     }
25 }
26 
spring提供了三种注解来对应MVC的三层,数据层的注解对应Repository,业务层对应service,表示层对应controller。还有一个公共的component,分开这么多是为了更好的标注当前这个待注入类的功能,如果不按照这些分类好的注解去注入也可以完成注入,但是还是规范的去写。关于对象中某个属性的注入看下面的action
 1 package org.duyt.action;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.duyt.domain.User;
 6 import org.duyt.service.IUserService;
 7 import org.springframework.context.annotation.Scope;
 8 import org.springframework.stereotype.Controller;
 9 
10 @Controller("userAction")
11 @Scope("prototype")
12 public class UserAction {
13     @Resource
14     private IUserService userService;
15     public String testString;
16     public User user;
17     
18     public UserAction() {
19     }
20     
21     public UserAction(String testString) {
22         super();
23         this.testString = testString;
24     }
25 
26     public void addUser(){
27         userService.add();
28     }
29     //get/set略
30 }
31 
对于某个要注入的属性,Spring自带的注解是@AutoWire,但是这种是按照类型注入的,为了避免接口多实现可能出现的问题,推荐可以使用javax.annotation提供的@resource,这种是按照属性的名称进行注入,可以直接在属性名之上添加注解,这样的话连属性的get/set都可以省了...或者是添加对应的get/set,在set方法上添加注解。但是要注意的是,按照属性名字进行注入,属性的名称需要和注解定义组件时的id一致。Action中的Iuserservice的实例名为userService,会寻找有@service("userService")的注解进行注入,否则会注入失败。
测试如下
 1 package org.duyt.test;
 2 import org.duyt.action.UserAction;
 3 import org.junit.Test;
 4 import org.springframework.beans.factory.BeanFactory;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 public class IocTest {
 7     private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
 8     @Test
 9     public void test(){
10         
11         //测试依赖注入
12         UserAction ua = (UserAction) factory.getBean("userAction");
13         UserAction ua2 = (UserAction) factory.getBean("userAction");
14         System.out.println(ua == ua2);
15         ua.addUser();
16     }
17 }
18 
结果为:
false
用户增加方法

posted on 2014-11-04 11:17 都较瘦 阅读(83) 评论(0)  编辑  收藏 所属分类: containerFramework


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

公告

博客定位:囿于目前的水平,博客定位在记录自己的学习心得和随手的练习

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜