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 配置移动到组件类本身. ...
随机推荐
- centos 下安装mysql-5.6.11
这次是在centos6.4下安装mysql,在安装之前,你要先确定你的linux已经安装了这些包: wget, gcc-c++, ncurses-devel ,cmake, make ,perl 如果 ...
- 【Android 应用开发】Android之Bluetooth编程
Android Bluetopth 编程大牛文章 http://my.oschina.net/u/994235/blog?catalog=313604 ViewGroup 相关资料 : http:// ...
- windows c/c++ 代码运行时间,毫秒级
#pragma once /* //计算代码段运行时间的类 // */ #include <iostream> #ifndef ComputeTime_h #define ComputeT ...
- SQLCODE和SQLERRM .
Oracle内置函数SQLCODE和SQLERRM是特别用在OTHERS处理器中,分别用来返回Oracle的错误代码和错误消息. OTHERS处理器应该是异常处理块中的最后的异常处理器,因为它是用来捕 ...
- Java Socket:Java-NIO-ServerSocketChannel
ServerSocketChannel 让我们从最简单的ServerSocketChannel来开始对socket通道类的讨论 ServerSocketChannel是一个基于通道的socket监听器 ...
- Java内部类与外部类
错误提示: 没有任何类型 TestThread 的外层实例可访问.必须用类型 TestThread 的外层实例(例如,x.new A(),其中 x 是 TestThread 的实例)来限定分配. pu ...
- rails4 new没有生成prototype.js之类的脚本解决办法
早期版本的rails在新生成程序时会在/public/javascript目录中自动放入若干个js脚本.不过在rails4.2.0中无论是否加-j选项,都不会生成这些脚本文件了.解决办法是安装prot ...
- 如何修改和查看tomcat内存大小
为了解决tomcat在大进行大并发请求时,出现内存溢出的问题,请修改tomcat的内存大小,其中分为以下两种方式: 一.使用 catalina.bat 等命令行方式运行的 tomcat 查看系统最大支 ...
- ubuntu 13.10 install wireshark
ubuntu 13.10 install wireshark 今天在使用java jsoup操作remote server的是否,在本地执行可以成功返回内容,然后打成jar包,使用shell在 ser ...
- 使用ASP.NET SignalR实现一个简单的聊天室
前言 距离我写上一篇博客已经又过了一年半载了,时间过得很快,一眨眼,就把人变得沧桑了许多.青春是短暂的,知识是无限的.要用短暂的青春,去学无穷无尽的知识,及时当勉励,岁月不待人.今天写个随笔小结记录一 ...