1,我们知道前端和数据库交互的过程的中,要经过以下流程:
从图中可以看出,spring整合了web,service,dao,使得开发更加便捷。
那么首先我们要下载spring-framework框架,下载的详细流程请参照https://www.cnblogs.com/116970u/p/10205041.html
这个是spring-framework-3.02RELEASE-dependdencies下载,这个包里面有我们平时使用到的工具我,而且已经不再更新了。 https://blog.csdn.net/qq_32965187/article/details/89337006
,下载到本地解压之后:
点开libs中就是常用的jar包,如果要搭建一个最简单的spring项目,我们只需要复制以下几个jar包到工程的libs目录下就行了
复制之后点击目录邮件选择Add to Libary就可以了。
后面也可能用到日子包,所以需要去3.0.2dependenpency包中把日治包找到并添加
以上是jar包添加的过程,jar包添加完成之后我们还需编写配置文。
要在bean标签中配置要自动创建的对象。bean标签:在src目录下创建applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
显然bean标签就在这里了:然后告诉文件需要自动创建那个对象:
<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.xsd">
<!--接下来要告诉beans需要为我们自动创建那个对象-->
<bean name="User" class="com.shop.domain.User"/>
</beans>
一切完成之后,我们创建一个类User
public class User {
@Override
public String toString() {
return "User{"+
"id="+id+'\''+
",username="+username+'\''+
",address="+address+'\''+
"}";
}
private Integer id;
private String username;
private String address;
public void setId(Integer id) {
this.id = id;
}
public void setAddress(String address) {
this.address = address;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getId() {
return id;
}
public String getAddress() {
return address;
}
public String getUsername() {
return username;
}
}
新建一个测试类:
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo1 {
@Test
public void fun1(){
//读取到applicationContext.xml问价
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
//找到我们想要的类
User user= (User) applicationContext.getBean("User");
System.out.println(user.toString());
}
}
运行之后输出: