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);
    }
}

复制代码

  结果

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值