Spring注解@Configuration与@Bean注册组件的使用详解
作者:爪哇斗罗 发布时间:2022-09-13 01:52:56
标签:Spring,@Configuration,@Bean
原始Spring开发
Person.java
准备Person.java类:
package com.jektong.spring;
public class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
pom.xml
在pom文件导入Spring基本依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jektong</groupId>
<artifactId>maven01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>maven01</name>
<description>maven01</description>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
</dependencies>
</project>
bean.xml
在没有使用Spring注解开发之前,我们通常会通过一个xml配置文件(bean.xml)去将我们需要使用的对象通过Bean的方式去注入到Spring容器中。
下面就是将Person作为对象注入Spring容器中:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.jektong.spring.Person">
<property name="name" value="zs"></property>
<property name="age" value="18"></property>
</bean>
</beans>
PersonTest.java
使用一个PersonTest.java测试类测试:
package com.jektong.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PersonTest {
public static void main(String[] args) {
// 加载配置文件,此文件放在类路径下。
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
// 获取bean.xml文件中注入的Person对象,并输出。
Person bean = (Person) applicationContext.getBean("person");
System.out.println(bean);
}
}
输出结果如下:
注解Spring开发
舍弃上面的bean.xml文件,通过注解的方式将xml文件转换成配置类,建立PersonConfig配置类:
package com.jektong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.jektong.spring.Person;
@Configuration // 告诉spring这是配置类相当于配置文件bean.xml
public class PersonConfig {
// 这是注入到spring容器的对象ID
// 括号内指定唯一ID,不指定则是默认以方法名为唯一ID,相当于:<bean>标签中的ID值。
@Bean("person01")
public Person person() {
return new Person("李四",21);
}
}
测试使用AnnotationConfigApplicationContext类读取注解:
package com.jektong.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.jektong.config.PersonConfig;
public class PersonTest {
public static void main(String[] args) {
ApplicationContext ac = new AnnotationConfigApplicationContext(PersonConfig.class);
Person bean = ac.getBean(Person.class);
System.out.println(bean);
// 查看BEAN的id
String[] beanDefinitionNames = ac.getBeanNamesForType(Person.class);
for (int i = 0; i < beanDefinitionNames.length; i++) {
System.out.println("beanid为:"+ beanDefinitionNames[i]);
}
}
}
输出如下:
@Configuration与@Bean作用总结
@Configuration
相当于spring中的XML配置文件,将此XML文件替代成配置类,声明在类上。
@Bean
相当于XML文件中所配置的各个Bean对象,现声明在方法上,默认以方法名作为注入的Bean的id。
来源:https://blog.csdn.net/qq_41857955/article/details/125213137


猜你喜欢
- 最近正式入坑Flutter,首先从环境搭建开始,看了网上好多关于Windows环境搭建的资料,基本都是按官方文档写的,看完的感受是,还不如直
- 如果你想知道java annotation是什么?你可以先看看:“http://www.infoq.com/articles/Annotat
- 引言在性能测试过程中,验证HTTP code和响应业务code码是比较基础的,但是在一些业务中,这些参数并不能保证接口正常响应了,很可能返回
- 前言从windows窗口的概念开始,通过对比去理解Android窗口体系,本文没有深入源码,重在理解概念代码都是抄来抄去,概念也是互相借鉴
- mapper-locations的作用说明1、mapper-locationsmapper-locations是一个定义mapper接口位置
- 本文以一个非常简单的实例讲述了Winform实现抓取web页面内容的方法,代码简洁易懂,非常实用!分享给大家供大家参考。具体实现代码如下:W
- Spring是什么?我们通常所说的 Spring 指的是 Spring Framework(Spring 框架),它是⼀个开源框架,有着活跃
- vs2005中总是保留最近打开的项目和文件的记录,甚至是以删除的它也不删,-_-!下面介绍几种删除的方法:第一种:建立一个bat文件,以后双
- 我就废话不多说了,大家还是直接看代码吧~<resultMap id="ParentMap" type="
- 注解注解定义Java 注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。Java 语言中的类、方法、变
- 前台form 表单:设置method=post,enctype=multipart/form-data。struts2在原有的上传解析器继承
- 本文实例讲述了C#通过指针实现快速拷贝的方法。分享给大家供大家参考。具体实现方法如下:// fastcopy.cs// 编译时使用:/uns
- 在Android开发过程中,有时会需要一些消息提示,大多数情况可以用dialog来做,但有些消息不需要用户去点击取消并且不能对用户体验产生影
- 1.springBoot的依赖确定项目中包含可以注解的依赖<dependency> <group
- RecyclerView 滑动时的优化处理,在滑动时停止加载图片,在滑动停止时开始加载图片,这里用了Glide.pause 和Glide.r
- 本文实例讲述了Java自定义注解用法。分享给大家供大家参考,具体如下:一 自定义注解语法[public] @interface Annota
- 问题?在很多公司(如阿里、华为等)的编程规范中,非常明确地禁止使用Executors快捷创建线程池,为什么呢?这里从源码讲起,介绍使用Exe
- 1、自定义消息转换器MessageConverter在WebMvcAutoConfiguration类中有一个方法configureMess
- 附GitHub源码:WebViewExplore先看图:在WebView页面长按时会弹出一个复制框,但如果里面的item不是我们想要的或者想
- 1.简介建议阅读本文最好对Dokcer有一些了解首先我们先了解一下Docker是什么Docker 属于 Linux 容器的一种封装,提供简单