直接看代码:

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的更多相关文章

  1. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

  2. spring之通过注解方式配置Bean(一)

    (1)组件扫描:spring能够从classpath下自动扫描.侦测和实例化具有特定注解的组件. (2)特定组件包括: @Component:基本注解,标识一个受spring管理的组件: @Respo ...

  3. Spring使用ioc注解方式配置bean

    context层 : 上下文环境/容器环境 applicationContext.xml 具体示例: 现在ioc容器中添加context层支持: 包括添加xmlns:context.xsi:schem ...

  4. 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

    Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ cl ...

  5. spring学习笔记 星球日two - 注解方式配置bean

    注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...

  6. Spring框架学习(6)使用ioc注解方式配置bean

    内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...

  7. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

  8. Spring中基于注解方式管理bean

    操作步骤 第一步:导入相关jar包 spring IoC的基本包 Spring支持注解的Jar包 第二步:创建Spring配置文件,ApplicationContext.xml 引入约束和开启注解扫描 ...

  9. spring笔记--通过注解(annotation)配置Bean

    Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...

随机推荐

  1. 【python】-- Django 分页 、cookie、Session、CSRF

    Django  分页 .cookie.Session.CSRF 一.分页 分页功能在每个网站都是必要的,下面主要介绍两种分页方式: 1.Django内置分页 from django.shortcuts ...

  2. 接口测试工具 — jmeter(基本使用)

    1.打开jemeter(bin目录下jemter.bat) 2.基本操作

  3. issubclass/type/isinstance、函数和方法、反射、callable、特殊成员补充

    一.issubclass/type/isinstance(***) 1.issubclass(参数1, 参数2):检查第一个参数是否是第二个参数的 子子孙孙类,如下示例: class Base(obj ...

  4. bzoj3007 解救小云公主

    3007: 解救小云公主 Time Limit: 5 Sec  Memory Limit: 512 MB Submit: 159  Solved: 71 [id=3007" style=&q ...

  5. Ubuntu16.04安装Chrome浏览器及解决root不能打开的问题

    1. 安装桌面(emmm,不知道是否只执行第二个命令就行) # apt-get install gonme# apt-get install ubuntu-desktop2. 安装Chrome浏览器 ...

  6. 深度学习:Keras入门(一)之基础篇(转)

    转自http://www.cnblogs.com/lc1217/p/7132364.html 1.关于Keras 1)简介 Keras是由纯python编写的基于theano/tensorflow的深 ...

  7. SpringBoot注解大全*(转发:http://www.cnblogs.com/ldy-blogs/p/8550406.html)

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  8. Ubuntu安装配置samba

    一. samba的安装: sudo apt-get insall sambasudo apt-get install smbfs 二. 创建共享目录: mkdir /home/chars/shares ...

  9. loadrunder之脚本篇——action分类

    Action分类 l . Vuser_init 2. Vuser_end 3.  Action 在lr中用户的初始化操作应该存放在Vuser_init中.用户的结束操作存放在Vuser_end中.因为 ...

  10. mysql ERROR 1045 (28000): Access denied (using password: YES)

    mysql 安装完成后 mysql -u root -p #让输入密码直接回车就能登录 设置mysql的root用户初始密码: mysqladmin -u root password 'root' 解 ...