具体文件格式
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);
}
}
测试结果如下