spring 基于注解的自动装配

xiaoxiao2021-02-28  85

自动装配会用到的注解主要有三个:@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(); } }
转载请注明原文地址: https://www.6miu.com/read-45071.html

最新回复(0)