自动装配

具体文件格式

application.xml

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">

<!--

<context:annotation-config></context:annotation-config>

解析Spring注解并且赋予其功能

Spring3.0之前是需要手动配置

Spring Test测试中是不需要引入

JAVA WEB中必须引入 因为两者的xml文件不一致

-->

<context:annotation-config></context:annotation-config>

<bean id="bean1" class="autowire.otherbean1"></bean>

<bean id="bean2" class="autowire.otherbean2"></bean>

<bean id="bean3" class="autowire.otherbean2"></bean>

<bean id="bean" class="autowire.somebean"></bean>

</beans>

 

 

 

somebean测试类

package autowire;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

 

public class somebean {

@Autowired

private otherbean1 bean1;

@Autowired //@Autowired:用于自动根据类型配置

@Qualifier("bean2") //@Qualifier:用于根据bean元素的id来配置

private otherbean2 bean2;

 

@Override

public String toString() {

return "somebean{" +

"bean1=" + bean1 +

", bean2=" + bean2 +

'}';

}

}

otherbean1类

package autowire;

 

public class otherbean1 {

}

otherbean2类

package autowire;

 

public class otherbean2 {

}

test测试类

package autowire;

 

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;

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class test {

@Autowired

private somebean bean;

@Test

public void test(){

System.out.println(bean);

}

}

测试结果:

 

 

 

第二种配置方式

具体文件格式

 

applicationContext.xml

<?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:annotation-config></context:annotation-config>

解析Spring注解并且赋予其功能

Spring3.0之前是需要手动配置

Spring Test测试中是不需要引入

JAVA WEB中必须引入 因为两者的xml文件不一致

使用JavaEE的时候必须引入

-->

<context:annotation-config></context:annotation-config>

<bean id="bean1" class="resource.otherbean1"></bean>

<bean id="bean2" class="resource.otherbean2"></bean>

<bean id="bean3" class="resource.otherbean2"></bean>

<bean id="bean" class="resource.somebean"></bean>

</beans>

 

 

 

otherbean1类

package resource;

 

public class otherbean1 {

}

 

 

otherbean2类

package resource;

 

public class otherbean2 {

}

 

somebean类

package resource;

 

import javax.annotation.Resource;

 

public class somebean {

@Resource//先寻找id,再寻找类型

private otherbean1 bean1;

@Resource

private otherbean2 bean2;

 

@Override

public String toString() {

return "somebean{" +

"bean1=" + bean1 +

", bean2=" + bean2 +

'}';

}

}

 

 

 

test测试类

package resource;

 

import javafx.application.Application;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.context.ApplicationContext;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

 

import javax.annotation.Resource;

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext.xml")

public class test {

@Resource

private ApplicationContext ctx;

@Resource

private somebean bean;

@Test

public void test(){

System.out.println(ctx.getBean("bean"));

System.out.println(bean);

}

}

 

 

 

测试结果如下

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值