一个完整的spring xml配置:是把action,service,dao以及其它的资源性配置(如basedao)和公共性配置(如连接数据库)配置在resource.xml中,这样就有四个xml配置信息.
案例:
四个xml配置:
applicationContext-action.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" scope="prototype" >
<property name="userService" ref="userService" />
</bean>
</beans>
applicationContext-service.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="userService" class="com.cdsxt.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
</beans>
applicationContext-dao.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="userDao" class="com.cdsxt.dao.impl.UserDaoImpl" parent="baseDao" ></bean>
</beans>
applicationContext-resource.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="baseDao" class="com.base.impl.BaseDaoImpl" lazy-init="true" ></bean>
</beans>
Base:
public interface BaseDao<T> {
}
public class BaseDaoImpl<T> implements BaseDao<T> {
private Class clazz;
public BaseDaoImpl() {
ParameterizedType type=(ParameterizedType) this.getClass().getGenericSuperclass();
clazz = (Class) type.getActualTypeArguments()[0];
}
public Class getClazz() {
return clazz;
}
public void setClazz(Class clazz) {
this.clazz = clazz;
}
}
Action:
public class UserAction {
private UserService userService;
public void add(){
System.out.println("=====UserAction=======");
userService.add();
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public static void main(String[] args) {
String[] rs= new String[]{"applicationContext-action.xml","applicationContext-service.xml","applicationContext-dao.xml","applicationContext- resource.xml"};
ApplicationContext context = new ClassPathXmlApplicationContext(rs);
UserAction u1 = (UserAction) context.getBean("userAction");
u1.add();
}
}
Service:
public interface UserService {
public void add();
}
public class UserServiceImpl implements UserService {
private UserDao userDao;
@Override
public void add() {
System.out.println("--------UserServiceImpl----------");
userDao.add();
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
Dao:
public interface UserDao extends BaseDao<User>{
public void add();
}
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao{
@Override
public void add() {
System.out.println("=========UserDaoImpl========");
}
}
po:
public class User {
}