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