constructor-arg与property的区别

本文深入解析Spring框架中两种主要的依赖注入方式:构造方法注入和setter注入。通过具体代码示例,展示了如何在XML配置文件中使用<constructor-arg>和<property>元素来实现依赖注入,帮助读者理解并掌握Spring IoC容器的使用。

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

      constructor-arg:通过构造方法注入
      property:是通过setter方法注入

  constructor-arg 示例说明:
代码:
package cn.com.leadfar.spring;

import java.io.File;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
public class UserService implements BeanFactoryAware{

//	private ExcelTransfer transfer;
	private BeanFactory factory;
	
	private String saveDir;//储存位置
	private int maxFile;//最大文件数量
	private List list;
	
	//通过构造方法注入
	**public UserService(String saveDir,int maxFile,List list) {
		this.saveDir=saveDir;
		this.maxFile=maxFile;
		this.list=list;
		
	}**

	public void addUser(){
		
		System.out.println("UserService.addUser()");
	}
	
	public void uploadExcel(File f) {
		ExcelTransfer transfer=(ExcelTransfer)factory.getBean("excelTransfer");
		transfer.transfer(f);
		
		System.out.println("储存的位置和文件的数量"+":"+saveDir+","+maxFile);
	}		

	@Override
	public void setBeanFactory(BeanFactory f) throws BeansException {
		// TODO Auto-generated method stub
		this.factory=f;
		
	}
}

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-3.2.xsd">
   <bean id="userAction" class="cn.com.leadfar.spring.UserAction" scope="prototype">
     <constructor-arg ref="userService"></constructor-arg>
   </bean>
   <bean id="userService" class="cn.com.leadfar.spring.UserService">
      <constructor-arg value="d:/t/"></constructor-arg>
      <constructor-arg value="500"></constructor-arg>
      <constructor-arg>
          <list>
              <value>世界</value>
              <value>你好</value>
              <ref bean="excelTransfer"/>
          </list>
      </constructor-arg>
   </bean>
   <bean id="excelTransfer" class="cn.com.leadfar.spring.ExcelTransfer" scope="prototype">
   </bean>
</beans>```




property示例:
  

private BeanFactory factory;

private String saveDir;//储存位置
private int maxFile;//最大文件数量
public void setSaveDir(String saveDir) {
	this.saveDir = saveDir;
}

public void setMaxFile(int maxFile) {
	this.maxFile = maxFile;
}

public void addUser(){
	
	System.out.println("UserService.addUser()");
}

public void uploadExcel(File f) {
	ExcelTransfer transfer=(ExcelTransfer)factory.getBean("excelTransfer");
	transfer.transfer(f);
	
	System.out.println("储存的位置和文件的数量"+":"+saveDir+","+maxFile);
}		

@Override
public void setBeanFactory(BeanFactory f) throws BeansException {
	// TODO Auto-generated method stub
	this.factory=f;
	
}

}```

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-3.2.xsd">
   <bean id="userAction" class="cn.com.leadfar.spring.UserAction" scope="prototype">
      <property name="myUserService" ref="userService"></property>
   </bean>
   <bean id="userService" class="cn.com.leadfar.spring.UserService">
      <property name="saveDir" value="d:t/tt/"></property>
      <property name="maxFile" value="500"></property>
   </bean>
   <bean id="excelTransfer" class="cn.com.leadfar.spring.ExcelTransfer" scope="prototype">
   </bean>
</beans>

参考:https://blog.51cto.com/racoguo/1236379

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值