xml配置方式

首先我们创建一个实体类Person

public class Person {
private String name;
private Integer age;
private String nickName;
// 省略getter and setter ,tostring ,Constructor
1}

以往,我们在spring的配置文件中注册一个bean,采用以下写法;

<?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="person1" class="com.atguigu.bean.Person" >
<property name="age" value="24"></property>
<property name="name" value="zhangsan"></property>
</bean> </beans>

写个测试类

public class MainTest {

	@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Person bean = (Person) applicationContext.getBean("person1");
System.out.println(bean); } }

控制台打印结果:

Person [name=zhangsan, age=24, nickName=null]

nickName我们没有注入,所以为null,

可以看到我们已经成功在spring的ioc容器中注入了这个Person对象

采用javaConfig的方式注入

创建一个配置类,作用等同于spring的配置文件

@Configuration  //告诉Spring这是一个配置类
public class MainConfig { //给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
@Bean("person")
public Person person01(){
return new Person("lisi", 20);
} }

写一个测试类

public class MainTest {
@SuppressWarnings("resource")
public static void main(String[] args) {
// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
// Person bean = (Person) applicationContext.getBean("person");
// System.out.println(bean); ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
Person bean = applicationContext.getBean(Person.class); // 获取Bean,注意这里的容器是AnnotationConfigApplicationContext
System.out.println(bean); // 输出Bean //获取类型为Person.class的容器中所有的bean,
String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
// 遍历
for (String name : namesForType) {
System.out.println(name);
} } }

控制台输出结果,发现已经成功注入了

Person [name=lisi, age=20, nickName=null]
person

通过配置类的形式注入bean,那bean的名字默认为方法名的小写,即person01,如何手动指定bean的名字呢?

@Bean("person") // 指定bean的名字为person,如果不写为方法名的小写。
// 以下两种写法也是可以的
// @Bean(value ="person")
// @Bean(name = "person")

一、Spring之组件注册-@Configuration&@Bean给容器中注册组件的更多相关文章

  1. 2、组件注册-@Configuration&@Bean给容器中注册组件

    2.组件注册-@Configuration&@Bean给容器中注册组件 2.1 创建maven项目 spring-annotation pom.xml文件添加 spring-context 依 ...

  2. 【Spring注解开发】组件注册-使用@Configuration和@Bean给容器中注册组件

    写在前面 在之前的Spring版本中,我们只能通过写XML配置文件来定义我们的Bean,XML配置不仅繁琐,而且很容易出错,稍有不慎就会导致编写的应用程序各种报错,排查半天,发现是XML文件配置不对! ...

  3. 向Spring容器中注册组件的方法汇总小结

    1.通过xml定义 <bean class=""> <property name="" value=""></ ...

  4. spring 给容器中注册组件的几种方式

    1.@Bean 导入第三方的类或包的组件 2.包扫描+组件的标注注解(@ComponentScan: @Controller,@service,@Reponsitory,@Componet), 自己写 ...

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

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

  6. 【Spring注解驱动开发】在@Import注解中使用ImportBeanDefinitionRegistrar向容器中注册bean

    写在前面 在前面的文章中,我们学习了如何使用@Import注解向Spring容器中导入bean,可以使用@Import注解快速向容器中导入bean,小伙伴们可以参见<[Spring注解驱动开发] ...

  7. 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!

    写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...

  8. 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?

    写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ...

  9. Spring注解驱动开发04(给容器中注册组件的方式)

    给容器中注册组件的方式 1. 组件注解标注 + 包扫描(适用于自己写的类) //控制层组件 @Controller public class PersonController { } //业务逻辑层组 ...

随机推荐

  1. Backpressure & Elastic Scaling

    spark.streaming从不稳定到稳定状态,解决数据量接收数据时突然变大,使得无法及时处理数据,稳定性得到保证 开启方式: spark.streaming.backpressure.enable ...

  2. Java基础(八 前面补充)

    1.笔记 1.局部内部类 如果一个类是定义在一个方法内部的,那么这就是一个局部内部类. “局部”:只有当前所属的方法才能使用它,出了这个方法外面就不能用了. 定义格式: 修饰符 class 外部类名称 ...

  3. Codechef:Fibonacci Number/FN——求通项+二次剩余+bsgs

    题意 定义 $F_n$ 为 $$F_n = \left\{\begin{matrix}0, n=0\\ 1, n=1 \\F_{n-1} + F_{n-2}, n > 1\end{matrix} ...

  4. Django REST framework版本控制

    参考链接:https://www.cnblogs.com/liwenzhou/p/10269268.html 1.路由: #版本控制 re_path('^(?P<version>[v1|v ...

  5. Spark-源码分析02-Luanch Executor

    1.SparkContext.scala sparkcontext 在被new的时候,会执行class中的代码 其中有一个就是创建TaskScheduler 和 SchedulerBackend,而S ...

  6. Oracle 分区默认segment大小变化(64k—>8M)

    原文链接:http://www.cnblogs.com/wcwen1990/p/6656545.html _partition_large_extents和_index_partition_large ...

  7. hadoop大作业

    1.数据准备 2.把CSV添加到/bigdatacase/dataset中 3.检查前5行并删除第一行 4.将csv文件导入hadoop并检查前10行数据情况 5.数据文件导入hive 6.在Hive ...

  8. Java 面向对象(九)

    常用类之Random Random类位于 java.util 包中,主要用于生成伪随机数 Random类将种子数作为随机算法的起源数字,计算生成伪随机数,其与生成的随机数字的区间无关 创建Random ...

  9. javacpp, javacv: 封装了FFmpeg、OpenCV等计算机视觉编程人员常用库的接口

    jvavacpp:     一个java调用jni的库,支持安卓. javacv:      封装了FFmpeg.OpenCV等计算机视觉编程人员常用库的接口,可以通过其中的Utility类方便的在包 ...

  10. python在windows上创建虚拟环境

    cmd进入 安装virtualenvwrapper-win pip install virtualenvwrapper-win 创建名为pyve的虚拟环境 mkvirtualenv pyve 退出虚拟 ...