Bean 注解(Annotation)配置(1)- 通过注解加载Bean
Spring 系列教程
- Spring 框架介绍
- Spring 框架模块
- Spring开发环境搭建(Eclipse)
- 创建一个简单的Spring应用
- Spring 控制反转容器(Inversion of Control – IOC)
- 理解依赖注入(DI – Dependency Injection)
- Bean XML 配置(1)- 通过XML配置加载Bean
- Bean XML 配置(2)- Bean作用域与生命周期回调方法配置
- Bean XML 配置(3)- 依赖注入配置
- Bean XML 配置(4)- 自动装配
- Bean 注解(Annotation)配置(1)- 通过注解加载Bean
- Bean 注解(Annotation)配置(2)- Bean作用域与生命周期回调方法配置
- Bean 注解(Annotation)配置(3)- 依赖注入配置
- Bean Java配置
- Spring 面向切面编程(AOP)
- Spring 事件(1)- 内置事件
- Spring 事件(2)- 自定义事件
Bean也可以通过Java注解的方式配置。
Java注解直接加在需要装配的Bean Java类上。
注解是类、方法或字段声明上的特殊标记。例如,常见的
@Override
就是一个注解,作用是告诉编译器这是一个被重写的方法。
注解配置对比XML配置
注解配置比XML配置更简洁,尤其是当有很多Bean时,可以省很多事。
XML注入会在注解注入之后执行,所以XML配置将覆盖注解配置。
1. 启用注解配置
默认情况下,Spring容器没有启用注解配置。需要在Bean的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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 打开组件扫描,启用注解配置 -->
<context:component-scan base-package="com.qikegu.demo"></context:component-scan>
<!-- ... -->
</beans>
base-package="com.qikegu.demo"
指定需要扫描的包路径。
2. 给Bean Java类添加@Component
注解
Spring容器扫描指定包路径下的所有类,每当找到1个@Component
注解,就会注册Bean,同时设置Bean ID。
默认Bean ID就是类名,但首字母小写。如果类名以连续几个大写字母开头,首字母不小写。(即QIKEGUService -> QIKEGUService)
package com.qikegu.demo;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Lazy;
@Component
@Lazy
public class App {
}
3. 通过Spring容器获取bean
与XML配置方式类似,使用getBean()
方法返回Bean实例。
示例:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
// 获取Bean实例
App app = context.getBean("app", App.class);
// App app = (App) context.getBean("app");
}
}
Bean 注解(Annotation)配置(1)- 通过注解加载Bean的更多相关文章
- spring注解第03课 按条件加载Bean @Conditional
package com.atguigu.config; import org.springframework.context.annotation.Bean; import org.springfra ...
- Bean 注解(Annotation)配置(3)- 依赖注入配置
Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...
- Bean 注解(Annotation)配置(2)- Bean作用域与生命周期回调方法配置
Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...
- Bean XML 配置(1)- 通过XML配置加载Bean
系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Contro ...
- spring加载bean流程解析
spring作为目前我们开发的基础框架,每天的开发工作基本和他形影不离,作为管理bean的最经典.优秀的框架,它的复杂程度往往令人望而却步.不过作为朝夕相处的框架,我们必须得明白一个问题就是sprin ...
- Spring多种加载Bean方式简析
1 定义bean的方式 常见的定义Bean的方式有: 通过xml的方式,例如: <bean id="dictionaryRelMap" class="java.ut ...
- spring Boot加载bean
1.SpringBoot中加载bean,可以使用注解@compenent直接加载到applicationContext容器中 2.在直接类@Configuration中,手动注册bean,如:
- IoC 之加载 Bean:总结
上文中我们将bean已经加载到了IOC容器中,接下来我们将把IOC加载Bean出来进行代码解析 备注:(有些解释是参考别个博客的相关解释 )一起探讨请加我QQ:1051980588 bean 的初始化 ...
- ligerui_ligerTree_003_配置url参数,加载“树”
配置url参数,加载“树”: 源码下载地址:http://download.csdn.net/detail/poiuy1991719/8571255 效果图:json.txt HTML代码: < ...
随机推荐
- PaperReading20200222
CanChen ggchen@mail.ustc.edu.cn VS-GAE Motivation: With the publication of NAS101, researchers can ...
- docker-构建建tomcat镜像并启动容器
1.下载一个tomcat8,解压好改名为tomcat并配置端口为80,删除webapps下的默认的应用,修改tomcat/bin目录下脚本的权限,chmod +x *.sh 2.路径一般放在/usr/ ...
- IP、TCP、DNS
负责传输的IP协议 按层次分,IP网际协议位于网络层.几乎所有使用网络的系统都会用到 IP 协议. “IP”和“IP地址不同”,“IP”是一种协议的名称.IP 协议的作用是把各种数据包传送给对方.而要 ...
- LeetCode刷题--基础知识篇--KMP算法
KMP算法 关于字符串匹配的算法,最知名的莫过于KMP算法了,尽管我们日常搬砖几乎不可能去亲手实现一个KMP算法,但作为一种算法学习的锻炼也是很好的,所以记录一下. KMP算法是根据三位作者(D.E. ...
- vue通过路由传值及在页面刷新后如何保存值
1.普通的路由跳转 方式一:通过routerLinkTo方式,转为a标签的跳转,to里面相当于a标签的href路径 如下: 方式二:通过this.$router.push方式: 如下: 2.带参数的路 ...
- Lesson 45 Of men and galaxies
In man's early days, competition with other creatures must have been critical. But this phase of our ...
- Jenkins + git + maven 安装
1.jenkins安装 sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo ...
- GetHub上很实用的几个Demo
手机号匹配的正则表达式:https://github.com/VincentSit/ChinaMobilePhoneNumberRegex/blob/master/README-CN.md FEBS- ...
- C++面试常见问题——13结构体与共用体的sizeof
结构体与共用体的sizeof 结构体的sizeof 结构体变量占用的内存空间大小通常是其基本类型的大小,但是由例外(字节对齐机制) struct S1{ char c[5]; int a; doubl ...
- 《动手学深度学习》系列笔记 —— 语言模型(n元语法、随机采样、连续采样)
目录 1. 语言模型 2. n元语法 3. 语言模型数据集 4. 时序数据的采样 4.1 随机采样 4.2 相邻采样 一段自然语言文本可以看作是一个离散时间序列,给定一个长度为\(T\)的词的序列\( ...