码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-01-28 23:04:59      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:用户   通过   两种   author   port   获取   rip   spring   public   

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名字

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

标签:用户   通过   两种   author   port   获取   rip   spring   public   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!