使用Spring容器管理JavaBean

本文介绍Spring框架的核心概念——依赖注入,并通过示例演示如何在JavaBean中应用依赖注入。此外,还介绍了如何读取和配置applicationContext.xml文件来管理对象及其依赖关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Spring容器管理JavaBean
1,什么是Spring?
Spring 是一个集成的框架,核心是一个完整的基于控制反转(loC)的轻量级容器。

2,依赖注入
   依赖注入是Spring的核心机制。
   对依赖注入的理解:两个元素中一个定义发生改变则会引起另一个元素发生变化。则成这两个元素之间存在依赖关系。一个类要发送消息给另外一个类,一个类将另一个类作为其数据的一部分;一个类的操作中将另一个类作为参数,这个类就依赖另一个类。
   例如:
   电脑cpu可以分为很多种,所以可以定义成一个类:
   class Cpu{
       private String cpuname;
       //省略生成的get和set方法。
   } 
   电脑的内存条可以分成很多种,所以也可以定义成一个类。
   class Memory{
        private String memoryname;
        //省略生成的get和set方法。
   }
   ....这里省略很多的元件
   当我要组装一台电脑的时候,要调用各个组成的类
   class Comupter{
       Pirvate Cpu cpu;
       private Memory memory;
      //省略生成的get和set方法。
   }
   //这样这三个类就产生了依赖的关系。

3,怎样读取applicationContext.xml配置文件?
      ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
      Computer hs=(Computer) ac.getBean("pc");
      hs.show();
      ApplicationContext 是一个接口,负责读取Spring的配置文件,管理对象的加载,生成,维护Bean对象
      和Bean对象之间的依赖关系,负责Bean的生命周期等功能。ClassPathXmlApplicationContext是 
      ApplicationContext 接口的一个实现类,主要负责读取Spring的配置文件。

4,applicationContext.xml配置文件的解析。
<?xml version="1.0" encoding="UTF-8"?>
<!--
  - Middle tier application context definition for the image database.
  -->
//头信息
<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"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
                 //定义的bean,该bean的id是hello,可以随意取。class指定该bean实例的类型。
 <bean id="hello" class="hfxt.HelloSpring">
                                //property元素用来指定需要容器注入的属性,hello属性需要容器的注入,此处是设置
                                  值注入,因此定义的类必须拥有set方法。
  <property name="hello" value="Spring"/>
 </bean>
</beans>

以下就是今天写的小demo。

第一个:

 

 HelloSpring.java

package hfxt;

public class HelloSpring {

	private String hello;

	public void say(){
		System.out.println("hello,"+hello);
	}
	public void setHello(String hello) {
		this.hello = hello;
	}
	
}
 
Test.java

package hfxt;

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {  public static void main(String[] args) {

  ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");   HelloSpring hs=(HelloSpring) ac.getBean("hello");   hs.say();  }

}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  - Middle tier application context definition for the image database.
  -->
<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"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	<bean id="hello" class="hfxt.HelloSpring">
		<property name="hello" value="Spring"/>
	</bean>


</beans>

 

第二个:



computer.java

package hfxt;

public class Computer {

 private CPUcom cpucom;
 private Memory menory;
 private Disk disk;
 
 public void show(){
  System.out.println("我买的是:"+cpucom.getCpuname()+","+menory.getMemoryname()+","+disk.getDiskname()+"的主板");
 }

 public void setCpucom(CPUcom cpucom) {
  this.cpucom = cpucom;
 }

 public void setDisk(Disk disk) {
  this.disk = disk;
 }

 public void setMenory(Memory menory) {
  this.menory = menory;
 }
}

CPUcom.java

package hfxt;

public class CPUcom {
 private String cpuname;

 public void setCpuname(String cpuname) {
  this.cpuname = cpuname;
 }

 public String getCpuname() {
  return cpuname;
 } 
}

 

Disk.java

package hfxt;

public class Disk {

 private String diskname;

 public void setDiskname(String diskname) {
  this.diskname = diskname;
 }

 public String getDiskname() {
  return diskname;
 } 
}

 

Memory.java

package hfxt;

public class Memory {

 private String memoryname;

 public void setMemoryname(String memoryname) {
  this.memoryname = memoryname;
 }

 public String getMemoryname() {
  return memoryname;
 }
 
}

Test.java

package hfxt;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
 public static void main(String[] args) {
  ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
  Computer hs=(Computer) ac.getBean("pc");
  hs.show();
  
  Computer hs1=(Computer) ac.getBean("pc1");
  hs1.show();
 }

}

 

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  - Middle tier application context definition for the image database.
  -->
<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"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
 <bean id="amd" class="hfxt.CPUcom">
  <property name="cpuname" value="AMD处理器"/>
 </bean>
 <bean id="inter" class="hfxt.CPUcom">
  <property name="cpuname" value="INTER处理器"/>
 </bean>
 <bean id="jinsiten" class="hfxt.Memory">
  <property name="memoryname" value="金士顿内存条"/>
 </bean>
 <bean id="weigang" class="hfxt.Memory">
  <property name="memoryname" value="威刚内存条"/>
 </bean>
 <bean id="xijie" class="hfxt.Disk">
  <property name="diskname" value="希捷硬盘"/>
 </bean> 
 <bean id="wd" class="hfxt.Disk">
  <property name="diskname" value="西部数据硬盘"/>
 </bean>
 
 <bean id="pc" class="hfxt.Computer">
  <property name="cpucom" ref="amd"/>
  <property name="menory" ref="jinsiten"/>
  <property name="disk" ref="xijie"/>
 </bean>
 
 <bean id="pc1" class="hfxt.Computer">
  <property name="cpucom" ref="inter"/>
  <property name="menory" ref="weigang"/>
  <property name="disk" ref="wd"/>
 </bean>

</beans>


 


 


 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 



                
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值