Spring框架学习之IOC(二)
Spring框架学习之IOC(二)
接着上一篇的内容,下面开始IOC基于注解装配相关的内容
在 classpath 中扫描组件
<context:component-scan>
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();
}
}
另外我们可以过滤掉一些类:
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框架学习之IOC(二)的更多相关文章
- Spring框架学习之IOC(一)
Spring框架学习之IOC(一) 先前粗浅地学过Spring框架,但当时忙于考试及后期实习未将其记录,于是趁着最近还有几天的空闲时间,将其稍微整理一下,以备后期查看. Spring相关知识 spri ...
- spring框架学习(一)——IOC/DI
什么是Spring框架: Spring是一个基于IOC和AOP的结构J2EE系统的框架: IOC 反转控制 是Spring的基础,Inversion Of Control,简单说就是创建对象由以前的程 ...
- Spring框架学习一
Spring框架学习,转自http://blog.csdn.net/lishuangzhe7047/article/details/20740209 Spring框架学习(一) 1.什么是Spring ...
- Spring框架学习1
AnonymouL 兴之所至,心之所安;尽其在我,顺其自然 新随笔 管理 Spring框架学习(一) 阅读目录 一. spring概述 核心容器: Spring 上下文: Spring AOP ...
- Spring框架学习总结(上)
目录 1.Spring的概述 2.Spring的入门(IOC) 3.Spring的工厂类 4.Spring的配置 5.Spring的属性注入 6.Spring的分模块开发的配置 @ 1.Spring的 ...
- Spring框架学习笔记(1)
Spring 框架学习笔记(1) 一.简介 Rod Johnson(spring之父) Spring是分层的Java SE/EE应用 full-stack(服务端的全栈)轻量级(跟EJB比)开源框架, ...
- Yii框架学习笔记(二)将html前端模板整合到框架中
选择Yii 2.0版本框架的7个理由 http://blog.chedushi.com/archives/8988 刚接触Yii谈一下对Yii框架的看法和感受 http://bbs.csdn.net/ ...
- spring框架学习(三)junit单元测试
spring框架学习(三)junit单元测试 单元测试不是头一次听说了,但只是听说从来没有用过.一个模块怎么测试呢,是不是得专门为一单元写一个测试程序,然后将测试单元代码拿过来测试? 我是这么想的.学 ...
- spring 框架学习网站
spring 框架学习网站 NO1 http://www.mkyong.com NO2 https://spring.io/docs/reference
随机推荐
- pom打包参数选择
pom.xml配置 <profiles> <profile> <id>dev</id> <properties> <token> ...
- [Django] 问题记录追踪表
关注的网站: https://simpleisbetterthancomplex.com/ Linux部署 1. Ubuntu下vsftpd安装部署 2. Utuntu下Django+Apache+W ...
- 第一百六十二节,jQuery入门介绍
jQuery入门 学习要点: 1.什么是 jQuery 2.学习 jQuery的条件 3.jQuery的版本 4.jQuery的功能和优势 5.其他 JavaScript库 6.是否兼容低版本 I ...
- 酷狗缓存文件kgtemp的加密方式
[转自:http://www.cnblogs.com/KMBlog/p/6877752.html] 首先对比了一下缓存文件和下载好的mp3文件,发现缓存文件多了1024个字节,而且对比了几个缓存文件, ...
- c# 将html添加进剪贴板(带格式)
调用: ClipboardHelper.CopyToClipboard("<h1>hello world</h1>", ""); /// ...
- smartJS 0.1 API 讲解 - FlowController
本篇介绍0.1版中最后一个特性,FlowController:同时也对第一版总结一下,因为近两年全部都是在搞前端,都是做一些js框架类的东西,也做了不少有意思的功能,做smartjs对我来说一个是对自 ...
- commit和rollback
COMMIT过程·已经在 SGA(系统全局区域)中产生了回滚段(Rollback segment)记录.·已经在 SGA 中产生了修改数据块.·已经在 SGA 中产生了上面两条的缓冲重做(redo). ...
- Django Admin后台管理模块的使用
Admin后台管理模块的使用 Django的管理员模块是Django的标准库django.contrib的一部分.这个包还包括其它一些实用的模块: django.contrib.auth django ...
- VS2010程序打包操作(结合图片详细讲解)
附视频教程:http://www.cnblogs.com/mengdesen/archive/2011/06/14/2080312.html 1. 在vs2010 选择“新建项目”----“其他项 ...
- 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集
[BZOJ1576][Usaco2009 Jan]安全路经Travel Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, ...