xml配置格式加载

 

package com.ls.spring.ioc.dummy;

import org.springframework.stereotype.Component;

@Component
public class B {

	public void put() {
		System.out.println("B is outing...");
	}
	
}

package com.ls.spring.ioc.dummy;

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

@Component
public class A {
	@Autowired
	private B b;
	
	public A() {
		super();
	}

	public A(B b) {
		super();
		this.b = b;
	}

	public B getB() {
		return b;
	}

	public void setB(B b) {
		this.b = b;
	}
	
}
 

<?xml version="1.0" encoding="GBK" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
    <bean id="a" class="com.ls.spring.ioc.dummy.A" >
        <constructor-arg index="0">
            <ref bean="b"/>
        </constructor-arg>
    </bean>
    <bean id="b" class="com.ls.spring.ioc.dummy.B" />
</beans>

 

package com.ls.spring.ioc.dummy;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

public class TestBeanFactoryViaXml {
	
	public static void main(String[] args) {
		DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
		
		XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
		reader.loadBeanDefinitions("classpath:com/ls/spring/ioc/dummy/ioc-test.xml");
		
		A a = (A) factory.getBean("a");
		a.getB().put();
	}
	
}
 

 

 

 

 

 

你可能感兴趣的:(spring,bean,xml,IOC)