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
随机推荐
- C++ Scoket的升级版(多态的运用)
//Socket报文发送c++升级版 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #in ...
- 4、手把手教React Native实战之flexbox布局(伸缩属性)
###伸缩项目的属性 1.order 定义项目的排列顺序,数值越小,排列越靠前,默认值为0,语法为:order:整数值 2.flex-grow 定义伸缩项目的放大比例,默认值为0,即表示如果存在剩余空 ...
- SurvivalShooter学习笔记(二.玩家移动旋转)
该案例中:(PC端操作) 1.玩家移动输入控制通过虚拟轴Axis,旋转输入控制通过鼠标位置: 2.玩家始终面朝鼠标停留点,鼠标停留点通过摄像机朝地面的射线获取: 3.玩家待机移动状态切换通过Anima ...
- spring 项目升级到spring cloud记录 数据源配置
用的阿里的数据源 增加pom <dependency> <groupId>com.alibaba</groupId> <artifactId>drui ...
- centos6.5下安装samba服务器与配置
转自:http://www.centoscn.com/CentosServer/ftp/2014/1023/3989.html http://www.cnblogs.com/x_wukong/p/56 ...
- Delphi数据库处理
Delphi数据库处理 第一节 BDE.ADO.InterBase和dbExpress Delphi中处理数据库主要有两种方法,也就是BDE.ADO,从Delphi 6.0开始还加入了一种dbExpr ...
- poj 2923(状态压缩+背包)
比较巧妙的一道题目,拿到题目就想用暴力直接搜索,仔细分析了下发现复杂度达到了2^n*n! ,明显不行,于是只好往背包上想. 于是又想二分找次数判断可行的方法,但是发现复杂度10^8还是很悬... 然后 ...
- 170118、快速失败Vs安全失败(Java迭代器附示例)
简介: 当错误发生时,如果系统立即关闭,即是快速失败,系统不会继续运行.运行中发生错误,它会立即停止操作,错误也会立即暴露.而安全失败系统在错误发生时不会停止运行.它们隐蔽错误,继续运行,而不会暴露错 ...
- GIT界的神探--bisect
GIT界的神探--bisect 今天我们需要使用git命令中的bisect破获一起凶杀案,下面请看具体案件: 在一个git仓库中有a,b,c,d,e,f,g,h,i,j,k这几个提交,在k提交中发现软 ...
- Openstack块存储cinder安装配置
openstack service create --name cinderv2 \ --description "OpenStack Block Storage" volumev ...