案例:
beans.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" >
<bean id="userAction" class="com.cdsxt.action.UserAction" >
<!-- name:对应action类内属性的名字 ref:对应bean元素的id的值
<property name="userDao" ref="userDao" />
<property name="str" value="aaaaa" />-->
<constructor-arg index="0" ref="userDao" />
<constructor-arg index="1" value="aaaa" />
<constructor-arg index="2" >
<list >
<value>aaaa</value>
<value>bbb</value>
</list>
</constructor-arg>
<constructor-arg index="3" >
<map >
<entry key="aaa">
<value>aaaa</value>
</entry>
</map>
</constructor-arg>
<constructor-arg index="4" >
<props >
<prop key="bbb">bbbb</prop>
</props>
</constructor-arg>
</bean>
<bean id="userDao" class="com.cdsxt.dao.impl.UserDaoImpl" ></bean>
</beans>
Action:
public class UserAction {
private UserDao userDao;
private String str;
private List<String> strList;
private Map<String, String> strMap;
private Properties pro;
public UserAction(){ }
public UserAction(UserDao userDao, String str, List<String> strList, Map<String, String> strMap, Properties pro) {
this.userDao = userDao;
this.str = str;
this.strList = strList;
this.strMap = strMap;
this.pro = pro;
};
public void add(){
System.out.println("==========UserAction============");
userDao.add();
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
public List<String> getStrList() {
return strList;
}
public void setStrList(List<String> strList) {
this.strList = strList;
}
public Map<String, String> getStrMap() {
return strMap;
}
public void setStrMap(Map<String, String> strMap) {
this.strMap = strMap;
}
public Properties getPro() {
return pro;
}
public void setPro(Properties pro) {
this.pro = pro;
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserAction u1 =(UserAction) context.getBean("userAction");
System.out.println(u1.str+"====="+u1.strList);
System.out.println(u1.strMap+"===="+u1.pro);
u1.add();
}
}
public interface UserDao {
public void add();
}
public class UserDaoImpl implements UserDao{
@Override
public void add() {
System.out.println("=========UserDao===========");
}
}