一、注解

  使用注解之前要开启自动扫描功能

  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" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/mvc
  7. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd
  12. http://www.springframework.org/schema/tx
  13. http://www.springframework.org/schema/tx/spring-tx.xsd
  14. ">
  15.  
  16. <context:component-scan base-package="SpringAnnotationBll,SpringAnnotationDal" />
  17. </beans>

二、结构代码

    

三、常用注解

    @Service用于标注业务层组件、 
    @Controller用于标注控制层组件(如struts中的action)
    @Repository用于标注数据访问组件,即DAO组件。
    @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
    @Scope用于指定scope作用域的(用在类上)

四、实例

  4.1、dao层代码
  1. package SpringAnnotationDal;
  2.  
  3. public interface SpringAnnotationDal {
  4.  
  5. public abstract void add();
  6.  
  7. }
  1. package SpringAnnotationDal;
  2.  
  3. import org.springframework.stereotype.Repository;
  4.  
  5. @Repository
  6. public class SpringAnnotationDalBean implements SpringAnnotationDal {
  7.  
  8. /* (non-Javadoc)
  9. * @see SpringAnnotationDal.SpringAnnotationDal#add()
  10. */
  11. public void add()
  12. {
  13. System.out.print("add...");
  14. }
  15. } 
  4.2、service层代码
  1. package SpringAnnotationBll;
  2.  
  3. public interface SpringAnnotationBll {
  4.  
  5. public abstract void add();
  6.  
  7. }
  1. package SpringAnnotationBll;
  2.  
  3. import org.springframework.stereotype.Service;
  4.  
  5. import SpringAnnotationDal.SpringAnnotationDal;
  6. @Service
  7. public class SpringAnnotaionBllBean implements SpringAnnotationBll {
  8.  
  9. private SpringAnnotationDal springAnnotationDal;
  10. public SpringAnnotationDal getSpringAnnotationDal() {
  11. return springAnnotationDal;
  12. }
  13. public void setSpringAnnotationDal(SpringAnnotationDal springAnnotationDal) {
  14. this.springAnnotationDal = springAnnotationDal;
  15. }
  16. public void add()
  17. {
  18. System.out.print("add...");
  19. }
  20. }
  4.3、调用
  1. package test;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10.  
  11. import org.springframework.context.ApplicationContext;
  12. import org.springframework.context.support.ClassPathXmlApplicationContext;
  13.  
  14. import SpringAnnotationBll.SpringAnnotationBll;
  15. import spring.testServiceBean;
  16.  
  17. public class cservlet extends HttpServlet {
  18.  
  19. public void doGet(HttpServletRequest request, HttpServletResponse response)
  20. throws ServletException, IOException {
  21.  
  22. ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  23. SpringAnnotationBll services=(SpringAnnotationBll)context.getBean("springAnnotaionBllBean");
  24. services.add();
  25. }
  26.  
  27. } 

五、结果

 

Java 系列之spring学习--注解(三)的更多相关文章

  1. Java 系列之spring学习--springmvc注解方式(五)

    一.springmvc注解方式 注解方式使用的更多,更加灵活.在上一篇的博客的基础上修改springmvc-servlet.xml配置文件. <?xml version="1.0&qu ...

  2. Java 系列之spring学习--springmvc注解参数传递(六)

    一.绑定参数注解如下 @RequestParam     绑定单个请求数据,既可以是URL中的参数,也可以是表单提交的参数或上传的文件. 它有三个属性:  value    用于设置参数名. defa ...

  3. Java 系列之spring学习--spring搭建(一)

    一.新建maven项目 二.引入spring jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...

  4. Java 系列之spring学习--springmvc搭建(四)

    一.建立java web 项目 二.添加jar包 spring jar包下载地址http://repo.spring.io/release/org/springframework/spring/ 2. ...

  5. Java 系列之spring学习--依赖注入(二)

    一.依赖注入的三种方式 接口注入,set注入,构造函数注入 二.构造函数注入 2.1.测试类 package test; public class test01 { public String msg ...

  6. Spring注解 系列之Spring常用注解总结

    参考:Spring系列之Spring常用注解总结 (1) Resource 默认是byName的方式进行bean配置,@AutoWired默认是按照byType的方式进行装配bean的:(2)Comp ...

  7. Spring系列之Spring常用注解总结 转载

    Spring系列之Spring常用注解总结   传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.x ...

  8. spring 学习(三):aop 学习

    spring 学习(三):aop 学习 aop 概念 1 aop:面向切面(方面)编程,扩展功能不修改源代码实现 2 AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码 3 aop底层使用动态代 ...

  9. MyEclipse Spring 学习总结三 SpringMVC

    MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...

随机推荐

  1. .NET Core & EntityFrameworkCore

    前言 .NET Core 相比于 .NET Fromework 有跨平台.轻量化且开源的优势. 在使用 EntityFrameworkCore 的时候也遇到了很多问题,至于网络上的教程嘛...大部分都 ...

  2. ANE打包

    哈哈,曾经梦寐以求的ANE终于弄成功了一个.说实话,学java和Android就是为了写ANE!好啦,今天把我体会到的记录一下: 网上其实打包ANE的教程好多,我也找了好多好多.但是好多我自己试了还是 ...

  3. 我的C++笔记(数据的共享与保护)

    *数据的共享与保护: * .作用域: * 作用域是一个标识符在程序正文中有效的区域.C++中标识符的作用域有函数原型作用域.局部作用域(块作用域).类作用域和命名空间作用域. * ().函数原型作用域 ...

  4. React Native Windows下环境安装(一)

    1.安装chocolatey 以管理员权限运行命令提示符(cmd.exe) @powershell -NoProfile -ExecutionPolicy Bypass -Command " ...

  5. 很实用的html meta标签实现页面跳转

    就算你是有很多年开发经验的web开发工程师,有着很多web开发经验,对于先进的web开发技术有着很深刻的研究,然而你却忽略了那些最最基础的东西!现在我来问你,你是否对html所有的标签都能熟练的使用呢 ...

  6. Java中Json的用法

    1. JSONObject json = new JSONObject(); json.put("list", "test"); msg=json.toStri ...

  7. H3C交换机配置学习随笔

    1.交换机配置VLAN vlan 创建VLAN: <h3c>system-view [h3c]vlan 10 删除ID为10的vlan:undo vlan 10 注:任何型号的交换机,都支 ...

  8. PAT_A1115#Counting Nodes in a BST

    Source: PAT A1115 Counting Nodes in a BST (30 分) Description: A Binary Search Tree (BST) is recursiv ...

  9. 自编码器----Autoencoder

    一.自编码器:降维[无监督学习] PCA简介:[线性]原矩阵乘以过渡矩阵W得到新的矩阵,原矩阵和新矩阵是同样的东西,只是通过W换基. 自编码: 自动编码器是一种无监督的神经网络模型,它可以学习到输入数 ...

  10. LINUX KERNEL SPINLOCK使用不当的后果

    LINUX KERNEL SPINLOCK使用不当的后果 spinlock(自旋锁)是内核中最常见的锁,它的特点是:等待锁的过程中不休眠,而是占着CPU空转,优点是避免了上下文切换的开销,缺点是该CP ...