为了账号安全,请及时绑定邮箱和手机立即绑定

001Spring注解@Configuration和@Bean

标签:
Spring

@Configuration和@Bean

1、简介

我们要想给Spring注入自己的bean,我们可能需要写配置文件,然后指定bean标签,还可能利用property给属性赋值等操作。总之离不开配置文件。

2、项目结构

webp

image.png

3、配置文件的方式

3.1、实体类

package com.chentongwei.spring.annotation.configuration.entity;/**
 * @Description:
 * @author TongWei.Chen 2018-06-29 14:27:43
 * @Project spring-annotation
 */public class User {    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;
    }    public User() {
    }    public User(String name, Integer age) {        this.name = name;        this.age = age;
    }    @Override
    public String toString() {        return "User{" +                "name='" + name + '\'' +                ", age=" + age +                '}';
    }
}

3.2、配置文件

<?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">

    <beans>
        <bean id="user" class="com.chentongwei.spring.annotation.configuration.entity.User">
            <property name="age" value="12" />
            <property name="name" value="张三" />
        </bean>
    </beans></beans>

3.3、测试类

package com.chentongwei.spring.annotation.configuration;import com.chentongwei.spring.annotation.configuration.entity.User;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/**
 * @author TongWei.Chen 2018-06-29 14:29:59
 * @Description:
 * @Project spring-annotation
 */public class MainXmlTest {    private static final String CONFIG = "classpath:configuration/beans.xml";    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(CONFIG);
        User user = applicationContext.getBean("user", User.class);
        System.out.println(user);
    }

}

3.4、输出结果

User{name='张三', age=12}

PS:

不做过多解释,因为这是我们大家最为熟悉的一种方式,xml配置文件的方式。

4、注解的方式

4.1、简介

我们完全可以利用@Configuration@Bean这两个注解来搞定上面的配置文件。

4.2、实体类

同上述配置文件的【User】

4.3、主配置类

package com.chentongwei.spring.annotation.configuration.config;import com.chentongwei.spring.annotation.configuration.entity.User;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/**
 * @Description: 配置类(@Configuration),等同于spring配置文件
 *
 * @author TongWei.Chen 2018-06-29 14:33:15
 * @Project spring-annotation
 */@Configurationpublic class MainConfig {    /**
     * @Bean: 给容器中注册一个bean,bean的类型为返回值类型,id默认为方法名
     * 类似于配置文件的
     * <bean id="user" class="com.chentongwei.spring.annotation.configuration.entity.User" />
     *
     * @return
     */
    @Bean
    public User user() {        return new User("李四", 20);
    }

}

PS:

1、@Configuration:告诉Spring,被@Configuration注解修饰的类都是配置类,等同于你写一份spring的配置文件。

2、@Bean:告诉Spring我这是一个等待被注册的bean,相当于配置文件的<bean></bean>标签。

2.1、bean的类型就是方法的返回值类型。

2.2、bean的id默认就是方法名。

2.3、可以通过@Bean()的value属性指定bean的id,比如@Bean(“user01”)

3、@Bean只能在被@Configuration所修饰的类中才会生效。就好比你只有在spring配置文件里写<bean>标签才会管用,在普通的xml文件里写不管用。

4.4、测试类

package com.chentongwei.spring.annotation.configuration;import com.chentongwei.spring.annotation.configuration.config.MainConfig;import com.chentongwei.spring.annotation.configuration.entity.User;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;/**
 * @author TongWei.Chen 2018-06-29 14:36:50
 * @Description:
 * @Project spring-annotation
 */public class MainAnonationTest {    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);

        User user = applicationContext.getBean("user01", User.class);
        System.out.println(user);
    }
}



作者:编程界的小学生
链接:https://www.jianshu.com/p/e9391032bb3b


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
228
获赞与收藏
996

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消