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