今天给大家介绍一下Spring中Bean注解的用法,后续的文章给大家介绍Sping其他注解用法,希望对大家日常工作能有所帮助!

1、首先创建一个maven项目引入spring依赖

<dependencies><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.9</version></dependency>
</dependencies>

2、新建一个person.java 实体类

package com.spring.bean;public class Person {private String name;private Integer age;private String address;public Person(String name, Integer age, String address) {this.name = name;this.age = age;this.address = address;}public Person() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age='" + age + '\'' +", address='" + address + '\'' +'}';}
}

3、新建配置类 TestBeanConfig.java

package com.spring.config;import com.spring.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class TestBeanConfig {/*@Bean作用是注册一个Bean,类型为返回值的类型,默认是使用方法名作为id,可以自己定义* value 可以自定义id,默认和方法名一致* */@Bean(value = "person1")public Person person() {return new Person("小王", 35, "北京");}
}

4、resources 创建配置文件

<?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 https://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="java"></context:component-scan><bean id="person" class="com.spring.bean.Person"><property name="name" value="小明"></property><property name="age" value="30"></property><property name="address" value="苏州"></property></bean>
</beans>

5、新建测试类TestBean.java 具体展示注解方式和配置方式的示例

package com.spring.test;import com.spring.bean.Person;
import com.spring.config.TestBeanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestBean {public static void main(String[] args) {//配置文件方式ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("person.xml");Person bean = (Person) applicationContext.getBean("person");System.out.println("配置方式:");System.out.println(bean);// 注解方式 AnnotationConfigApplicationContext 注解的方式获取spring容器AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(TestBeanConfig.class);Person annotationPerson = (Person) annotationContext.getBean("person1");System.out.println("注解方式:");System.out.println(annotationPerson);// 用来获取Spring容器中指定类型的所有JavaBean的名称String[] beanNamesForType = annotationContext.getBeanNamesForType(Person.class);for (String item : beanNamesForType) {System.out.println(item);}}}

6、运行效果:

IT技术分享社区

个人博客网站:https://programmerblog.xyz

文章推荐程序员效率:画流程图常用的工具程序员效率:整理常用的在线笔记软件远程办公:常用的远程协助软件,你都知道吗?51单片机程序下载、ISP及串口基础知识硬件:断路器、接触器、继电器基础知识

Spring系列(二):Bean注解用法介绍相关推荐

  1. Spring系列(十一):@Profile 注解用法介绍

    今天给大家分享Spring属性注入的注解@Profile 介绍,希望对大家能有所帮助! 一.@Profile 注解的作用 在Spring容器中如果存在同一类型的多个组件,可以使用@Profile注解标 ...

  2. Spring系列(十):@Autowired 和@Resource注解用法介绍

    今天给大家分享Spring属性注入的注解@Autowired 和@Resource介,希望对大家能有所帮助! 一.@Autowired 注解 1.1 介绍 @Autowired注解默认按照类型容器中找 ...

  3. Spring系列(七):@FactoryBean注解用法介绍

    今天给大家介绍@FactoryBean注解用法,希望对大家能有所帮助! 1.@FactoryBean注解介绍 FactoryBean是实现了FactoryBean<T>接口的Bean,可以 ...

  4. Spring系列(六):@Conditional注解用法介绍

    今天给大家介绍@Conditional懒加载注解用法,希望对大家能有所帮助! 1.@Conditional注解介绍 @Conditional是Spring4版本新提供的一种注解,它的作用是按照设定的条 ...

  5. Spring系列(五):@Lazy懒加载注解用法介绍

    今天给大家介绍@Lazy懒加载注解用法,希望对大家能有所帮助! 1.@Lazy 懒加载注解的概念 SpringIoC容器会在启动的时候实例化所有单实例 bean .如果我们想要实现 Spring 在启 ...

  6. Spring系列之bean的使用

    转载自 https://www.cnblogs.com/xiaoxi/p/5850095.html 一.Bean的定义 <bean id="userDao" class=&q ...

  7. Spring系列(三) Bean装配的高级技术

    profile 不同于maven的profile, spring的profile不需要重新打包, 同一个版本的包文件可以部署在不同环境的服务器上, 只需要激活对应的profile就可以切换到对应的环境 ...

  8. Spring @Configuration 和 @Bean 注解

    @Configuration 和 @Bean 注解 带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源.@Bean 注解告诉 Spri ...

  9. 详细讲解Spring中的@Bean注解

    点击关注公众号,实用技术文章及时了解 来源:blog.csdn.net/weixin_42140261/ article/details/104864333 随着SpringBoot的流行,我们现在更 ...

最新文章

  1. kafkatool 配置_kafka tools的使用
  2. 一起谈.NET技术,.NET4.0 之 Dynamic VS Reflection 效率
  3. SQL分页查询的介绍以及好处~~
  4. python 键盘输入_跟我一起学python | 探究07
  5. idea-导入其他项目模块的包爆红
  6. 【工程项目经验】之C语言或汇编语言宏展开
  7. set 和 vector的简单比较
  8. 微信hash ajax,基于vue hash模式微信分享#号的解决
  9. 一个前端UI资源共享网站
  10. python查看汉字的编码_python汉字编码
  11. 微信小程序从云开发到上线
  12. fu7推挽胆机音质_fu7电子管功放电路图大全(6N8P\6P3P\胆机功放电路\耦合电容器)...
  13. Mac 下 Netty 4.1 代码编译与运行
  14. 爬虫基础09B—scrapy爬虫
  15. 听课记录高中计算机,高中听课记录
  16. 点击 按钮 下载图片
  17. 小微企业内部用服务器应该怎么选择配置
  18. APISpace 的 星座配对API
  19. 【PhotoShop】用自己的照片做个好看的星空头像PS
  20. 礼品卡配合U盘,美国一公司遭受BadUSB真实攻击

热门文章

  1. Linux运维系统工程师系列---13
  2. H.264 Profile、Level、Encoder三张简图
  3. char **p, char a[16][8]; 问:p=a 是否会导致程序在以后出现问题?为什么?
  4. VS中lib和dll
  5. java消息顺序执行_Apache Flink:如何并行执行但保持消息顺序?
  6. Min_25 筛小结
  7. vue created 生命周期
  8. Spring积累总结
  9. mac搭建本地svn
  10. mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法