SSM-Spring-07:Spring基于注解的di注入
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
注解:
说起注解,哇哦,每个人都或多或少的用到过
像什么@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注入的更多相关文章
- Spring框架第四篇之基于注解的DI注入
一.说明 与@Component注解功能相同,但意义不同的注解还有三个: 1)@Repository:注解在Dao实现类上 2)@Service:注解在Service实现类上 3)@Controlle ...
- 07 Spring框架 依赖注入(四)基于注解的依赖注入
前面几节我们都在使用xml进行依赖的注入,但是在实际的开发中我们往往偏爱于使用注解进行依赖注入,因为这样更符合我们人的思维,并且更加快捷,本节就来讲述Spring基于注解的依赖注入: 信息注入注解 @ ...
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/ 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sp ...
- 使用 Spring 2.5 基于注解驱动的 Spring MVC--转
概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...
- spring boot 中@Autowired注解无法自动注入的错误
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...
- Spring IOC之基于注解的容器配置
Spring配置中注解比XML更好吗?基于注解的配置的介绍提出的问题是否这种途径比XML更好.简单来说就是视情况而定. 长一点的答案是每一种方法都有自己的长处也不足,而且这个通常取决于开发者决定哪一种 ...
- Spring MVC中基于注解的 Controller
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...
- Spring 使用AOP——基于注解配置
首先,使用注解实现AOP是基于AspectJ方式的. 创建包含切点方法的类 package cn.ganlixin.test; import org.aspectj.lang.annotation.P ...
- Spring(七)之基于注解配置
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
随机推荐
- Customer Form Issue: Automatic Matching Rule Set Defaults Value AutoRuleSet-1
In this Document Symptoms Changes Cause Solution References APPLIES TO: Oracle Receivables ...
- Android实训案例(七)——四大组件之一Service初步了解,实现通话录音功能,抽调接口
Service Service的神奇之处,在于他不需要界面,一切的操作都在后台操作,所以很多全局性(手机助手,语音助手)之类的应用很长需要这个,我们今天也来玩玩 我们新建一个工程--ServiceDe ...
- 中国象棋游戏Chess(2) - 走棋
之前的文章请看:中国象棋游戏Chess(1) - 棋盘绘制以及棋子的绘制 现在实现走棋的功能. 首先需要获取点击到的棋子,用QWidget中的函数 mouseReleaseEvent 实现函数: vo ...
- 有关java的引用传递,直接操作对象本身。直接删除BE的value中某值
HashSet<String> refRegions = BE.get(regionName); HashSet<String> values = new HashSet ...
- UIScrollView的无限左滑轮播一点也不难
UIScrollView的轮播在如今的app中用得十分广泛,最初实现的时候方式比较拙劣,滚动到最后一个视图时再返回到第一个看起来非常的不连贯. 今天查询UIScrollView轮播资料,总结两种比较喜 ...
- 关于SharePoint2007简单随感
首先,还是要感谢我毕业以后的这第一份正式工作,当然现在也依然在做,带我走进了SharePoint的世界,很奇妙也许是有缘吧,自己不是个努力的人,从面试的时候对Moss这个东西闻所未闻,到现在一知半解, ...
- obj-c编程13:归档
这篇归档内容的博文也挺有趣的,笨猫对好玩的东西一向感兴趣啊!如果用过ruby就会知道,obj-c里的归档类似于ruby中的序列化概念,不过从语法的简洁度来说,我只能又一次呵呵了. 下面大家将会看到2种 ...
- ruby中printf "%x"%-4为何会打印开头..
先看一下ruby中printf "%x" % -4的返回结果: irb(main):134:0> printf "%x\n" % -4 ..fc 前面的. ...
- climbing stairs(爬楼梯)(动态规划)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- CSDN的博客搜索功能不又给力了呵呵呵呵
不得不说,CSDN博客的搜索功能是在太弱了.而且一直都很弱,以至于我每次想在自己博客上找自己发的文章都变得那么难.做一个搜索博客内文章的功能没有那么难吧? 还是说CSDN已经放弃了博客这一块了? 我发 ...