Spring注解开发--@Bean注解介绍

@Bean注解的使用:

在以往我们利用springIOC容器获取对象,都是使用配置文件,通过<bean>标签模式来获取的。先看一下以往的模式:

一、以往配置文件的方式:

1.在pom.xml中将spring的依赖导入(mvn的依赖库:https://mvnrepository.com/)
找到 spring context 这个依赖包,我用的是4.3.18版本

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.18.RELEASE</version>
    </dependency>

2.创建一个实体类Person (set/get/toString/构造)

package cn.dxyzz.bean;

/**
 * @author Mr.dong
 * @creat 2019-01-28 10:04
 * Person实体类
 */
public class Person {
    private String name;//姓名
    private Integer age;//年龄

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Person(){

    }

    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{" +
                "mame='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3.在src/main./resources/目录下创建applicationContext.xml配置文件,在配置文件中配置bean

<!--id:自定义的名称,用于获取对象-->
<!-- class:需要Spring来创建哪个类的对象(写完整的包名) -->
<!--name:指定到某一个属性,value:给属性赋的值-->
<bean id="person" class="cn.dxyzz.bean.Person">
	<property name="age" value="18"></property>
	<property name="name" value="zhangsan"></property>
</bean>	
4.写一个测试类cn.dxyzz.test.Test:
package cn.dxyzz.test;

import cn.dxyzz.bean.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @author Mr.dong
 * @creat 2019-01-28 10:06
 * 测试
 */
public class Test {
    public static void main(String[] args) {
        //加载配置文件,获取Spring容器  参数:指定到配置文件的名称
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 从Spring容器中获取对象,getBean()方法的参数是配置文件中的bean的 id
        Person person = (Person) applicationContext.getBean("person");
        //输出
        System.out.println(person);
    }
}

4.输出的结果:(已获取到对象)
在这里插入图片描述

二、用注解@Bean的方式来获取对象

1.导入依赖(跟上面的依赖是一样的)

2.新建一个配置类:(cn.dxyzz.config.Config)

package cn.dxyzz.cn.dxyzz.config;

import cn.dxyzz.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @author Mr.dong
 * @creat 2019-01-28 10:44
 * 配置类
 */
@Configuration //@Configuration注解:告诉spring容器这是一个配置类
public class Config {
    @Bean //@Bean注解:给容器注册一个Bean,类型为返回值的类,id默认是用方法名作为id
    public Person person(){
        return new Person("zhangsan",18);//给name属性,age属性赋值
    }
}

3.再新建一个测试类cn.dxyzz.test.ConfigTest:

import cn.dxyzz.bean.Person;
import cn.dxyzz.cn.dxyzz.config.Config;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
 * @author Mr.dong
 * @creat 2019-01-28 10:49
 * 测试类ConfigTest
 */
public class ConfigTest {
    public static void main(String[] args) {
        //获取spring容器,加载配置类
        AnnotationConfigApplicationContext aa = new AnnotationConfigApplicationContext(Config.class);
        //通过类名获取对象
        Person person = aa.getBean(Person.class);
        //Person person = aa.getBean("person");这是通过id获取对象
        //result是一样的
        //当然里面还有很多方法,这里就不一一介绍啦,你可以自己研究研究
        //输出
        System.out.println(person);
    }
}

4.输出结果:
在这里插入图片描述

5.当然@Bean是可以自定义配置id的名称:

//只需要 @Bean(value = "这里写自定义名称")
//这样就可以将这个bean的id值,换成自定义的啦。
/*将Config类变成这样*/
@Configuration //@Configuration注解:告诉spring容器这是一个配置类
public class Config {
    @Bean(value = "aa") //@Bean注解:给容器注册一个Bean,类型为返回值的类,id默认是用方法名作为id
    public Person person(){
        return new Person("zhangsan",18);//给两个属性赋值
    }
}
-----------------------------------------------------------
//测试
public class ConfigTest {
    public static void main(String[] args) {
        //获取spring容器,加载配置类
        AnnotationConfigApplicationContext aa = new AnnotationConfigApplicationContext(Config.class);
        //通过类名获取对象
        Object person = aa.getBean("aa");
        System.out.println(person);
    }
}
//结果是一样的!
//Person{mame='zhangsan', age=18}

好了,@Bean注解介绍就到这里,使用起来是不是比配置文件简单的多!
但是吧不太建议用,注解开发的维护效率是很低的,所以这篇文章只是带大家了解一下注解开发。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值