标签:xmla pre turn classpath this string ram bean int
1.
自动装配: byName或byType
2. spring.xml中设置全局是byName自动装配
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName">
<!-- ###############自动装配############### -->
<bean id="userDao" class="cn.itcast.d_auto.UserDao"></bean>
<bean id="userService" class="cn.itcast.d_auto.UserService"></bean>
<!-- 如果根据类型自动装配: 必须确保IOC容器中只有一个该类型的对象 -->
<bean id="userAction" class="cn.itcast.d_auto.UserAction"></bean>
<!-- 报错: 因为上面已经有一个该类型的对象,且使用了根据类型自动装配
<bean id="userService_test" class="cn.itcast.d_auto.UserService" autowire="byType"></bean>
-->
</beans>
3.测试类
public class App { // 创建容器对象 private ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/d_auto/bean.xml"); @Test public void testExecuteAction() { // 从容器中获取Action UserAction userAction = (UserAction) ac.getBean("userAction"); userAction.execute(); } }
4. 控制层
public class UserAction { private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public String execute() { userService.save(); return null; } }
5. 业务逻辑
public class UserService { private UserDao userDao; // = new UserDao(); public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save() { userDao.save(); } }
6.持久层
public class UserDao { public void save() { System.out.println("DB:保存用户..."); } }
标签:xmla pre turn classpath this string ram bean int
原文地址:http://www.cnblogs.com/bravolove/p/6820597.html