@Bean的使用

@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属性

  这篇博客有什么错误的地方请大牛们多指教

 


  

转载于:https://www.cnblogs.com/SpringStudy/p/8590036.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值