1.1 Spring开发步骤
Spring开发步骤:
1.在pom.xml中导入jar包坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
2.编写接口Dao层和实现类
3.创建Spring核心xml配置文件
**在src.main,resources中配置(xml文件为图中的applicationContext.xml)
这里我遇到了如下问题:
IDEA创建XML文件没有Spring Config选项,这个问题是由于pom.xml文件没有被加载,需要刷新一下项目
解决方法:参考 https://blog.csdn.net/qq_34624315/article/details/82661373 的方法成功解决
application context not configured for this file,这个问题是由于xml文件没有被加载
解决方法:
参考 https://blog.csdn.net/YangJiaJun0506/article/details/79302364 的方法成功解决。
4.在Spring配置文件中配置bean
//通过唯一的id去访问,class里放实现类的路径
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
5.通过Spring的API获取Bean实例
在test测试类中,获取Bean实例的方法:
//1.获取ApplicationContext对象
ApplicationContext app=new ClassPathXmlApplicationContext("application.xml"); //参数为Spring核心对象
//2.通过ApplicationContext对象的getBean方法获取Bean实例
UserDao dao=(UserDao)app.getBean("userDao"); //参数为xml中bean标签的id
//....执行方法
一点小问题:
执行方法后Intellij idea 报错:Error : java 不支持发行版本5
原因 jdk没有统一为本地jdk
解决:设置统一jdk,参考 https://www.cnblogs.com/powerwu/articles/10570448.html 的做法成功解决