Springmvc注解注入的简单demo
今天看了注解注入觉得确实简化了xml配置,一般情况下Spring容器要成功启动的三大要件分别是:Bean定义信息,Bean实现类,以及spring本身。如果采取基于XML的配置,Bean信息和Bean实现类本身是分离的,而采用注解基于注解配置的方式,Bean定义信息通过在Bean实现类上标注注解实现,这种方式确实简化了xml配置,但是是不是某种程度增加了耦合?
常用的注解有Autowired、Resource、Qualifier、Service、Controller、Repository、Component,其中作用如下:
Autowired是自动注入,自动从spring的上下文找到合适的bean来注入,主要用来消除setter和getter方法
Resource用来指定名称注入
Qualifier和Autowired配合使用,指定bean的名称
Service,Controller,Repository分别标记类是Service层类,Controller层类,数据存储层的类,spring扫描注解配置时,会标记这些类要生成bean。
Component是一种泛指,标记类是组件,spring扫描注解配置时,会标记这些类要生成bean。
- 基于注解的方式
package com.xsf.springtest;
import org.springframework.stereotype.Repository;
@Repository
public class kiteDao {
private String driverType = "天空";
public kiteDao(String palce) {
this.driverType = place;
}
public kiteDao() {
}
public void flyKite(String kite) {
String insertMsg = String.format("把风筝 %s 放到了 %s", kite, driverType);
System.out.println(insertMsg);
}
}
下面使用常用的Autowired将刚才定义的Bean注解到addkite中去,该类为service类,因此该类标注@Service注解
package com.xsf.springtest;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class kiteService {
@Autowired
//将前面定义的kiteDao类的实例就会自动注入到kitedao的实例中了
private kiteDao kitedao;
public void addkite(String kite) {
this.kitedao.flyKite(kite);
}
}
最后一个写一个测试代码,运行下面的测试代码即可看到信息
package com.xsf.springtest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class flyKiteTest
{
public static void main( String[] args )
{
//它的构造函数接受一个package的名称,来限定要扫描的package。然后就可以通过appContext的getBean方法获得的实例了
ApplicationContext appContext = new AnnotationConfigApplicationContext("com.xsf.springtest");
;
kiteService service = appContext.getBean(kiteService.class);
service.addkite("大蝴蝶");
}
}
- 注解与xml混合的方式
在source文件夹下面新建spring.xml文件这里的bean id为kitedao与kiteService中的一致若不一致会报错(或者在@Autowired 后加上 @Qualifier("###")将kiteService中的kitedao换做对应的###即可)
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
<context:annotation-config />
<!-- 扫描包 -->
<context:component-scan base-package="com.xsf.springtest">
</context:component-scan>
<!-- 定义一个id为kitedao的Bean 他的构造函数palce的值为sqlite -->
<bean id="kitedao" class="com.xsf.springtest.kiteDao">
<constructor-arg name="place" value="树上" />
</bean>
</beans>
此时测试代码中使用ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring.xml");连接配置文件即可
package com.xsf.springtest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class flyKiteTest
{
public static void main( String[] args )
{
//它的构造函数接受一个package的名称,来限定要扫描的package。然后就可以通过appContext的getBean方法获得的实例了
//ApplicationContext appContext = new AnnotationConfigApplicationContext("com.xsf.springtest");
//通过spring配置文件加载类
ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring.xml");
kiteService service = appContext.getBean(kiteService.class);
service.addkite("大蝴蝶");
}
}
Springmvc注解注入的简单demo的更多相关文章
- 注解学习(模仿springMvc的注解注入方式)
最近在看springMvc的源码,看到了该框架的注入注解的部分觉的有点吃力,可能还是对注解的方面的知识还认识的不够深刻,所以特意去学习注解方面的知识.由于本人也是抱着学习的态度来阅读源码,若文章在表述 ...
- springMvc的注解注入方式
springMvc的注解注入方式 最近在看springMvc的源码,看到了该框架的注入注解的部分觉的有点吃力,可能还是对注解的方面的知识还认识的不够深刻,所以特意去学习注解方面的知识.由于本人也是抱着 ...
- 解决 SpringMVC 非spring管理的工具类使用@Autowired注解注入DAO为null的问题
在SpringMVC框架中,我们经常要使用@Autowired注解注入Service或者Mapper接口,我们也知道,在Controller层中注入service接口,在service层中注入其它的s ...
- 使用注解注入properties中的值的简单示例
spring使用注解注入properties中的值的简单示例 1.在web项目的src目录下新建setting.properties的文件,内容如下: version=1 2.在spring的xm ...
- springMVC 注解版
http://blog.csdn.net/liuxiit/article/details/5756115 http://blog.csdn.net/hantiannan/article/categor ...
- Spring+SpringMVC+MyBatis深入学习及搭建(十六)——SpringMVC注解开发(高级篇)
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/7085268.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(十五)——S ...
- springMVC注解方式+easyUI+MYSQL配置实例
刚接触springMVC,使用的注解方式,也在学习阶段,所以把自己学习到的记下来.本文利用springMVC从数据库读取用户信息为例,分享一下. 1.准备相关架包及资源.因为使用springMVC+e ...
- 《SpringMVC从入门到放肆》八、SpringMVC注解式开发(基本配置)
上一篇我们结束了配置式开发,配置式开发目前在企业中用的并不是很多,大部分企业都在使用注解式开发,所以今天我们就来学习注解式开发.所谓SpringMVC注解式开发是指,处理器是基于注解的类的开发方式.对 ...
- spring常用的一些注解以及注解注入总结
常用的spring注解有如下几种: @Controller@Service@Autowired@RequestMapping@RequestParam@ModelAttribute@Cacheable ...
随机推荐
- [PA 2014]Iloczyn
Description 斐波那契数列的定义为:k=0或1时,F[k]=k:k>1时,F[k]=F[k-1]+F[k-2].数列的开头几项为0,1,1,2,3,5,8,13,21,34,55,…你 ...
- [ZJOI2009]染色游戏
Description 一共n × m 个硬币,摆成n × m 的长方形.dongdong 和xixi 玩一个游戏, 每次可以选择一个连通块,并把其中的硬币全部翻转,但是需要满足存在一个 硬币属于这个 ...
- [SCOI2009]围豆豆
Description Input 第一行两个整数N和M,为矩阵的边长. 第二行一个整数D,为豆子的总个数. 第三行包含D个整数V1到VD,分别为每颗豆子的分值. 接着N行有一个N×M的字符矩阵来描述 ...
- [bzoj4820][Sdoi2017]硬币游戏
来自FallDream的博客,未经允许,请勿转载,谢谢. 周末同学们非常无聊,有人提议,咱们扔硬币玩吧,谁扔的硬币正面次数多谁胜利.大家纷纷觉得这个游戏非常符合同学们的特色,但只是扔硬币实在是太单调了 ...
- CentOS7.4 源码安装MySQL8.0
MySQL 8 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能! 以下为本人2018.4.23日安装过程的记录.整个过程大 ...
- 【Tensorflow系列】使用Inception_resnet_v2训练自己的数据集并用Tensorboard监控
[写在前面] 用Tensorflow(TF)已实现好的卷积神经网络(CNN)模型来训练自己的数据集,验证目前较成熟模型在不同数据集上的准确度,如Inception_V3, VGG16,Inceptio ...
- POJ-2299 Ultra-QuickSort---树状数组求逆序对+离散化
题目链接: https://vjudge.net/problem/POJ-2299 题目大意: 本题要求对于给定的无序数组,求出经过最少多少次相邻元素的交换之后,可以使数组从小到大有序. 两个数(a, ...
- JS 判断是否为IP格式
<html> <head> <title><a href='http://js.zz5u.net'><u>JavaScript</u& ...
- Tomcat访问路径去掉发布项目的项目名称
需求: 把发布到Tomcat下的web项目,访问路径去掉项目名称 实现方式及原理: 方式一: 原理:Tomcat的默认根目录是ROOT,实际上ROOT这个项目在实际生产环境是没有用的,所以我们可以用我 ...
- Mysql各种引擎原理实战对比
1)存储引擎概述: (2)MySQL各大存储引擎: (3)InnoDB和MyIsam使用及其原理对比: (4)InnoDB和MyIsam引擎原理: (5)剩余引擎的使用DEMO(主要是Mrg_Myis ...