1、使用xml创建bean的方式


1、首先新建一个maven工程,添加如下依赖

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-context</artifactId>
  4. <version>5.0.5.RELEASE</version>
  5. </dependency>

2、其次新建一个Person对象

  1. package com.yefengyu.annotation.bean;
  2.  
  3. public class Person
  4. {
  5. private String name;
  6.  
  7. private Integer age;
  8.  
  9. public Person()
  10. {
  11. }
  12.  
  13. public Person(String name, Integer age)
  14. {
  15. this.name = name;
  16. this.age = age;
  17. }
  18.  
  19. public String getName()
  20. {
  21. return name;
  22. }
  23.  
  24. public void setName(String name)
  25. {
  26. this.name = name;
  27. }
  28.  
  29. public Integer getAge()
  30. {
  31. return age;
  32. }
  33.  
  34. public void setAge(Integer age)
  35. {
  36. this.age = age;
  37. }
  38.  
  39. @Override
  40. public String toString()
  41. {
  42. return "Person{" +
  43. "name='" + name + '\'' +
  44. ", age=" + age +
  45. '}';
  46. }
  47. }

3、编写spring配置文件

  1. <bean id="person" class="com.yefengyu.annotation.bean.Person">
  2. <property name="name" value="yefengyu"></property>
  3. <property name="age" value="28"></property>
  4. </bean>

4、测试

  1. public static void main(String[] args)
  2. {
  3. ApplicationContext ctx= new ClassPathXmlApplicationContext("spring.xml");
  4. Person person = (Person) ctx.getBean("person");
  5. System.out.println(person);
  6. }

2、使用注解创建bean的方式


1、不需要xml文件,但是需要一个可以替换xml配置文件的配置类,也就是上面第三步可以被删除,使用如下代码:

  1. package com.yefengyu.annotation.config;
  2.  
  3. import com.yefengyu.annotation.bean.Person;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6.  
  7. //配置类等于配置文件
  8. @Configuration//告诉spring这是一个配置类
  9. public class MainConfig
  10. {
  11. @Bean//给容器注册一个bean,类型为返回值类型,id默认为方法名称
  12. public Person person()
  13. {
  14. return new Person("yefengyu", 28);
  15. }
  16. }

2、测试

  1. public static void main(String[] args)
  2. {
  3. ApplicationContext ctx= new AnnotationConfigApplicationContext(MainConfig.class);
  4. Person person = (Person) ctx.getBean("person");
  5. System.out.println(person);
  6. }

3、注意点:

(1)ApplicationContext 使用AnnotationConfigApplicationContext来获取,并且参数为配置类而非配置文件。

(2)在MainConfig配置类中,使用Bean注解给容器注册了一个bean,bean的id为方法名称person,因此可以在测试main方法中使用 person 作为bean的id获取bean。

(3)除了使用bean的id来从容器中获取bean,还可以使用bean的类型来获取,因此下面的这句

  1. Person person = (Person) ctx.getBean("person");

可以改为如下代码,无需强转:

  1. Person person = ctx.getBean(Person.class);

(4)如何给bean起个名字?在上面的MainConfig类中,id默认为方法名称,如何另起一个名称呢?只需要在bean注解上面加一个值,表示使用我们指定的注解,而不使用默认的注解。

  1. @Bean("person")
  2. public Person getPerson()
  3. {
  4. return new Person("yefengyu", 28);
  5. }

上面的代码中,我们可以指定了bean的id为 person,而非默认的 getPerson。注意只要指定了bean的id,就不能使用默认的id。

(5)查看注册的bean的名称(ID)

  1. public static void main(String[] args)
  2. {
  3. ApplicationContext ctx= new AnnotationConfigApplicationContext(MainConfig.class);
  4. String[] names = ctx.getBeanDefinitionNames();
  5. for (String name : names)
  6. {
  7. System.out.println(name);
  8. }
  9. }

结果如下:

  1. org.springframework.context.annotation.internalConfigurationAnnotationProcessor
  2. org.springframework.context.annotation.internalAutowiredAnnotationProcessor
  3. org.springframework.context.annotation.internalRequiredAnnotationProcessor
  4. org.springframework.context.annotation.internalCommonAnnotationProcessor
  5. org.springframework.context.event.internalEventListenerProcessor
  6. org.springframework.context.event.internalEventListenerFactory
  7. mainConfig
  8. person

这里我们看到除了spring框架自身的bean之外,还有mainConfig,也就是注解配置实例,此外就是我们关注的person这个bean了。

spring注解开发:Configuration&Bean的更多相关文章

  1. spring注解开发:bean的作用域与懒加载

    1.bean的作用域 1.新建一个maven工程,添加如下依赖 <dependency> <groupId>org.springframework</groupId> ...

  2. spring注解开发:容器中注册组件方式

    1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring ...

  3. Spring注解开发系列Ⅵ --- AOP&事务

    注解开发 --- AOP AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,横向重复,纵向抽取.详细的AO ...

  4. 浅尝Spring注解开发_自定义注册组件、属性赋值、自动装配

    Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含自定义扫描组件.自定义导入组件.手动注册组件.自动注入方法和参数.使用Spring容器底层组件等 配置 @Confi ...

  5. 浅尝Spring注解开发_Bean生命周期及执行过程

    Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含Bean生命周期.自定义初始化方法.Debug BeanPostProcessor执行过程及在Spring底层中的应 ...

  6. 浅尝Spring注解开发_AOP原理及完整过程分析(源码)

    浅尝Spring注解开发_AOP原理及完整过程分析(源码) 浅尝Spring注解开发,基于Spring 4.3.12 分析AOP执行过程及源码,包含AOP注解使用.AOP原理.分析Annotation ...

  7. Spring注解开发_Spring容器创建概述

    浅尝Spring注解开发_Spring容器创建概述 浅尝Spring注解开发,基于Spring 4.3.12 概述Spring容器创建的过程,包括12个方法的执行 浅尝Spring注解开发_自定义注册 ...

  8. Java开发学习(十)----基于注解开发定义bean 已完成

    一.环境准备 先来准备下环境: 创建一个Maven项目 pom.xml添加Spring的依赖 <dependencies>    <dependency>        < ...

  9. Annotation(三)——Spring注解开发

    Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...

  10. spring注解开发中常用注解以及简单配置

    一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...

随机推荐

  1. Java arraylist重复使用问题

    arraylist同一个实例重复使用时,需要使用clear()及时清空,否则会在上次的结果后面添加项. List<Double> weightsList = new ArrayList&l ...

  2. Windows下anaconda安装opencv

    win+R打开cmd界面,输入conda create -n opencv python=3.6,创建名为opencv的虚拟空间,然后一路y,直到安装完成. activate opencv 然后输入  ...

  3. JS面向对象——原型模型

    以下通过一段示例代码,说明原型模型中的基本概念以及知识点. <!DOCTYPE html> <html> <head> <title>原型模型</ ...

  4. VPX板卡 基于XC7K325T的3U VPX FMC接口数据收发预处理平台

    一.板卡概述       标准VPX 3U板卡, 基于Xilinx公司的FPGAXC7K325T-2FFG900 芯片,pin_to_pin兼容FPGAXC7K410T-2FFG900 ,支持PCIe ...

  5. how to pass variable from shell script to sqlplus

    script sqlplus ${ORA_USR}/${ORA_PASS}@${ORA_DB} @${PARM}/TEST $new_usr $model_usr $new_pwd parm of s ...

  6. 在vscode中快速生成vue模板

    点击文件-->首选项-->用户代码片段-->输入vue,此时会打开vue.json文件,将下列代码复制进文件保存即可,新建一个vue文件,输入vue回车即可生成模板,$0表示生成模板 ...

  7. css 文字对齐

    // html <div>姓名</div> <div>手机号码</div> <div>账号</div> <div>密 ...

  8. hive的数据定义之创建数据库和表

    1.对数据库的操作 create database hive_db //创建数据库hive_db create table hive_db.test(字段内容及其格式省略) //在数据库hive_db ...

  9. GET和POST是HTTP请求的两种基本方法,区别是什么!?

    GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过request body传递参数. 你可能自己 ...

  10. Jmeter性能测试,使用ServerAgent监控服务端性能指标

    一.jmeter1.下载JMeter Plugins Manager.jar放到你的jmeter\lib\ext目录下2.启动jmeter,进入Plugins Manager找到perfmon安装这个 ...