Spring——环境搭建步骤

1. 搭建项目环境

2. 实现dao、service等编码

3. 在项目src目录下新建applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

</beans>

4.在 applicationContext.xml的<beans>节点中管理bean对象实例
<bean id="userDao" class="spring.UserDaoImpl_2"></bean>

<bean id="userService" class="spring.UserService">
<property name="dao" ref="userDao"></property>
</bean>

5.新建lib文件夹,添加spring【四大核心】包:
spring-beans.jar、
spring-context.jar、
spring-core.jar、
spring-expression.jar

异常: java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
添加依赖包: commons-logging.jar

6.测试spring环境
//1.装载并解析applicationContext.xml文件
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");

//2.获取工厂中的Service实例
UserService service = (UserService) factory.getBean("userService");

//3.调用实例中的方法
service.findUser();

posted @ 2016-12-01 16:46  龙之天族  阅读(490)  评论(0编辑  收藏  举报