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:基 ...
随机推荐
- 批量索引以提高索引速度 -d --data-binary
index create update 第1.2行分别为:信息行.数据行,在索引中增加或更换文档delete 移除文档,只包含信息行 Bulk API | Elasticsearch Referenc ...
- 阿里云RDS上的一些概念性记录
刚接触RDS,只能对RDS上的一些特性做一些笔记,方便记忆,以下为RDS上的内容摘录,取自官方文档 1 数据备份 可使用命令行或图形界面进行逻辑数据备份.仅限通过 RDS 管理控制台 或 OPEN A ...
- win7安装laravel
使用Packagist 镜像 建立一个composer.json文件,内容如下: { "name": "laravel/laravel", "desc ...
- 《UNI|X环境高级编程》 源代码配置
代码下载地址:http://www.apuebook.com/ 下的第二版,里面有个readme文件: root@iZ23onhpqvwZ:~/ms/linux/apue/apue.2e# cat R ...
- Python之函数总结
一,函数的定义与调用 定义:def 关键词开头,空格之后接函数名称和圆括号(),最后还有一个":". def 是固定的,不能变,必须是连续的def三个字母,不能分开 def 函数名 ...
- 3.6中的range()
在python3中range()是这样的: >>> range(4) range(0, 4) #额,列表跑哪去了 在之前的python2中是这样的: >>> ran ...
- java中byte数组与int,long,short间的转换
http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * <l ...
- PAT 天梯赛 L1-037. A除以B 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-037 AC代码 #include <iostream> #include <cstdio&g ...
- Loadrunder脚本篇——webservice接口测试(一)
函数介绍 soap_request 函数执行一个SOAP请求 函数原型 int soap_request( const char *StepName, ExpectedResponse, URL, , ...
- iOS9 Search API 之 Spotlight
iOS9以后 有三种api提供搜搜方式 加强引导用户关注 我们的app及相关内容的方式 NSUserActivity Web Markup Core Spotlight 用法 前两种 实战操作性不够 ...