@Bean
1@bean的属性介绍
String[] value() default {}; //这个属性的作用我也不知道
String[] name() default {}; //bean的名字 默认方法的名字
Autowire autowire() default Autowire.NO; 是否对该bean的属性实行自动装配
String initMethod() default ""; //初始hua方法
String destroyMethod() default "(inferred)";//bean死亡时调用的方法
2@Bean的使用
实体类
package liusheng.entity; public class Student { private String name; private String password; public Student() { } public Student(String name, String password) { this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <context:component-scan base-package="liusheng"/> </beans>
产生Bean的工厂
package liusheng.fatory; import liusheng.entity.Student; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration//也可以使用@Component public class StudentFactory { /* 默认使用方法名,如果指定了name="student" 那么bean的名字是student */ /** * 默认使用 * @return */ @Bean public static Student getNotParameterStudent(){ return new Student(); } /* 使用Beans上参数的名字,可以指定多个 */ @Bean(name={"student1","student2"}) public static Student getNotParameterStudent1(){ return new Student(); } /** * 使用一个参数的名字 * 由@Value给予 * @param name * @return */ @Bean @Value("张三") public static Student getOneParamterStudent(String name){ return new Student(name,"123"); } }
测试类
package liusheng; import liusheng.entity.Student; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Scope; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:SpringConfig.xml") public class BeanTest { @Autowired Student student1; @Autowired @Qualifier("student1") Student student2; @Autowired @Qualifier("student1") Student student3; @Autowired @Qualifier("getOneParamterStudent") Student student4; @Test public void test(){ System.out.println(student1); System.out.println(student2); System.out.println(student3); System.out.println(student4); } }
结果
结论:我还不知道value属性是有什么用,我一开始把value属性可以设置bean的名字,但是实际上是name属性
这篇博客有什么错误的地方请大牛们多指教