Spring初学之注解方式配置bean
直接看代码:
UserController.java
package spring.beans.annotation.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import spring.beans.annotation.service.UserService; @Controller
public class UserController { @Autowired
private UserService userService;
public void add() { System.out.println("UserController add...");
userService.add();
}
}
UserService.java
package spring.beans.annotation.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; import spring.beans.annotation.repository.UserRepository;
import spring.beans.annotation.repository.UserRepositoryImpl; @Service
public class UserService { @Autowired
@Qualifier("userRepositoryImpl")
private UserRepository userRepository;
public void add(){
System.out.println("UserService add...");
userRepository.add();
}
}
UserRepository.java
package spring.beans.annotation.repository; public interface UserRepository {
public void add();
}
UserRepositoryImpl.java
package spring.beans.annotation.repository; import org.springframework.stereotype.Repository; @Repository//("userRepository")
//这里设置的value="userRepository",也可以用@Qualifier("userRepositoryImpl")写在装配它的地方
public class UserRepositoryImpl implements UserRepository { @Override
public void add() {
System.out.println("UserRepositoryImpl add...");
} }
xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 扫描指定spring IOC容器的类 -->
<!--使用resource-pattern扫描特定的类 -->
<!--
<context:component-scan base-package="spring.beans.annotation"
resource-pattern="repository/*.class">
</context:component-scan>
--> <!-- 筛选扫描 -->
<context:component-scan base-package="spring.beans.annotation"
>
<!-- 扫描不包含指定类型(表达式)的类 -->
<!--
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
-->
<!-- 扫描包含指定类型(表达式)的类(context下设置use-default-filters="false")配合使用 -->
<!-- <context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
-->
<!-- 扫描除了该接口 或该接口实现类的类 -->
<!-- <context:exclude-filter type="assignable"
expression="spring.beans.annotation.repository.UserRepository"/>
-->
<!-- 只扫描包含该接口 或该接口实现类的类 (context下设置use-default-filters="false")配合使用-->
<!-- <context:include-filter type="assignable"
expression="spring.beans.annotation.repository.UserRepository"/>
-->
</context:component-scan> </beans>
Spring初学之注解方式配置bean的更多相关文章
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- spring之通过注解方式配置Bean(一)
(1)组件扫描:spring能够从classpath下自动扫描.侦测和实例化具有特定注解的组件. (2)特定组件包括: @Component:基本注解,标识一个受spring管理的组件: @Respo ...
- Spring使用ioc注解方式配置bean
context层 : 上下文环境/容器环境 applicationContext.xml 具体示例: 现在ioc容器中添加context层支持: 包括添加xmlns:context.xsi:schem ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- spring学习笔记 星球日two - 注解方式配置bean
注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...
- Spring框架学习(6)使用ioc注解方式配置bean
内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- Spring中基于注解方式管理bean
操作步骤 第一步:导入相关jar包 spring IoC的基本包 Spring支持注解的Jar包 第二步:创建Spring配置文件,ApplicationContext.xml 引入约束和开启注解扫描 ...
- spring笔记--通过注解(annotation)配置Bean
Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...
随机推荐
- [MongoDB]学习笔记--Linux 安装和运行MongoDB
背景知识 MongoDB 是用C++写的, 开源的, NoSQL(Not Only SQL)文档数据库. 特点:high performance(高性能), high availability(高可靠 ...
- SharePoint 离线安装
SharePoint 离线安装,主要是AppFrabic服务出错,可以使用下来命令: "X:\WindowsServerAppFabricSetup_x64.exe" /i Cac ...
- Viewpage实现左右无限滑动
实现逻辑参考:http://www.cnblogs.com/xinye/archive/2013/06/09/3129140.html 代码:如下 public class MainActivity ...
- NodeJS版本EasyDarwin开源流媒体服务器开发心得
title: Node版本EasyDarwin开发心得 date: 2018-03-27 22:46:15 tags: 年后着手Node版本EasyDarwin的开发工作,截止到今天2018年03月2 ...
- Java的四种引用,强弱软虚,用到的场景
众所周知,java中是JVM负责内存的分配和回收,这是它的优点(使用方便,程序不用再像使用c那样操心内存),但同时也是它的缺点(不够灵活).为了解决内存操作不灵活这个问题,可以采用软引用等方法. 在J ...
- SharePoint服务器端对象模型 之 序言
对于刚刚开始接触SharePoint的开发人员,即使之前有较为丰富的ASP.NET开发经验,在面对SharePoint时候可能也很难找到入手的方向.对于任何一种开发平台而言,学习开发的过程大致会包括: ...
- php cmd 不能利用$_COOKIE 的处理 通过文件来暂存字符串
路径 <?php define('CMDPATH', 'wD:\cmd\\'); echo CMDPATH; die(); broswer 路径无问题 w 读 用 <?php $wfile ...
- <2013 08 12> Andrew:C语言的一点心得
C语言的特点在于,这是少见的中级语言(介于机器汇编和高级语言之间),因此它极其紧密地与特定机器架构.编译器.操作系统.库等基本概念相连.在底层,人们可以少量的甚至不使用汇编,但是不能不使用C.它以一种 ...
- Python利用subprocess起进程
from multiprocessing import Process, Pool import time import subprocess def task(msg): print 'hello, ...
- ASP.NET,什么是MVC,MVC的简单介绍
什么是MVC模式 MVC(Model-View-Controller,模型—视图—控制器模式)用于表示一种软件架构模式.它把软件系统分为三个基本部分:模型(Model),视图(View)和控制器(Co ...