Spring注解(一):@Configuration、@Bean给容器中注册组件

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: Spring是在进行web开发中必不可少的一个框架,而基于传统的xml文件配置bean的方式太过繁琐,降低了开发的效率。从Spring2.5以后注解开发的出现大大简化日常开发中繁琐的配置。接下来就通过实例分析Spring中各种注解的用法。

Spring是在进行web开发中必不可少的一个框架,而基于传统的xml文件配置bean的方式太过繁琐,降低了开发的效率。从Spring2.5以后注解开发的出现大大简化日常开发中繁琐的配置。接下来就通过实例分析Spring中各种注解的用法。


如果不采用注解开发,通常进行组件注册是首先新建一个实体类:


package com.xinyi.bean;
public class Person {
  private String name;
  private Integer age;
  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;
  }
  @Override
  public String toString() {
    return "Person [name=" + name + ", age=" + age + "]";
  }
  public Person(String name, Integer age) {
    super();
    this.name = name;
    this.age = age;
  }
  public Person() {
  }
}
复制代码


然后在resource文件夹下新建一个配置文件beans.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"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
     <bean id="Person" class="com.xinyi.bean.Person">
         <property name="name" value="新一"></property>
         <property name="age" value="18"></property>
     </bean> 
</beans>
复制代码


然后新建一个测试类:


package com.xinyi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xinyi.bean.Person;
public class MainTest {
  public static void main(String[] args) {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
    Person personbean = (Person) applicationContext.getBean("Person");
    System.out.println(personbean);
  }
}
复制代码


输出结果如下:


Person [name=新一, age=18]
复制代码


接下来通过采用注解的方式进行开发,首先介绍两个注解@Configuration和@Bean。


@Configuration


从Spring3.0,@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。


@Configuration注解的配置类有如下要求:


@Configuration不可以是匿名类;


@Configuration不可以是final类型;


  1. 嵌套的configuration必须是静态类。


@Bean


@Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。@Bean给容器中注入一个bean,类型为返回值的类型,默认方法名作为id。


通过注解开发的方式在spring容器中注册组件过程,首先声明一个实体类,同上。然后声明一个配置类:


package com.xinyi.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.xinyi.bean.Person;
//配置类等同于以前的配置文件
//@Configuration等同于告诉spring这是一个配置类
@Configuration
public class MyConfig {
  //@Bean给容器中注入一个bean,类型为返回值得类型,id默认方法名作为id
  //注入person的值
  //person222为自定义bean的名字
  @Bean("person222")
  public Person person111() {
    return new Person("xinyi、",19);
  }
}
复制代码


测试类测试:


package com.xinyi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.xinyi.bean.Person;
import com.xinyi.config.MyConfig;
public class MainTest {
  public static void main(String[] args) {
    //以前是返回配置文件的位置,这里是返回配置类的位置
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyConfig.class);
    Person person = applicationContext.getBean(Person.class);
    System.out.println(person);
   //返回bean类型
    String[] persontype = applicationContext.getBeanNamesForType(Person.class);
    for(String nameString :persontype) {
      System.out.println(nameString);
    }
  }
}
复制代码


输出结果如下:


Person [name=xinyi、, age=19]
person222
复制代码


以上就是如何通过采用@Configuration和@Bean注解的方式注册组件,与传统的基于配置文件开发来比较减少了繁琐的配置文件,灵活性更高。

目录
相关文章
|
6天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
12天前
|
Java 测试技术 开发者
Spring IoC容器通过依赖注入机制实现控制反转
【4月更文挑战第30天】Spring IoC容器通过依赖注入机制实现控制反转
22 0
|
1天前
|
JSON 前端开发 Java
【JAVA进阶篇教学】第七篇:Spring中常用注解
【JAVA进阶篇教学】第七篇:Spring中常用注解
|
4天前
|
JavaScript Java 开发者
Spring Boot中的@Lazy注解:概念及实战应用
【4月更文挑战第7天】在Spring Framework中,@Lazy注解是一个非常有用的特性,它允许开发者控制Spring容器的bean初始化时机。本文将详细介绍@Lazy注解的概念,并通过一个实际的例子展示如何在Spring Boot应用中使用它。
17 2
|
6天前
|
前端开发 Java
SpringBoot之自定义注解参数校验
SpringBoot之自定义注解参数校验
16 2
|
12天前
|
XML Java 程序员
什么是Spring的IoC容器?
【4月更文挑战第30天】什么是Spring的IoC容器?
20 0
|
12天前
|
Java Spring
springboot自带的@Scheduled注解开启定时任务
springboot自带的@Scheduled注解开启定时任务
|
1月前
|
缓存 Java Spring
Spring 框架中 Bean 的生命周期
Spring 框架中 Bean 的生命周期
38 1
|
25天前
|
Java 数据库连接 开发者
浅谈Spring的Bean生命周期
浅谈Spring的Bean生命周期
21 1
|
6月前
|
XML Java 数据格式
Spring之Bean的生命周期
Spring之Bean的生命周期
56 0