Spring框架学习之IOC(二)

接着上一篇的内容,下面开始IOC基于注解装配相关的内容

在 classpath 中扫描组件

  <context:component-scan>

特定组件包括:
–@Component: 基本注解, 标识了一个受 Spring 管理的组件
–@Respository: 标识持久层组件
–@Service: 标识服务层(业务层)组件
–@Controller: 标识表现层组件
对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称
用一个简单的代码演示一下
 1 package per.zww.spring.beans.annotation;
2
3 import org.springframework.stereotype.Component;
4
5 @Component
6 public class TestObject {
7 public void test(){
8 System.out.println("testObject...");
9 }
10 }
 1 package per.zww.spring.beans.annotation.respository;
2
3 import org.springframework.stereotype.Repository;
4
5 @Repository("userRepository")
6 public class UserRepository {
7 public void repository() {
8 System.out.println("repository...");
9 }
10 }

xml:

    <context:component-scan base-package="per.zww.spring.beans.annotation.*"></context:component-scan>

测试:

package per.zww.spring.beans.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("annotation.xml");
TestObject testObject=(TestObject) applicationContext.getBean("testObject");
testObject.test();
UserRepository userRepository=applicationContext.getBean(UserRepository.class);
userRepository.repository();
}
}

另外我们可以过滤掉一些类:

–<context:include-filter> 子节点表示要包含的目标类
–<context:exclude-filter> 子节点表示要排除在外的目标类
 
组件装配:Bean属性的依赖
  <context:component-scan> 元素还会自动注册 AutowiredAnnotationBeanPostProcessor 实例, 该实例可以自动装配具有 @Autowired 和 @Resource 、@Inject注解的属性.
  当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称
用代码来演示一下组件装配
package per.zww.spring.beans.annotation.service;

public interface UserService {
void service();
}
package per.zww.spring.beans.annotation.service;

import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImp implements UserService{ @Override
public void service() {
System.out.println("service...");
} }
package per.zww.spring.beans.annotation.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller; import per.zww.spring.beans.annotation.service.UserService; @Controller
public class UserController {
    @Autowired
    @Qualifier("userService") //可以用Qualifier来精确匹配
    public UserService userService;
    
    public void controller() {
        System.out.println("controller...");
        userService.service();
    }
}

测试:

package per.zww.spring.beans.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import per.zww.spring.beans.annotation.controller.UserController;
import per.zww.spring.beans.annotation.service.UserService; public class Main {
public static void main(String[] args) { UserController userController=(UserController) applicationContext.getBean("userController");
userController.controller();
}
}
Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired 注解的功用类似,但推荐使用@Autowired注解。
 
转至:http://www.cnblogs.com/zhaoww/p/5104822.html

Spring框架学习之IOC(二)的更多相关文章

  1. Spring框架学习之IOC(一)

    Spring框架学习之IOC(一) 先前粗浅地学过Spring框架,但当时忙于考试及后期实习未将其记录,于是趁着最近还有几天的空闲时间,将其稍微整理一下,以备后期查看. Spring相关知识 spri ...

  2. spring框架学习(一)——IOC/DI

    什么是Spring框架: Spring是一个基于IOC和AOP的结构J2EE系统的框架: IOC 反转控制 是Spring的基础,Inversion Of Control,简单说就是创建对象由以前的程 ...

  3. Spring框架学习一

    Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...

  4. Spring框架学习1

    AnonymouL 兴之所至,心之所安;尽其在我,顺其自然 新随笔 管理   Spring框架学习(一)   阅读目录 一. spring概述 核心容器: Spring 上下文: Spring AOP ...

  5. Spring框架学习总结(上)

    目录 1.Spring的概述 2.Spring的入门(IOC) 3.Spring的工厂类 4.Spring的配置 5.Spring的属性注入 6.Spring的分模块开发的配置 @ 1.Spring的 ...

  6. Spring框架学习笔记(1)

    Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...

  7. Yii框架学习笔记(二)将html前端模板整合到框架中

    选择Yii 2.0版本框架的7个理由 http://blog.chedushi.com/archives/8988 刚接触Yii谈一下对Yii框架的看法和感受 http://bbs.csdn.net/ ...

  8. spring框架学习(三)junit单元测试

    spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...

  9. spring 框架学习网站

    spring 框架学习网站 NO1 http://www.mkyong.com NO2 https://spring.io/docs/reference

随机推荐

  1. redis-trib构建集群

    https://blog.csdn.net/qq_35946990/article/details/78957618

  2. 那些让人睡不着觉的bug,你有没有遭遇过?

    我先讲一个小故事,以前在外企工作时的一个亲身经历. 当时我所在的team,负责手机上多媒体Library方面的开发.有一天,一个具有最高等级的bug被转到了我的手上.这个bug非常诡异,光是重现它就需 ...

  3. MySQL集群系列2:通过keepalived实现双主集群读写分离

    在上一节基础上,通过添加keepalived实现读写分离. 首先关闭防火墙 安装keepalived keepalived 2台机器都要安装 rpm .el6.x86_64/ 注意上面要替换成你的内核 ...

  4. Android开发 获取当前activity的屏幕截图(转载)

    首先通过下面的函数获取Bitmap格式的屏幕截图: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 pu ...

  5. 位集合类BitSet

    位集合类中封装了有关一组二进制数据的操作. 我们先来看一下例8.6 BitSetApp.java. 例8.6 BitSetApp.java //import java.lang.*; import j ...

  6. PHP 清除HTML代码、空格、回车换行符的函数

    function DeleteHtml($str) { $str = trim($str); $str = strip_tags($str,""); $str = ereg_rep ...

  7. js节流防抖应用场景,以及在vue中节流防抖的具体实现

    故事背景: 项目有个需求是输入框在输入的时候进行搜索,展示下拉数据,但是没必要输入一个字都进行搜索,所以想到了在输入结束200毫秒后再进行搜索,从而引出来了 js的节流(throttle),防抖(de ...

  8. html table 上移下移

    js操作表格操方法,增加,修改,删除,一行记录 随机选择行 添加一行 删除选定行 上移选定行 下移选定行 按第一列排序 按数据和排序   <!DOCTYPE html PUBLIC " ...

  9. windows 中 Eclipse 打开当前文件所在文件夹

    默认情况下使用eclipse打开当前文件所在文件夹很麻烦,需要右键点击 Package Explorer 中的节点选择属性,然后复制路径,再打开资源管理器,然后再把路径粘贴进去.而MyEclipse一 ...

  10. Duilib教程-自动布局3-分隔条

    先看一个常用的图,如下: 左边是导航栏,右边是信息区. 中间可以自由拉伸. XML如下: <?xml version="1.0" encoding="utf-8&q ...