spring 基于注解的自动装配

自动装配会用到的注解主要有三个:@AutoWired、@Qualifier、@Resource


下面会用一个HelloWorld的例子来演示这三种注解的使用,首先准备用于测试的helloWorld类,并将其配置到Spring配置文件中

package com.autowired;

/**
 * Created by PLEI on 7/6/2017.
 */
public class HelloWorld {
    public HelloWorld(){
        System.out.println("HelloWorld init.........");
    }
    
    public void sayHello(){
        System.out.println("hellowWorld!....");
    }
}


配置到spring配置文件中

<bean id="helloWorld" class="com.autowired.HelloWorld"/>


1.@AutoWired:

@AutoWired注解可以使用在属性上也可以使用在方法上

在属性上使用
package com.test;

import com.autowired.HelloWorld;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by PLEI on 7/6/2017.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class AutowiredTest {
    @Autowired
    private HelloWorld helloWorld;

    @Test
    public void test() throws Exception{
        helloWorld.sayHello();
    }
}

在方法上使用
package com.test;

import com.autowired.HelloWorld;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by PLEI on 7/6/2017.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class AutowiredTest {

    private HelloWorld helloWorld;

    @Autowired
    public void reyfangfa(HelloWorld helloWorld){
        this.helloWorld=helloWorld;
    }

    @Test
    public void test() throws Exception{
        helloWorld.sayHello();
    }
}

注意事项:

@Autowired是根据类去spring容器中查找实例并自动装配的,如果spring有多个相同类的实例便会报错,@Autowired在默认情况下是必须进行装配,如果没有装配成功会报错可以通过设置

@Autowired(required = false)来让自动装配没有成功时不进行自动装配

2.@Qualifier

@Qualifier一般与@AutoWired搭配使用,作用为在spring容器中通过类进行查找的同时会通过spring中配置的bean的id进行查找
	<bean id="helloWorld1" class="com.autowired.HelloWorld"/>
	<bean id="helloWorld2" class="com.autowired.HelloWorld"/>
package com.test;

import com.autowired.HelloWorld;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by PLEI on 7/6/2017.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class AutowiredTest {

    @Autowired
    @Qualifier("helloWorld1")
    private HelloWorld helloWorld;


    @Test
    public void test() throws Exception{
        helloWorld.sayHello();
    }
}

3.@Resource

@Resource为sun公司提供的注解,也可以使用与属性和方法上,在自动装配时默认使用装配类的首字母小写的字符串去容器中查找实例进行装配
<bean  class="com.autowired.HelloWorld"/>


package com.test;

import com.autowired.HelloWorld;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

/**
 * Created by PLEI on 7/6/2017.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class AutowiredTest {

    @Resource
    private HelloWorld helloWorld;


    @Test
    public void test() throws Exception{
        helloWorld.sayHello();
    }
}

也可以通过指定spring配置文件的id来查找实例进行装配
<bean id="abc" class="com.autowired.HelloWorld"/>

package com.test;

import com.autowired.HelloWorld;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

/**
 * Created by PLEI on 7/6/2017.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class AutowiredTest {

    @Resource(name="abc")
    private HelloWorld helloWorld;


    @Test
    public void test() throws Exception{
        helloWorld.sayHello();
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值