Spring IOC实例

【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>

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值