[align=center][b] :D 上一篇是注解注入一个bean,这篇就是XML配置注入一个bean[/b][/align]
废话不多说直接上代码,首先是XML配置文件:
上面的配置,是以XML为主,也可以注解的方式:
这是其中一个配置,包含在上面咯:
下面开始声明几个类:
准备工作完毕了,下面我们测试吧:
测试一XML配置为主的配置:
测试以注解为主的配置:
废话不多说直接上代码,首先是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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="classAId" class="com.expect.oa.entity.ClassA"></bean>
<bean id="classBId" class="com.expect.oa.entity.ClassB">
<!-- 这嘎达用的是构造函数注入,注入了一个对象,跟两个字符串 -->
<constructor-arg ref="classAId"></constructor-arg>
<constructor-arg name="string1" value="some string1"></constructor-arg>
<constructor-arg name="string2" value="some string2"></constructor-arg>
</bean>
<bean id="classCId" class="com.expect.oa.entity.ClassC">
<!-- 这嘎达也是用的构造函数注入,注入了一个ArrayLisy<String> -->
<constructor-arg>
<list>
<value>Lin</value>
<value>Chirs</value>
<value>Biger</value>
</list>
</constructor-arg>
</bean>
<!-- 这嘎达是属性注入,注入一个对象 -->
<bean id="classDId" class="com.expect.oa.entity.ClassD">
<property name="aEntity" ref="classAId"></property>
</bean>
<!-- 这嘎达是注解的配置文件 -->
<bean id="SpringXMLconfig_other" class="com.expect.oa.config.SpringXMLconfig_other" />
</beans>
上面的配置,是以XML为主,也可以注解的方式:
package com.expect.oa.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
import com.expect.oa.entity.ClassE;
import com.expect.oa.entity.ClassF;
@Configuration
@Import(value={SpringXMLconfig.class})
@ImportResource(locations={"classpath:spring/ApplicationContext.xml"})
public class SpringXMLconfig_other {
@Bean
public ClassF getClassF(ClassE fEntity){
return new ClassF(fEntity);
}
}
这是其中一个配置,包含在上面咯:
package com.expect.oa.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.expect.oa.entity.ClassE;
@Configuration
public class SpringXMLconfig {
@Bean
public ClassE getClassE (){
return new ClassE();
}
}
下面开始声明几个类:
package com.expect.oa.entity;
public class ClassA {
}
package com.expect.oa.entity;
public class ClassB {
private ClassA aEntity;
private String string1;
private String string2;
public ClassB(ClassA aEntity){
this.aEntity = aEntity;
}
public ClassA getaEntity() {
return aEntity;
}
public void setaEntity(ClassA aEntity) {
this.aEntity = aEntity;
}
public String getString1() {
return string1;
}
public void setString1(String string1) {
this.string1 = string1;
}
public String getString2() {
return string2;
}
public void setString2(String string2) {
this.string2 = string2;
}
public ClassB(ClassA aEntity, String string1, String string2) {
super();
this.aEntity = aEntity;
this.string1 = string1;
this.string2 = string2;
}
}
package com.expect.oa.entity;
import java.util.List;
public class ClassC {
private List<String> names;
public List<String> getNames() {
return names;
}
public void setNames(List<String> names) {
this.names = names;
}
public ClassC(List<String> names) {
super();
this.names = names;
}
}
package com.expect.oa.entity;
public class ClassD {
ClassA aEntity;
public ClassA getaEntity() {
return aEntity;
}
public void setaEntity(ClassA aEntity) {
this.aEntity = aEntity;
}
}
package com.expect.oa.entity;
public class ClassE {
}
package com.expect.oa.entity;
public class ClassF {
private ClassE eEntity;
public ClassF(ClassE eEntity){
this.eEntity = eEntity;
}
public ClassE geteEntity() {
return eEntity;
}
public void seteEntity(ClassE eEntity) {
this.eEntity = eEntity;
}
}
准备工作完毕了,下面我们测试吧:
package com.expect.oa.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.expect.oa.entity.ClassB;
import com.expect.oa.entity.ClassC;
import com.expect.oa.entity.ClassD;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring/ApplicationContext.xml"})
public class TestXMLSpringDI {
@Resource(name="classBId")
ClassB bEntity;
//通过构造函数构造了一个对象成员变量,跟两个字符串变量。
@Test
public void test() {
System.out.println(bEntity.getString1());
System.out.println(bEntity.getaEntity());
}
@Resource(name="classCId")
ClassC cEntity;
//构造一个list
@Test
public void testXmlDi (){
for (int i = 0;i<3;i++){
String name = cEntity.getNames().get(i);
System.out.println(name);
}
}
@Resource(name="classDId")
ClassD dEntity;
//属性注入对象
@Test
public void testXmlPropertyDi (){
System.out.println(dEntity.getaEntity());
}
}
测试一XML配置为主的配置:
package com.expect.oa.test;
import javax.annotation.Resource;
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;
import com.expect.oa.entity.ClassD;
import com.expect.oa.entity.ClassF;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring/ApplicationContext.xml"})
public class TestSpringXMLConfig {
//这里是通过注解配置的
@Autowired
ClassF fEntity;
@Test
public void test() {
System.out.println(fEntity.geteEntity());
}
//属性注入对象,这里是通过XML配置的
@Resource(name="classDId")
ClassD dEntity;
@Test
public void testXmlPropertyDi (){
System.out.println(dEntity.getaEntity());
}
}
测试以注解为主的配置:
package com.expect.oa.test;
import javax.annotation.Resource;
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;
import com.expect.oa.config.SpringXMLconfig_other;
import com.expect.oa.entity.ClassD;
import com.expect.oa.entity.ClassF;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringXMLconfig_other.class})
public class TestSpringConfig {
//这里是通过注解配置的
@Autowired
ClassF fEntity;
@Test
public void test() {
System.out.println(fEntity.geteEntity());
}
//属性注入对象,这里是通过XML配置的
@Resource(name="classDId")
ClassD dEntity;
@Test
public void testXmlPropertyDi (){
System.out.println(dEntity.getaEntity());
}
}