浅谈引用类型自动注入的三种方式

第一种 :通过配置文件注入

1、 通过 autowire=“byType”
<bean class="com.Banzhuren" id="banzhuren" autowire="byType"></bean>

在这段代码中,首先通过class="com.Banzhuren"找到所在的类文件里面的相关引用类型数据,再给这些引用类型自动注入,方式是ByType,即通过属性的类型,在配置文件中找到相应的class类型与之相对应的,在创建该类的对象,例如我的com.Banzhuren里面有Student student对象,所以通过autowire="byType"会在bean配置里面改class里面有与之相对应的Student,并创建该对象。


<bean id="student" class="com.Student">
2、通过 autowire=“byName”
<bean class="com.Banzhuren" id="banzhuren" autowire="byName"></bean>

这种方式与byType相类似,在自动创建时通过Class找到类所包含的引用类型,并在配置文件中找到与之id相对应的bean,再通过该bean’创建对象。

第二种 :配置文件与@Autowired注解注入

@Autowired
private Student student;
@Autowired
private Teacher teacher;

在实体类里面,通过在需要自动注入的对象前添加@@Autowired,相当于在配置文件中的autowire=“byType”,配合配置文件的其他相关的bean属性完成配置。

第三种 :纯注解完成注入

//表明是配置类,相当于xml文件
@Configuration
public class SpringConfig  {
    //相当与<bean id="getstudent" class="com.Student">
    @Bean
    public Student getStudent(){
        return new Student();
    }
    @Bean
    public Teacher getTeacher(){
        return new Teacher();

    }
    @Bean
    public Banzhuren getBanzhuren(){
        return new Banzhuren();
    }
}

首先创建一个配置类,在类前面添加@Configuration注解表面该类为一个配置类类,再通过@Bean注解标识需要自动注入的对象,相当与在配置文件中的bean id=“getstudent” class=“com.Student”。第二步,在实体类中的引用类型前添加@Autowired自动注入该对象,在创建容器对象时应该使用new AnnotationConfigApplicationContext(配置类类名.class)的方式创建。

// SpringConfig为我的配置类类名,Banzhuren为我的实体类类名
ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class);
Banzhuren banzhuren=(Banzhuren)context.getBean(Banzhuren.class);
System.out.println(banzhuren.getStudent());

简述集合类型注入:

1、集合类型注入基本类型数据

Map<String,Student> map1;
List<String> list;
Set<String> set;
Map<String,String> map;

在某一个实体类中有以下集合数据类型需要自动注入,在配置文件中要如下配置:

  <bean id="student" class="com.Student">
        <!--简单属性list的自动注入-->
        <property name="list">
            <list>
            <value>1</value>
                <value>2</value>
                <value>3</value>
            </list>
        </property>
<!--        set类型的注入方式-->
        <property name="set">
            <set>
                <value>4</value>
                <value>5</value>
                <value>6</value>
            </set>
        </property>
<!--        map 使用键值对简单对象的注入方式-->
        <property name="map">
            <map>
                <entry key="yihao" value="小明"></entry>
                <entry value="小李" key="erhao"></entry>
                <entry key="sanhao" value="小华"></entry>
            </map>
        </property>
       </bean>

通过class属性指定需要注入的类所在的位置,通过property name="list"指定需要注入的类型的名称,之后通过value按序注入值,其中map为键值对储存的,需要entry标签注值。

2、集合类型注入引用类型

// 通过value-ref注入引用类型的value 链接
<property name="map1">
    <map>
        <entry value-ref="student" key="zhangsan"></entry>
    </map>
</property>

通过ref-value注值时,ref-value会在配置文件中寻找参数名与bean的id值想对应的bean标签,通过与该bean标签链接注入引用类型的值。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值