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. linux 启动jar命令

    进入ECS目录jar 存放目录: 1.执行命令: linux:启动jar nohup java -jar qualityshop-api.jar>log.file 2>&1 &am ...

  2. 【vijos】1543 极值问题(数论+fib数)

    https://vijos.org/p/1543 好神奇的一题.. 首先我竟然忘记n可以求根求出来,sad. 然后我打了表也发现n和m是fib数.. 严格证明(鬼知道为什么这样就能对啊,能代换怎么就能 ...

  3. java项目学习

    GitHub地址:https://github.com/zhanglei-workspace/shopping-management-system

  4. 【C语言天天练(二)】预处理

    引言: 学C语言之初.一提到预处理,脑子里想到的就是#define的宏定义以及#include包括的头文件.后来随着对C的深入学习发现.预处理不止这些.比方条件编译.提前定义的宏等等.以下对此进行总结 ...

  5. DBUtils结果集处理

    1.BeanHandler查询 package jdbc; import java.sql.Connection; import java.sql.SQLException; import org.a ...

  6. 《转》Win7 IIS7.5 安装

    一.进入Win7的 控制面板,选择左侧的 打开或关闭Windows功能. 二.现在出现了安装Windows功能的选项菜单,注意选择的项目,我们需要手动选择需要的功能,下面这张图片把需要安装的服务都已经 ...

  7. 【BZOJ4259】残缺的字符串 FFT

    [BZOJ4259]残缺的字符串 Description 很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n.可当你现在再次碰到这两个串时, ...

  8. quartz定时任务配置

    参考:http://www.cnblogs.com/kay/archive/2007/11/02/947372.html Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quar ...

  9. [LintCode] 二叉树的后序遍历

    The recursive solution is trivial and I omit it here. Iterative Solution using Stack (O(n) time and  ...

  10. https://blog.newrelic.com/2014/05/02/25-php-developers-follow-online/

    w https://blog.newrelic.com/2014/05/02/25-php-developers-follow-online/ 1. Rob Allen. Zend Framework ...