1、引入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
建议引入这个,他会自动下载相关的包
1、Dao
2、service(如果不使用Beans.xml,我们可以用Set接口实现注入)
3、Beans
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="MysqlImpl" class="com.xiong.dao.UserMysqlImpl"/>
<bean id="OracleImpl" class="com.xiong.dao.UserOracleImpl"/>
<bean id="SqlservlceImpl" class="com.xiong.dao.UserSqlservlceImpl"/>
<bean id="ServiceImpl" class="com.xiong.service.UserServiceImpl">
<property name="userDao" ref="OracleImpl"/>
</bean>
</beans>
5、Test
public class Mytest {
public static void main(String[] args) {
//获取ApplicationContext,拿到Spring中的容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//需要什么拿什么
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("ServiceImpl");
userServiceImpl.getUser();
/* //用户实际调用的是业务层,dao层他们不需要接触
UserService userService = new UserServiceImpl();
((UserServiceImpl) userService).setUserDao(new UserSqlservlceImpl());
userService.getUser();*/
}
}