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
0
投稿
猜你喜欢
- 字符串每隔4位加空格今天弄了个银行卡识别功能,回显的时候想要将银行卡号每四位加一个空格,这样核对卡号会方便很多,这里记录一下1.正则表达式实
- 本文实例讲述了java实现的五子棋游戏代码,分享给大家供大家参考,具体代码如下package gyb.exam;import java.aw
- 首先:因为工作需要,需要对接socket.io框架对接,所以目前只能使用netty-socketio。websocket是不支持对接sock
- 目录Map的computeIfAbsent使用场景和方法常规实现使用computeIfAbsent方法实现Map中computeIfAbse
- static修饰符是java里面非常常用的一个东西,用法也非常多。然而,在kotlin里竟然没有这个东西!那该如何替代呢?本文就总结了下ja
- 平时我们在开发过程中,代码出现bug时为了更好的在服务器日志中寻找问题根源,会在接口的首尾打印日志,看下参数和返回值是否有问题。但是手动的l
- 1.以ApplocationContext上下文单例模式装配bean为例,深入探讨bean的生命周期:(1).生命周期图:(2).具体事例:
- 简介本文主要讲解如何用java Selenium 控制鼠标在浏览器上的操作方法。主要列举的代码示例,无图显示。可以自己上代码执行操作看效果。
- 一、NIOjava.nio全称java non-blocking IO,是指jdk1.4 及以上版本里提供的新api(New IO) ,为所
- 构造方法以及参数:PageView可用于Widget的整屏滑动切换,如当代常用的短视频APP中的上下滑动切换的功能,也可用于横向页面的切换,
- 一、背景在上一篇文章中,我们使用Seata整合了SpringBoot,在这篇文章中我们使用Seata整合SpringCloud。同时有了上一
- 一、安装MongoDB4.0.3(××)1.1、官方安装文档https://docs.mongodb.com/manual/tutorial
- 一、背景在开发过程中,我们的软件会面对不同的运行环境,比如开发环境、测试环境、生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比
- 1. 异常1.1 try…catch异常处理try catch的异常处理的格式写法 :try{ &nbs
- ThreadLocal是什么?该类提供了线程局部 (thread-local) 变量。这些变量不同于它们的普通对应物,因为访问某个变量(通过
- 1. 什么是AOPAOP (Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期 * 实现在
- 操作符就是为了解决对Observable对象的变换的问题,操作符用于在Observable和最终的Subscriber之间修改Observa
- 什么是继承面向对象的特征:封装:不必要公开的数据成员和方法,使用private关键字进行修饰。意义:安全性。背景代码中创建的类, 主要是为了
- springboot获取项目目录路径springboot部署后获取项目的路径//获取跟目录(绝对路径)File path = new Fil
- 前言最近看插件库上少有的取色器大都是圆形的或者奇奇怪的的亚子,所以今天做两个矩形的颜色取色器提示:以下是本篇文章正文内容,下面案例可供参考一