Spring的重点配置:
<bean>标签
id属性:在容器中Bean实例的唯一标识,不允许重复
class属性:要实例化的Bean的全限定名
scope属性:Bean的作用范围,常用是Singleton(默认,单例)和prototype
<property>标签:属性注入
name属性:属性名称
value属性:注入的普通属性值
ref属性:注入的对象引用值
<list>标签
<map>标签
<properties>标签
<constructor-arg>标签
<import>标签:导入其他的Spring的分文件
<?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.xsd">
<!-- <bean id="userDao" class="com.dingdong.dao.impl.UserDaoImpl" scope="prototype">-->
<!-- <property name="name" value="zhangsan"></property>-->
<!-- <property name="age" value="18"></property>-->
<!-- </bean>-->
<!-- <bean id="userService" class="com.dingdong.dao.impl.UserServiceImpl" p:userDao-ref="userDao"></bean>-->
<bean id="userDao" class="com.dingdong.dao.impl.UserDaoImpl" scope="prototype">
<property name="strList">
<list>
<value>zhangsan</value>
<value>lisi</value>
<value>wangwu</value>
</list>
</property>
<property name="userMap">
<map>
<entry key="zhangsan" value-ref="user1"></entry>
<entry key="lisi" value-ref="user2"></entry>
</map>
</property>
<property name="properties">
<props >
<prop key="p1">ppp1</prop>
<prop key="p2">ppp2</prop>
<prop key="p3">ppp3</prop>
</props>
</property>
</bean>
<bean id="user1" class="com.dingdong.domain.User" >
<property name="name" value="zhangsan111"></property>
<property name="addr" value="zhangzhou"></property>
</bean>
<bean id="user2" class="com.dingdong.domain.User" >
<property name="name" value="lisi555555"></property>
<property name="addr" value="beijing"></property>
</bean>
<bean id="userService" class="com.dingdong.dao.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
<import resource="applicationContext-product.xml"/>
<import resource="applicationContext-user.xml"/>
</beans>
package com.dingdong.demo;
import com.dingdong.dao.UserDao;
import com.dingdong.dao.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserDaoDemo {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
// UserDao userDao = (UserDao) app.getBean("userDao");
// userDao.save();
UserService userService = (UserService)app.getBean("userService");
userService.save();
}
}
##############
package com.dingdong.dao.impl;
import com.dingdong.dao.UserDao;
public class UserServiceImpl implements com.dingdong.dao.UserService {
private UserDao userDao;
// public void setUserDao(UserDao userDao) {
// this.userDao = userDao;
// }
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void save(){
this.userDao.save();
}
}
################
package com.dingdong.dao.impl;
import com.dingdong.dao.UserDao;
import com.dingdong.domain.User;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class UserDaoImpl implements UserDao {
// private String name;
// private int age;
//
// public void setAge(int age) {
// this.age = age;
// }
//
// public void setName(String name) {
// this.name = name;
// }
private Properties properties;
private List<String> strList;
@Override
public String toString() {
return "UserController{" +
"properties=" + properties +
", strList=" + strList +
", userMap=" + userMap +
'}';
}
private Map<String, User> userMap;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public List<String> getStrList() {
return strList;
}
public void setStrList(List<String> strList) {
this.strList = strList;
}
public Map<String, User> getUserMap() {
return userMap;
}
public void setUserMap(Map<String, User> userMap) {
this.userMap = userMap;
}
@Override
public void save() {
System.out.println("UserDataLmpl save running");
// System.out.println(name+"============"+age);
System.out.println(strList);
System.out.println(userMap);
System.out.println(properties);
}
}
###########################
package com.dingdong.dao;
public interface UserDao {
public void save();
}
############################
package com.dingdong.dao;
public interface UserService {
public void save();
}
#######################
#User.java
package com.dingdong.domain;
import java.util.List;
import java.util.Properties;
public class User {
private String name;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", addr='" + addr + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
private String addr;
}
#################