Spring注解来注入bean

在classpath中扫描组件

组件扫描,即componetscanning

利用注解来扫描的组件有  @Component  :基本注解,表示一个受Spring管理的组件

@Respository:标识持久层组件

@Service:标识服务层(业务层)组件

@Controller:标识表现层

对于扫描组件,Spring有默认的命名策略,使用非限类名,第一个字母小写,也可以通过value属性值标识组件名称。值得说的是

1.        <context:include-filter>

2.       <context:exclude-filter> 这两个标签

要进行这两个标签的作用 首先要将 Use-default-filters=“false”的情况下才能使用

如果只想扫描指定包下面的Controller或者其他 则添加<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

如果不想扫描则用<context:exclude-filter>指定的不扫描

详情 请看 http://outofmemory.cn/java/spring/spring-DI-with-annotation-context-component-scan

之后代码简单实现

package com.annotion.service;

import org.springframework.stereotype.Service;

//模拟几个不同层
@Service("myservice")//这样就自定义注解名字了 myservice
public class MyService { public void say (){
System.out.println("I am MyService");
}
} package com.annotion.controller; import org.springframework.stereotype.Controller; @Controller
public class MyController {
public void say (){
System.out.println("I am Controller");
}
} package com.annotion.repository; import org.springframework.stereotype.Repository; @Repository
public class MyRespository {
public void say (){
System.out.println("I am Repository");
}
} package com.annotion.main; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.annotion.controller.MyController;
import com.annotion.repository.MyRespository;
import com.annotion.service.MyService; public class Main { public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");//在工程目录下读取bean的配置。
MyService ms = (MyService) context.getBean("myservice");//bean的名字默认时类的名字,只是将类名字第一个小写,如果要自定义,可以在注解类的时候用value值写上
// MyController mc = (MyController) context.getBean("myController");//上面一个注解名字就是改之后的,后面两个没改
// MyRespository mr = (MyRespository) context.getBean("myRespository");为什么要 ms.say();
// mc.say();
// mr.say(); }
}

bean文件

<!-- 如果要指定某些anntion 则可以使用 标签<context:include-filter>当然  use-defalut-filters要设置为false  -->
<context:component-scan base-package="com.annotion" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>

如果需要在使用注解的时候进行关联怎么办

使用 @Autowired  也可以使用 @Resource 或者@Inject  后两者功能类似,基本不用,这里只学习 @Autowired注解

使用@Autowired注解要注意几点

@Autowired 的自动装配可以在 方法(setter) 或者成员上面添加

自动装配的bean必须已经是IOC容器里面加载的bean,没有加载不行。

当能够兼容的装配对象不止一个的时候,可以使用@Qualifier指定名字(在方法里面使用@Qualifier整合框架里面比较常见),或者实现的多个兼容的bean中有指定装配bean的名字。

代码测试

package com.annotion.service;

public interface Dao {//公共接口
public void say ();
} package com.annotion.service;
import org.springframework.stereotype.Service; @Service
public class MyDao implements Dao {//其中一个实现 public void say() {
System.out.println("I am MyDao");
} } package com.annotion.service; import org.springframework.stereotype.Service; @Service
public class YouDao implements Dao {//其中一个实现 public void say() {
System.out.println("I am YouDao");
} } package com.annotion.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; @Service("myservice")//这样就自定义注解名字了 myservice
public class MyService { //@Autowired
//@Qualifier("myDao") 这是其中一个办法
private Dao dao; @Autowired//可以在上面也可以在下面用,因为有不止一个接口实现Dao类,所以要使用@Autowired
public void setDao(@Qualifier("myDao")Dao dao) {//这种方式比较常见,建议。
this.dao = dao;
} public void say (){
System.out.println("I am MyService");
dao.say();
}
} //bean文件无需动,主程序也不用动。输出
//I am MyService
//I am MyDao

泛型依赖注入

作用 来自http://www.cnblogs.com/solverpeng/p/5687304.html

一、为了更加快捷的开发,为了更少的配置,特别是针对 Web 环境的开发,从 Spring 4.0 之后,Spring 引入了 泛型依赖注入。

二、泛型依赖注入:子类之间的依赖关系由其父类泛型以及父类之间的依赖关系来确定,父类的泛型必须为同一类型。

通俗一点来说:两个子类之间的依赖关系不需要在子类中去声明,而是在父类中进行了声明,而依赖的纽带就是 泛型类型,必须是相同的父类泛型类型才具有依赖关系。

三、UML 类图:

说明:在 BaseService 中通过 @Autowired 注解自动装配了 BaseDao 的实例。而在 UserService 中并没有注入 UserDao 的实例,但是通过父类的泛型类型,

在 UserService 中已经注入了 UserDao 的实例。Department 与之类似。

四、例子:

package com.test2;

public class BaseRespository<T> {
public void add(){
System.out.println("Respository add...");
}
} package com.test2; import org.springframework.beans.factory.annotation.Autowired; public class BaseService<T> { @Autowired
protected BaseRespository<T> respository; public void add(){
System.out.println("add...");
System.out.println(respository);
} } package com.test2; import org.springframework.stereotype.Repository; @Repository
public class UserRespository extends BaseRespository<User> { } package com.test2; import org.springframework.stereotype.Service; @Service
public class UserService<User> extends BaseService<User> { } package com.test2; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test2.Person; public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");//在工程目录下读取bean的配置。
UserService us = (UserService)context.getBean("userService");//子类没有装配然而,父类已经帮忙装配,并且可以泛型引用,这大概就是泛型依赖注入了。
us.add(); }
}

Bean

<context:component-scan base-package="com.test2" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>

之后进入Spring面向切面编程 SpringAOP

Spring学习三的更多相关文章

  1. spring学习(三) ———— spring事务操作

    前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...

  2. Spring学习(三)--高级装配

    一.Spring profile 在开发软件的时候,有一个很大的挑战就是将应用程序从一个环境迁 移到另外一个环境.开发阶段中,某些环境相关做法可能并不适合迁 移到生产环境中,甚至即便迁移过去也无法正常 ...

  3. spring学习 三 框架的搭建

    1 导入jar包 spring启来最少要5个包,四个核心包和一个依赖的日志包 2 创建配置文件 在dynamic web project下的src目录下,创建一个spring的xml配置文件,名称可以 ...

  4. Spring学习(三)-----Spring自动装配Beans

    在Spring框架,可以用 auto-wiring 功能会自动装配Bean.要启用它,只需要在 <bean>定义“autowire”属性. <bean id="custom ...

  5. Spring学习三----------注入方式

    © 版权声明:本文为博主原创文章,转载请注明出处 Spring注入方式 本篇博客只讲最常用的两种注入方式:设值注入和构造器注入.代码为完整代码,复制即可使用. 1.目录结构 2.pom.xml < ...

  6. Spring学习(三)

    DI (Dependency Injection) 1.依赖注入,组件之间的依赖关系由容器在运行期间决定.Ioc容器注入应用程序某个对象,它所需要的外部资源(包括对象,资源,常量数据). birthd ...

  7. spring学习三:Spring Bean 生命周期

    Bean 的生命周期 理解 Spring bean 的生命周期很容易.当一个 bean 被实例化时,它可能需要执行一些初始化使它转换成可用状态.同样,当 bean 不再需要,并且从容器中移除时,可能需 ...

  8. spring学习三:Spring的Aop、代理

    ref:https://mp.weixin.qq.com/s/J77asUvw8FcnF-6YlX6AAw AOP相关术语:    Joinpoint(连接点):类里面可以被增强的方法,这些方法称为连 ...

  9. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

随机推荐

  1. Web版记账本开发记录(一)

    //index.js var util = require("../../utils/util.js"); //获取应用实例 var app = getApp(); Page({ ...

  2. 『算法设计_伪代码』贪心算法_最短路径Dijkstra算法

    Dijkstra算法实际上是一个贪婪算法(Greedy algorithm).因为该算法总是试图优先访问每一步循环中距离起始点最近的下一个结点.Dijkstra算法的过程如下图所示. 初始化 给定图中 ...

  3. 6月13 ThinkPHP框架基础

    ThinkPHP 一.php框架基础介绍 真实项目开发步骤: 多人同时开发项目,协作开发项目.分工合理.效率有提高(代码风格不一样.分工不好) 测试阶段 上线运行 对项目进行维护.修改.升级(单个人维 ...

  4. bootstrap table导出功能无效报错Uncaught INVALID_CHARACTER_ERR: DOM Exception 5和导出中文乱码问题

    由于表格数据中含有中文导致的,在网页的开发者选项中报一个 Uncaught INVALID_CHARACTER_ERR: DOM Exception 5 问题.这个问题是由于BootStrap tab ...

  5. windows 系统使用 git 和码云管理代码(本地已有项目)

    1. 为本地项目创建本地仓 找到项目所在的根目录(目录下有解决方案那个),右击目录,点击右键菜单中的“Git Bash Here”(前提是你的电脑已经装了Git,我用的是TortoiseGit) 然后 ...

  6. CompareTo 基于的排序算法

    CompareTo 基于的排序算法(高级排序) 这个是今天学习MapReduce时发现的,自定义类后实现了WritableComparable<>接口后实现了接口中的compareTo方法 ...

  7. Tomcat禁用SSLv3和RC4算法

    1.禁用SSLv3(SSL 3.0 POODLE攻击信息泄露漏洞(CVE-2014-3566)[原理扫描]) 编缉$CATALINA_HOEM/conf/server.xml配置文件,找到https端 ...

  8. Greys Java在线问题诊断工具

    摘要: 线上系统为何经常出错?数据库为何屡遭黑手?业务调用为何频频失败?连环异常堆栈案,究竟是那次调用所为? 数百台服务器意外雪崩背后又隐藏着什么?是软件的扭曲还是硬件的沦丧? 走进科学带你了解Gre ...

  9. Java Date实现加一天,年月日类推往后+1,日期+1,月份+1,年份+1

    System.out.println("String类型 "+endDate); //页面传递到后台的时间 为String类型 SimpleDateFormat sdf = new ...

  10. day30-python阶段性复习四

    九.函数 函数就是完成特定功能的一个语句组,这组语句可以作为一个单位使用,并且给它取一个名字. 降低编程难度 代码重用 可以通过函数名在程序的不同地方多长执行,这通常叫函数调用(.). 预定义函数 可 ...