Spring如何使用注解的方式创建bean

作者:闻窗 时间:2022-01-29 03:45:49 

这篇文章主要介绍了Spring如何使用注解的方式创建bean,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

第一种使用配置类的方式

1、创建一个bean


package com.springbean;

public class Person {

private String name;
 private Integer age ;

public Person(String name, Integer age) {
   this.name = name;
   this.age = age;
 }

public void setName(String name) {
   this.name = name;
 }

public void setAge(Integer age) {
   this.age = age;
 }

public String getName() {
   return name;
 }

public Integer getAge() {
   return age;
 }

@Override
 public String toString() {
   return "Person{" +
       "name='" + name + '\'' +
       ", age=" + age +
       '}';
 }
}

2、创建配置类:


import com.springbean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PersonConfig {

@Bean//@Bean("myperson") 这是设置bean的名字
 public Person person(){ System.out.println("已经创建实例");
return new Person("张三",20); } }

3、测试


import com.spring.config.PersonConfig;
import com.springbean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class ApplicationTest {
 public static void main(String[] args) {

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
   Person bean = applicationContext.getBean(Person.class);
   System.out.println(bean);

//获取bean的类型,默认是方法名,需要修改就在配置类中@Bean里面加上名字

String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
for (String beanType : beanNamesForType){
 System.out.println(beanType);
}
}
}

和xml配置文件一样,默认的bean是单例的,如果需要改变为prototype,xml配置文件里是加上scope="prototype",这里PersonConfig配置类中需要加上注解@Scope("prototype")。

介绍一下bean的几种类型的作用域。

  • singleton:单实例(默认),ioc容器启动时就会创建对象放到ioc容器中,以后每次获取都是直接从ioc容器中获取,ioc容器可以简单理解为map

  • prototype:多实例(原型),ioc容器启动并不会去调用方法创建对象,而是每次我们获取对象的时候,才会调用方法去创建。

  • requst:同一次请求创建一个实例

  • session:同一个session创建一个实例

不加注解测试:


ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
   Person bean = applicationContext.getBean(Person.class);
   Person bean2 = applicationContext.getBean(Person.class);
   System.out.println(bean==bean2);//打印结果为true

加上注解@Scope("prototype")测试:


ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
   Person bean = applicationContext.getBean(Person.class);
   Person bean2 = applicationContext.getBean(Person.class);
   System.out.println(bean==bean2);
//打印结果为fale

我们也可以改变单例时ioc加载的时候就创建实例,只要在我们的PersonConfig配置类中加上@Lazy注解,使用懒加载。测试


public class ApplicationTest {
 public static void main(String[] args) {

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);
   /* Person bean = applicationContext.getBean(Person.class);
   Person bean2 = applicationContext.getBean(Person.class);
   System.out.println(bean==bean2);*/
/*
   String[] beanNamesForType = applicationContext.getBeanNamesForType(Person.class);
   for (String beanType : beanNamesForType){
     System.out.println(beanType);
   }*/
 }
}

这是时打印栏将不会打印出“已经创建实例”,就实现的单例情况下的懒加载。

第二种使用@import注解的方式

新建一个student类


public class Student {
}

在配置类PersonConfig上使用@Import注解,这里面可以传入一个数组,用大括号{}


@Configuration
@Import({Student.class})
public class PersonConfig {

测试:


public class DemoTest {

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PersonConfig.class);

@Test
 public void test(){

Student bean = applicationContext.getBean(Student.class);
   System.out.println(bean);
 }
}

打印结果:com.springbean.Student@2c34f934 ,注入成功

还可以在@Import中加入ImportSelector的实现类来实现bean的注入

创建Parent和Teacher类


public class Parent {
}

public class Teacher {
}

创建ImportSelector的实现类MyImportSelector,返回需要注入的bean,这里是全类名


public class myImportSelector implements ImportSelector{
 @Override
 public String[] selectImports(AnnotationMetadata annotationMetadata) {

return new String[]{"com.springbean.Parent","com.springbean.Teacher"};
 }
}

修改PersonConfig,这里传入实现类MyImportSelector


@Configuration
@Import({Student.class, myImportSelector.class})
public class PersonConfig {

测试:


Parent parent = applicationContext.getBean(Parent.class);
   Teacher teacher = applicationContext.getBean(Teacher.class);
   System.out.println(parent);
   System.out.println(teacher);

打印结果:

com.springbean.Parent@3b2cf7ab
com.springbean.Teacher@2aa5fe93

来源:https://www.cnblogs.com/tdyang/p/11867704.html

标签:spring,注解,创建,bean
0
投稿

猜你喜欢

  • synchronized背后的monitor锁实现详解

    2023-07-31 08:14:10
  • 关于@ApiImplicitParams、ApiImplicitParam的使用说明

    2023-11-09 10:49:34
  • C#设置Word文档背景的三种方法(纯色/渐变/图片背景)

    2022-07-08 01:33:09
  • 详解如何更改SpringBoot TomCat运行方式

    2021-11-17 02:48:01
  • C# 通过反射获取类型的字段值及给字段赋值的操作

    2021-11-14 13:30:23
  • 分析讲解SpringMVC注解配置如何实现

    2023-10-30 17:23:58
  • es(elasticsearch)整合SpringCloud(SpringBoot)搭建教程详解

    2023-12-06 07:34:14
  • Docker搭建前端Java的开发环境详解

    2023-05-21 04:23:25
  • JVM常量池的深入讲解

    2021-09-13 15:54:00
  • IDEA创建Java项目文件并运行教程解析

    2023-01-14 15:50:47
  • java中申请不定长度数组ArrayList的方法

    2023-02-24 17:37:37
  • Java实现批量向mysql写入数据的方法

    2023-11-05 20:51:21
  • IDEA远程连接HBase及其Java API实战详解

    2023-11-27 21:54:46
  • Spring Security认证提供程序示例详解

    2022-11-07 18:06:40
  • java清除html转义字符

    2023-03-18 12:20:10
  • Android实现登录注册功能

    2023-07-31 09:35:44
  • java之assert关键字用法案例详解

    2022-07-10 23:01:32
  • Dubbo实现分布式日志链路追踪

    2023-08-23 21:00:54
  • Linux下g++编译与使用静态库和动态库的方法

    2023-06-21 13:41:46
  • Spring Boot和Thymeleaf整合结合JPA实现分页效果(实例代码)

    2023-11-25 07:05:15
  • asp之家 软件编程 m.aspxhome.com