1、使用xml创建bean的方式


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

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>

2、其次新建一个Person对象

package com.yefengyu.annotation.bean;

public class Person
{
private String name; private Integer age; public Person()
{
} public Person(String name, Integer age)
{
this.name = name;
this.age = 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 +
'}';
}
}

3、编写spring配置文件

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

4、测试

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

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


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

package com.yefengyu.annotation.config;

import com.yefengyu.annotation.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; //配置类等于配置文件
@Configuration//告诉spring这是一个配置类
public class MainConfig
{
@Bean//给容器注册一个bean,类型为返回值类型,id默认为方法名称
public Person person()
{
return new Person("yefengyu", 28);
}
}

2、测试

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

3、注意点:

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

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

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

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

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

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

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

@Bean("person")
public Person getPerson()
{
return new Person("yefengyu", 28);
}

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

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

public static void main(String[] args)
{
ApplicationContext ctx= new AnnotationConfigApplicationContext(MainConfig.class);
String[] names = ctx.getBeanDefinitionNames();
for (String name : names)
{
System.out.println(name);
}
}

结果如下:

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
mainConfig
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. C. Trailing Loves (or L'oeufs?) (质因数分解)

    C. Trailing Loves (or L'oeufs?) 题目传送门 题意: 求n!在b进制下末尾有多少个0? 思路: 类比与5!在10进制下末尾0的个数是看2和5的个数,那么 原题就是看b进行 ...

  2. 67.Task Scheduler(任务规划)

    Level: Medium 题目描述: Given a char array representing tasks CPU need to do. It contains capital letter ...

  3. CSS 实现水平垂直居中

    使用绝对定位 在已经知道子元素的宽高的时候,子元素设置成绝对定位,top,left,right,bottom=0, margin = auto .wrap{ position: relative; w ...

  4. YARN的伪分布式安装

    前提:安装完HDFS以后 1.修改mapred-site.xml 这个文件初始时是没有的,有的是模板文件,mapred-site.xml.template 所以需要拷贝一份,并重命名为mapred-s ...

  5. 关于手机端在同一个Grid中使用不同的布局展现即Layout的使用

    标题可能说的不是很清楚,我举个栗子好了,现在你正在写手机端的一个审批模块,这个模块要求能够展示所有待审批的信息 比如出差申请,请假申请,加班申请,以及报销申请 那么我的思路有两个 1:建立一个Tab页 ...

  6. shell截取小数点前后的子串

  7. USB接口外壳地和信号地间的处理

    USB外壳地和信号地之间串接1M电阻,并且还接一个0.01uf的电容到信号地,能否将一下这样处理的原理和目的: 1.将影响外壳的噪音滤除,不影响信号地: 2.迫使板子上电流是流入内部的信号地,而不是流 ...

  8. HDU 5634 线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5634 题意:给定一个长度为n的序列,有m次操作.操作有3种: 1 l,r :区间[l,r]的值变成ph ...

  9. demo board boot mode

    demo扩展板 QSPI0_IO0_MIO2--A13--PS-MIO2 QSPI0_IO0_MIO3--A14--PS-MIO3 QSPI0_IO0_MIO4--B11--PS-MIO4 QSPI0 ...

  10. Git最全总结

    一个小时学会Git   目录 一.版本控制概要 工作区 暂存区 本地仓库 远程仓库 1.1.什么是版本控制 1.2.常用术语 1.3.常见的版本控制器 1.4.版本控制分类 1.4.1.本地版本控制 ...