@Configuration和@bean给容器中注册组件

spring容器中注册组件的两种方式:

  • xml配置文件方式
  • 注解方式

1、xml配置文件方式

  • 编写bean类
  • 使用xml文件配置bean注入到spring容器
  • 通过ClassPathXmlApplicationContext类获取bean

1.1、编写bean类

package com.jcon.entity;

/**
 * @author Jcon
 * @version V1.0
 * @Description: (用户实体类)
 * @date 2019年01月28日
 */
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 +
                ‘}‘;
    }
}

1.2、使用xml文件配置bean注入到spring容器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="person" class="com.jcon.entity.Person">
        <property name="age" value="20"/>
        <property name="name" value="张三"/>
    </bean>

</beans>

1.3、通过ClassPathXmlApplicationContext类获取bean

    // 传统xml方式注入bean并从容器中获取
    @Test
    public void xmlGetBean(){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        Person person = (Person) applicationContext.getBean("person");
        System.out.println(person);
    }

2、注解方式

  • 编写bean类
  • 编写bean配置类,使用注解注入bean
  • 通过AnnotationConfigApplicationContext获取bean对象

2.1、编写bean类

package com.jcon.entity;

/**
 * @author Jcon
 * @version V1.0
 * @Description: (用户实体类)
 * @date 2019年01月28日
 */
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 +
                ‘}‘;
    }
}

2.2、编写bean配置类,使用注解注入bean

package com.jcon.entity;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Jcon
 * @version V1.0
 * @Description: (配置类)
 * @date 2019年01月28日
 */
@Configuration
public class Config {

    @Bean("person")
    public Person getPerson(){
        Person person = new Person();
        person.setAge(18);
        person.setName("李四");
        return person;
    }

}

2.3、通过AnnotationConfigApplicationContext获取bean对象

    // 注解方式注入bean并从容器中获取
    @Test
    public void annotationGetBean(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
        Person person = (Person) applicationContext.getBean("person"); // 对应bean名字为@bean方法的方法名,也可以使用@bean("name")指定bean名字
        System.out.println(person);
    }

总结

  • 使用注解类可以代替xml配置类
  • 注解方式bean名字默认是方法名,可以使用@bean("beanName")来指定bean名字

原文地址:https://www.cnblogs.com/Jcon/p/10332072.html

时间: 2024-08-29 02:35:15

@Configuration和@bean给容器中注册组件的相关文章

Spring——往容器中注册组件的方法总结

1.通过配置类的包扫描(@Configuration @ComponentScan)+给组件标注注解(@Component @Service @Controller...) 2.通过@Bean在配置类中加入组件 特点:使用第三方组件时可以不用更改第三方代码. 3.@Import 1)在配置类中使用@Import可以直接导入组件,容器会自动注册,id默认全类名 2)ImportSelect:实现这个接口,通过接口提供的selectImports方法返回并注册全类名数组中的类,再给容器中注册Impo

[翻译]Component Registration in Script System 在脚本系统中注册组件

Component Registration in Script System 在脚本系统中注册组件   To refer to our component from a script, the class, its properties and methods must first be registered in the script system. You may place the registration code in a file with the same name as the

vue中注册组件

注册组件,其实就是自定义标签(一堆标签堆在一起去现实某一个功能,可以类似成方法去理解) 注册组件,有2种 1:全局注册(全局注册的组件可以在任意的vue实例中使用) 2:局部注册(局部注册的组件只能在当前的vue实例中使用) 例如: 全局组件: 注意:全局组件可以在任意的vue实例中使用,这样就可以实现代码的复用的好处了. 局部组件: 顾名思义:只能在当前注册它的vue实例中使用它. 注意:对于组件的命名,W3C规范是字母小写且包含一个短横杠'-'. 原文地址:http://blog.51cto

给spring容器注册组件

/** * 给容器中注册组件: * 1).包扫描+组件标注注解(@Controller/@Service/@Repository/@Component)[自己写的类] * 2).@Bean[导入的第三方包里面的组件] * 3).@Import[快速给容器中导入一个组件] * 1).@Import(要导入到容器中的组件):容器中就会自动注册这个组件,id默认是全类名 * 2).ImportSelector:返回需要导入的组件的全类名数组: * 3).ImportBeanDefinitionRegi

Spring注解驱动第八讲--容器中bean的生命周期

bean的生命周期指的就是bean在容器中的: 创建-->初始化-->销毁; 以上的过程都是由容器来进行管理. 我们可以自定义初始化和销毁方法,的那个进行到当前bean的生命周期的时候,调用我们自己定义的初始化方法和销毁方法.那么自定义初始化和销毁方法有以下四种方式: 1,指定初始化和销毁方法: 在以往使用xml配置文件的时候可以在<bean>标签中加上"init-method"和"destory-method"属性来指定自定义的初始化和销毁

二、注册组件

2.1.注册 你通过创建ContainerBuilder对象来注册组件并且将组件暴露的服务告知builder. 组件可以通过反射来进行创建(通过注册一个特定的.NET类型或者开放的泛型(by registering a specific .NET type or open generic)):通过提供现成的实例(你创建的对象的一个实例):或者通过lambda表达式(通过一个匿名方法来执行对象的创建).你可以通过ContainerBuilder中一系列重载的Register()方法来设置创建组件的

3. Angular中的组件及服务

组件(component) 可以使用ng generate component xxx来创建组件相关文件html文件.样式表.ts文件.spec.ts单元测试文件 组件的命名默认要符合XxxComponent,除非关掉tslint中的component-class-suffix设置 selector的命名默认要符合app-xxx,除非在tslint中修改component-selector的规则内容 所有的组件都要在NgModule中注册声明 "component-selector":

IOC容器在web容器中初始化——(一)两种配置方式

参考文章http://blog.csdn.net/liuganggao/article/details/44083817,http://blog.csdn.net/u013185616/article/details/52186184. 最近在研究IOC容器在web容器中初始化的过程.阅读了源码,参照了很多文章,在这里记录一下. 使用的web容器为tomcat7.spring的jar包为4.3.7.RELEASE版本. 我们可以通过web.xml配置文件web容器中声明spring容器.有以下两

vue和react之间关于注册组件和组件间传值的区别

注册组件 Vue中:1.引入组件:2.在components中注册组件:3.使用组件; React中:1.引入组件:2.使用组件; 子父传值 Vue中: 父组件向子组件传值: 1.在父组件中绑定值:2.子组件通过在props中接收值:3.正常使用; 子组件向父组件传值 1.子组件通过this.$emit订阅:2.父组件通过v-on监听: React中: 父组件向子组件传值: 1.在父组件中直接写值:2.在子组件中通过this.props.接收值: 子组件向父组件传值: (方法一) 1.给子组件定