------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

注解:

  说起注解,哇哦,每个人都或多或少的用到过

  像什么@Overried,@Test,@Param等等之前就早已熟悉的注解,现在要用注解实现di的注入

  注解的本质是什么?就是一个接口,他里面的参数是什么呢?就是这个接口里面的方法,so,我们怎么做?

案例如下:

  基于注解的jar包就不用说了,按照之前的博客走下来的无需再添加新的jar包

  还是俩个类,一个car,一个student,学生有一辆小汽车,基于注解的di注入

    Car类

package cn.dawn.day07annotationdi;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; /**
* Created by Dawn on 2018/3/5.
*/
@Component("car")
public class Car {
@Value("红色")
private String color;
@Value("奔驰")
private String type; public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
}
}

    @Componed("car")  //表示这个生成的对象的名字

    @Value("奔驰")    //用于给属性赋值

  Student类

package cn.dawn.day07annotationdi;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import javax.annotation.Resource; /**
* Created by Dawn on 2018/3/5.
*/
//student类
@Component("student")
public class Student {
@Value("老胡小子,呵呵哒")
private String name;
@Value("")
private Integer age; //@Resource(name = "car")
@Autowired
@Qualifier(value = "car")
private Car car; //带参构造
public Student(String name, Integer age, Car car) {
this.name = name;
this.age = age;
this.car = car;
} //无参构造
public Student() {
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Car getCar() {
return car;
} public void setCar(Car car) {
this.car = car;
}
}

    一样的就不做解释了,说一下下面这几个

    @Resource(name="car")    //这个是javax包下的注解,可以实现域属性的注入,下面还有一种方式,

    @AutoWried

    @Qualifier(value="car")    //这两行联用,他是spring的注解,也是给对象的域属性赋值

  

  在Spring的配置文件中,需要配置一点内容,首先导入命名空间context,和注解的包扫描器(我是idea,写完下面的节点,上面的命名空间自动生成)

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.dawn.day07annotationdi">
</context:component-scan> </beans>

  单测方法

package cn.dawn.day07annotationdi;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by Dawn on 2018/3/3.
*/
public class test20180306 { @Test
/*di注解注入*/
public void t01(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day07annotationdi.xml");
Student student = (Student) context.getBean("student");
System.out.println("学生"+student.getName()+"开"+student.getCar().getType());
}
}

SSM-Spring-07:Spring基于注解的di注入的更多相关文章

  1. Spring框架第四篇之基于注解的DI注入

    一.说明 与@Component注解功能相同,但意义不同的注解还有三个: 1)@Repository:注解在Dao实现类上 2)@Service:注解在Service实现类上 3)@Controlle ...

  2. 07 Spring框架 依赖注入(四)基于注解的依赖注入

    前面几节我们都在使用xml进行依赖的注入,但是在实际的开发中我们往往偏爱于使用注解进行依赖注入,因为这样更符合我们人的思维,并且更加快捷,本节就来讲述Spring基于注解的依赖注入: 信息注入注解 @ ...

  3. 使用 Spring 2.5 基于注解驱动的 Spring MVC

    http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/ 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sp ...

  4. 使用 Spring 2.5 基于注解驱动的 Spring MVC--转

    概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...

  5. spring boot 中@Autowired注解无法自动注入的错误

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...

  6. Spring IOC之基于注解的容器配置

    Spring配置中注解比XML更好吗?基于注解的配置的介绍提出的问题是否这种途径比XML更好.简单来说就是视情况而定. 长一点的答案是每一种方法都有自己的长处也不足,而且这个通常取决于开发者决定哪一种 ...

  7. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  8. Spring 使用AOP——基于注解配置

    首先,使用注解实现AOP是基于AspectJ方式的. 创建包含切点方法的类 package cn.ganlixin.test; import org.aspectj.lang.annotation.P ...

  9. Spring(七)之基于注解配置

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

随机推荐

  1. struts ajax多级下拉菜单

    先看项目截图: 看看要加入的jar包 除了struts核心的那个几个之外,我们还需要这两个 OK先看struts.xml <?xml version="1.0" encodi ...

  2. LeetCode之“链表”:Add Two Numbers

    题目链接 题目要求: You are given two linked lists representing two non-negative numbers. The digits are stor ...

  3. LIRe 源代码分析 5:提取特征向量[以颜色布局为例]

    ===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...

  4. 如何在SpriteBuilder中使用BM Font Label

    开始不知道,还真有点小繁琐. mac系统上创建BM Font的工具有不少,我主要用hiero和GlyphDesigner:前者是java写的,后者是mac原生的,功能都差不多. 还有一个类似的工具bm ...

  5. 海量数据处理 - 10亿个数中找出最大的10000个数(top K问题)

    前两天面试3面学长问我的这个问题(想说TEG的3个面试学长都是好和蔼,希望能完成最后一面,各方面原因造成我无比想去鹅场的心已经按捺不住了),这个问题还是建立最小堆比较好一些. 先拿10000个数建堆, ...

  6. 苹果新的编程语言 Swift 语言进阶(四)--字符串和收集类型

    一.字符串( String  )和字符类型(Character) 字符串是一种字符的带次序的收集类型(相当于数组),字符是字符串中的元素. 在Swift 语言中,字符串是编码独立的Unicode字符的 ...

  7. java--加强之 Java5的泛型

    转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9944389 36.入门泛型的基本应用 体验泛型: Jdk1.5以前的集合类中存在什么问题? A ...

  8. javascript算术运算溢出

    js中的算术在溢出overflow,下溢underflow或被0整除时不会报错. 当数字结果超过了数字上限时,结果为一个特殊的无穷大(infinity)值,在js中以Infinity表示:同理,当负数 ...

  9. python 3下基于select模型的事件驱动机制程序

    它的基本原理就是select/epoll这个function会不断的轮询所负责的所有socket,当某个socket有数据到达了,就通知用户进程.它的流程如图: 当用户进程调用了select,那么整个 ...

  10. ubuntu18.04 安装mysql 5.7.22

    后台下载,脱离终端控制 后台下载,可以节省ssh资源占用,且不会因为ssh连接断开而导致下载失败,适用于操作远端云服务器 wget -b 启动后台下载 -o 指定logfile(记录下载进度信息) w ...