@Component: 基本注解, 标识了一个受 Spring 管理的组件
@Repository: 标识持久层组件
@Service: 标识服务层(业务层)组件
@Controller: 标识表现层组件

配置文件

<?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.0.xsd"> <!-- 配置自动扫描的包: 需要加入 aop 对应的 jar 包 -->
<!-- 扫描此包及此包下的子包 -->
<context:component-scan base-package="com.spring.annotation"></context:component-scan> </beans>

demo

package com.spring.annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller; @Controller
public class UserAction { /*默认情况下所有使用 @Authwired 注解的属性都需要被设置. 当 Spring 找不到匹配的 Bean 装配属性时,
会抛出异常, 若某一属性允许不被设置, 可以设置 @Authwired 注解的 required 属性为 false
默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作.
此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注
@Qualifiter 已指定注入 Bean 的名称*/ @Autowired(required=false)
@Qualifier("usreService")
private UsreService usreService; public void execute(){
System.out.println("添加用户");
usreService.add();
} } package com.spring.annotation; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UsreService { @Autowired
private UserRepository userDao; public void add(){
System.out.println("接收请求");
userDao.save();
} } package com.spring.annotation; import org.springframework.stereotype.Repository; @Repository
public class UserRepository { public void save(){
System.out.println("保存数据");
} }

测试

package com.spring.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml"); UserAction userAction = (UserAction) ctx.getBean("userAction");//默认Bean id为类名首字母小写
userAction.execute();
} }

Spring通过注解配置Bean的更多相关文章

  1. [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  2. Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等

    实验1:配置通过静态工厂方法创建的bean  [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory { private st ...

  3. Spring(十五):通过注解配置 Bean

    在ClassPath中扫描组件 1)组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件: 2)特定组件包含: --- @C ...

  4. IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置

    1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...

  5. Spring IOC机制之使用注解配置bean

    一. 通过注解配置bean 1.1       概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2       ...

  6. Spring框架入门之基于Java注解配置bean

    Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...

  7. Spring 注解配置Bean

    一.使用注解配置Bean 1.注解 在类定义.方法定义.成员变量定义前使用.其简化<bean>标签,功能同<bean>标签.格式为: @注解标记名. 2.组件扫描 Spring ...

  8. 通过注解配置Bean

    之前说的三种配置方式,都是使用XML配置,现在我们说说使用注解配置Bean. 这部分内容主要分为两个部分:使用注解配置Bean,使用注解配置Bean属性. 在classpath中扫描组件 组件扫描:S ...

  9. 14Spring通过注解配置Bean(2)

    下面将对13Spring通过注解配置Bean(1)的中Repository.Service.Controller通过注解方式来建立关联. <context:component-scan>元 ...

随机推荐

  1. MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

    Mybatis在与Spring集成的时候可以配置 MapperFactoryBean来生成Mapper接口的代理. 例如 <bean id="userMapper" clas ...

  2. rem初始化

    使用css实现rem适配,头部引入即可 adaptive.js !function(e, t) { function i() { o = , e.devicePixelRatioValue = o, ...

  3. python sockerserver tcp 文件下载 udp

    #tcp serverclass MyHandler(socketserver.BaseRequestHandler): def handle(self): # 通信循环 while True: tr ...

  4. laravel中不使用 remember_token时退出报错,如何解决?

    Route::get('auth/logout','Auth\AuthController@getLogout'); 这是laravel自带的退出功能只需要写这一条路由就行了,但是很可能爆出以下错误: ...

  5. Jmeter常用脚本开发之Java请求

    Java请求:就是用来对java项目的类进行压测 例子:将输入的两个参数通过IO存入文件 1.创建Java工程,工程目录如下 2.将Jmeter-lib下面的所有jar包导入java工程, 3.创建一 ...

  6. Linux locales

    一.简介   二.语法   三.实例 aptitude install locales dpkg-reconfigure locales ; vi /etc/default/locale more / ...

  7. PAT 1056 组合数的和(15)(代码)

    1056 组合数的和(15 分) 给定 N 个非 0 的个位数字,用其中任意 2 个数字都可以组合成 1 个 2 位的数字.要求所有可能组合出来的 2 位数字的和.例如给定 2.5.8,则可以组合出: ...

  8. kvm介绍 转载

    KVM 介绍(1):简介及安装 学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I ...

  9. 洛谷4054 [JSOI2009]计数问题

    原题链接 二维树状数组模板题. 对每一种颜色开一棵二维树状数组统计即可. #include<cstdio> using namespace std; const int N = 310; ...

  10. ZBlog你选择PHP还是ASP?

    最近趁着空闲玩了一下zblog,对于很多第一次接触zblog的博主大多都会问zblog是PHP好还是ASP好?我们应该如何选择?其实,对于这个问题我也不是很懂,我个人比较倾向于PHP.今天我就整理一下 ...