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
随机推荐
- JDK8集合的便捷操作
JDK8新特性,stream相关操作.把集合转换成stream,再对其进行相关操作,加上lambada表达式. demo: List<String> list = Arrays.asLis ...
- 谈谈对MVC、MVP和MVVM的理解
刚出来工作的时候维护过一个老系统,该系统是用微软的ASP(Active Server Pages 动态服务器页面)写的.每一个页面都是一个ASP文件,每一个一个ASP文件中又同时包含了HTML.CSS ...
- servlet里面拿到common.property的属性
---------------------common.property文件----------------------- kongxc_wx_dinghuo_orderSendMusic=http: ...
- checkbox 更新回显
if($row['name']==$_SESSION['name']){$checked="checked disabled";}else{$checked=" &quo ...
- 安装python各类工具包、IDE以及著名开源模块如kaldi等的简单总结
在学习一门语言或者使用一个著名的开源工具时,搭建环境是非常重要的一步,在环境搭建的过程中往往要踩很多坑.昨天一不小心把电脑操作系统整坏了,搞了个通宵算是搞定了,把win10系统重装回win7,但之前搭 ...
- MFC自绘框架窗口客户区
利用MFC开发用户界面往往需要需要根据要求进行界面美化,界面的美化包括很多内容,比如说界面各功能模块空间布局,控件位置选择,各功能模块区域的字体.背景颜色选择.添加位图,标题栏.菜单栏.状态栏等的重绘 ...
- 模式识别之Shape Context---利用Shape Context进行形状识别
什么是Shape Context Shape Context是一个用于形状识别的,非常经典的特征(一串便于计算机处理的数字)提取方法,它由Serge Belongie和Jitendra Malik ...
- C语言程序设计-同一天生日[综合应用]
[问题描述] 在一个有200人的大班级中,存在两个人生日相同的概率非常大,现给出每个学生的学号,出生月日,试找出所有生日相同的学生. [输入形式] 第一行为整数n,表示有n个学生,n<=200. ...
- 下载xftp,xshell进行与linux端的远程操作
在window下下载xftp5和xshell5 xshell主要是对远程的及其进行访问,在远程的情况下进行操作 xftp可以对远程的机器进行文件传输. 我安装这两个是单个的安装的. 进入官网 http ...
- linux连接mysql命令
连接MYSQL: 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL 找到mysql的安装目录,一般可以直接键入命令mysql -uroot -p,回车后提 ...