【1】依赖注入两种方式
一、set注入方式
1、java类 [IoCService.java]
package com.weixun.bean;
public class IoCService {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void display(){
System.out.println(getMessage());
}
}
2、bean配置 [IoCBeans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="iocService" class="com.weixun.spring.service.IoCService">
<property name="message">
<value>hello</value>
</property>
</bean>
</beans>
3、测试 [Test.java]
package com.weixun.spring.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.weixun.spring.service.IoCService;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ClassPathResource resource=new ClassPathResource("IoCBeans.xml");
BeanFactory bean=new XmlBeanFactory(resource);
IoCService service= (IoCService)bean.getBean("iocService");
service.getMessage();
}
}
二、构造注入方式
1、
public class IoCService {
private String message;
public IoCService (String message){
this,message=message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void display(){
System.out.println(getMessage());
}
}
2、
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="iocService" class="com.weixun.bean.IoCService" >
<constructor-arg>
<value>hello</value>
</constructor-arg>
</bean>
</beans>
【2】
注入列表、注入对象
package com.weixun.bean;
import java.util.List;
public class IoCService {
public String message;
public List list;
public Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public IoCService(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void display() {
System.out.println(getMessage());
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).toString());
}
System.out.println("学生名"+getStudent().name);
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="student" class="com.weixun.bean.Student">
<property name="number" value="1" />
<property name="name" value="xiaoming" />
</bean>
<bean id="iocService" class="com.weixun.bean.IoCService" >
<constructor-arg>
<value>hello</value>
</constructor-arg>
<property name="list">
<list>
<value>stu1</value>
<value>stu2</value>
<value>stu3</value>
<value>stu4</value>
</list>
</property>
<property name="student" ref="student" />
</bean>
</beans>