先引入jar包,common-annotations.jar

接着上代码:

1、dao接口
package com.dao;

public interface OkpDao {
public void save();
}

  

2、dao实现
package com.dao.impl;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import com.dao.OkpDao;
@Component
public class OkpDaoImpl implements OkpDao{ public void save() {
System.out.println("OkpDaoImpl.save()");
}
}

  

@Component注解:将该类加载为spring容器的组件,相当于在spring配置文件文件中的<bean name="okpDaoImpl" class="com.dao.Impl.OkpDaoImpl"/>,@Component注解格式为
@Component("name") 手动设置bean配置的name,如果不设置会自动设为 "类名首字母小写" 。
这四个注解用法基本一样,只是明确的分出哪个处理的层。@controller 控制器(注入服务) @service 服务(注入dao) @repository dao(实现dao访问) @component ,将其他几个注解标签替换上述代码的@Component标签会有同样的效果。

3、service
package com.service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.dao.OkpDao;
@Component
public class OkpService {
private OkpDao okpDao; public OkpDao getOkpDao() {
return okpDao;
}
@Resource(name="okpDaoImpl")
public void setOkpDao(OkpDao okpDao) {
this.okpDao = okpDao;
}
public void save(){
this.okpDao.save();
}
@PostConstruct
public void init(){ System.out.println("容器创建前执行");
} @PreDestroy
public void destory(){
System.out.println("容器销毁后执行");
}
}

@Component也是将name="okpService"的bean加入到spring容器 
@Resource将name="okpDaoImpl"的bean 注入到okpDao,该注解先根据ByName找相应的bean,如果没有找到就会根据ByType查找,现在推荐使用该注解,不推荐使用@Autowired(根据ByTpe装配)以及@Qualifier(根据ByName装配)---该两个注解一般联合使用

@Autowired
public void setOkpDao(@Qualifier("okpDaoImpl")OkpDao okpDao) {
this.okpDao = okpDao;
}

  


  因为根据ByType进行查找有可能会有多个bean,到处出错,所以还需要@Qualifier
  @PostConstruct 标记的方法代表容器创建之前执行的方法
  @PreDestory标记的方法代表容器销毁之后执行的方法
  还有一个不太常用的 @Scope代表该类的作用范围 value默认为Singleton单例模式,proptotype原型(每次都会创建一个新的对象)。
4、Service测试类
package com.service;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class OkpServiceTest { @Test
public void testSave() {
BeanFactory bf=new ClassPathXmlApplicationContext("applicationContext.xml");
OkpService okp=(OkpService) bf.getBean("okpService");
okp.save();
} }
  5、spring配置文件  applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com"></context:component-scan>
</beans>
  标签 <context:annotation-config/> 使用注解
  <context:component-scan base-package="com"></context:component-scan> 使用@Component注解 要加上这句,他会根据base-package的包名,扫描他下面所有的包,以及包中用@component注解的类 注册到Spring容器。
 
  由于刚开始学习Spring 注解,有什么理解有误的地方,求大婶告知。

最新文章

  1. class写法[tip]
  2. java学习第10天 (增加关于eclipse添项目)
  3. iOS之UI--使用SWRevealViewController实现侧边菜单功能详解实例
  4. DataGridView的按钮列的点击事件
  5. 007 The Inheritance In JAVA
  6. 无法在Web服务器上启动调试,与Web服务器通信时出现身份验证错误
  7. NPOI的使用
  8. myNote
  9. OC-KVO简介
  10. 用特殊字体来实现矢量ICON
  11. Tera Term——访问linux的ssh工具
  12. SpringMVC 自定义类型转换器
  13. NV12和NV21转rgb
  14. 关于Python的那些话
  15. 关于python深浅拷贝的个人浅见
  16. JAVA进阶5
  17. mybatis : ERROR. token : COMMA, pos : 373
  18. C++对象作为返回值的问题
  19. 简述 Spring Cloud 是什么1
  20. Omi框架学习之旅 - 插件机制之omi-transform及原理说明

热门文章

  1. vim常用指令一览
  2. YUI Array 之each| forEach(遍历)
  3. 高性能PHP日志插件--Seaslog
  4. mysql install
  5. Raising Modulo Numbers(POJ 1995 快速幂)
  6. [TYVJ] P1004 滑雪
  7. 大端模式&amp;小端模式、主机序&amp;网络序、入栈地址高低问题
  8. android学习精要
  9. kabina启动配置
  10. 转:Dynamic Binding Of RDLC To ReportViewer