Spring框架搭建记录
综合自己的手动搭建过程记录一下使用idea搭建Spring框架(未使用maven)。idea内部有集成一键搭建,新手的话推荐手动搭建,也可以熟悉一下流程。
Spring核心点就是IOC(控制反转)和AOP(面向切面),使用框架可以让开发过程趋于简便,降低代码的耦合度。
Spring的结构
借用一张图来概述
jar包准备
beans、core、context、expression四个核心,一个日志库com.springsource.org.apache.commons.logging-1.1.1.jar
idea添加方法:File–>Project Structrue,选择project setting下的libraries,点击左边任务栏的 + ,选择存放路径下的jar包。
我这里是事先在WEB-INF下新建了一个lib文件夹用来存放所需要的jar包。位置影响不大,第一次创建一定要按照上述方法选择添加,直接复制粘贴不会生效,一定要注意!!!
Spring配置文件
在src目录下创建一个applicationContext.xml文件,idea的xml创建貌似阔以引入schema约束文件但导入不了,也阔能是我的打开方式不对。(这点问题不大,可自行百度研究)
既然用不了,那也不可能手动码出来。其实idea已经为我们搭建了一个模板。如图所示
完成以上步骤以后就不用重复的操作了,因为已经添加到可选项了(使用一键式搭建则不用这么麻烦了,idea自动给你把一切创建好)
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--这里就是配置所有管理对象的地方了-->
<!-- <bean> 配置需要创建的对象
id属性 :用于之后从spring容器中获取使用
class属性 :被管理对象的包名,spring通过包名来创建对象,需要完整的类名
<bean id="user" class="com.minato.bean.User" />
-->
<!-- 标签解释 -->
<!--name与id作用相同,不支持特殊字符,不能重复-->
<!--name是取一个名字,可以在代码中用name利用容器来获取对象
name可以使用特殊字符
name可以重复
不推荐使用多个重复name对象
-->
<bean id="user" class="com.minato.bean.User" />
</beans>
测试:接下来需要创建一下我们跟配置文件绑定的实体类
public class User {
private Integer uId;
private String uPassWord;
private String uName;
public Integer getuId() {
return uId;
}
public void setuId(Integer uId) {
this.uId = uId;
}
public String getuPassWord() {
return uPassWord;
}
public void setuPassWord(String uPassWord) {
this.uPassWord = uPassWord;
}
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
@Override
public String toString() {
return "User{" +
"uId=" + uId +
", uPassWord='" + uPassWord + '\'' +
", uName='" + uName + '\'' +
'}';
}
}
再创建一个测试类
//普通通过new创建对象
@Test
public void Test()
{
User user=new User();
user.setuId(1);
System.out.println(user);//User{uId=1, uPassWord='null', uName='null'}
}
//IOC的反转:创建对象工作交给spring执行
//IOC的控制:由Spring负责创建销毁对象,掌控对象生命周期等,需要使用对象时向spring申请即可(不再通过new创建)
//IOC是一种编程思想;需要DI(依赖注入)的支持
//DI(依赖注入):通过配置将变量初始化/赋值
//spring是一个容器,管理对象beans
@Test
public void Test()
{
//根据spring配置文件获取容器对象。用ApplicationContext将配置文件引入
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//读取applicationContext.xml文件中的bean-->id
//通过getBean获取配置好的实体类对象(返回的是Object类型,需要强制转换)
User user=(User) context.getBean("user");
//User user=context.getBean(User.class);
System.out.println(user);//User{uId=null, uPassWord='null', uName='null'}
}
初步搭建就完成了。
下面是更全面的介绍spring配置文件的问题
DI(依赖注入)
需要的实体类User(上面已有)、User1、Pet、MultiParameter
public class User1 {
private Integer uId;
private String uPassWord;
private String uName;
private Pet pet;
public User1() {
System.out.println("空参构造");
}
public User1(Integer uId, String uPassWord, String uName, Pet pet) {
this.uId = uId;
this.uPassWord = uPassWord;
this.uName = uName;
this.pet = pet;
}
public Integer getuId() {
return uId;
}
public void setuId(Integer uId) {
this.uId = uId;
}
public String getuPassWord() {
return uPassWord;
}
public void setuPassWord(String uPassWord) {
this.uPassWord = uPassWord;
}
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
@Override
public String toString() {
return "User1{" +
"uId=" + uId +
", uPassWord='" + uPassWord + '\'' +
", uName='" + uName + '\'' +
", pet=" + pet +
'}';
}
}
public class Pet {
private String petName;
private String petType;
public String getPetName() {
return petName;
}
public void setPetName(String petName) {
this.petName = petName;
}
public String getPetType() {
return petType;
}
public void setPetType(String petType) {
this.petType = petType;
}
@Override
public String toString() {
return "Pet{" +
"petName='" + petName + '\'' +
", petType='" + petType + '\'' +
'}';
}
}
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class MultiParameter {
//Array
private Object[] array;
//List
private List list;
//set
private Set set;
//Map
private Map map;
//properties
private Properties properties;
@Override
public String toString() {
return "MultiParameter{" +
"map=" + map +
", properties=" + properties +
'}';
}
public Object[] getArray() {
return array;
}
public void setArray(Object[] array) {
this.array = array;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
配置文件
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 普通数据:<value>
引用数据:<ref>-->
<!--set注入值类型-->
<bean id="user" class="com.minato.bean.User">
<property name="uId" value="1"></property>
<property name="uPassWord" value="1"></property>
<property name="uName" value="自来也"></property>
</bean>
<bean id="pet" class="com.minato.bean.Pet">
<property name="petName" value="九尾"></property>
<property name="petType" value="尾兽"></property>
</bean>
<!-- 构造函数注入 -->
<bean id="user1" class="com.minato.bean.User1">
<constructor-arg name="uId" value="2"></constructor-arg>
<constructor-arg name="uPassWord" value="2"></constructor-arg>
<constructor-arg name="uName" value="水门"></constructor-arg>
<!-- bean引入,ref -->
<constructor-arg name="pet" ref="pet"></constructor-arg>
</bean>
<!-- 复杂类型注入 -->
<bean id="MultiParameter" class="com.minato.bean.MultiParameter">
<property name="array">
<array>
<value>1</value>
<value>abc</value>
<ref bean="pet"></ref>
</array>
</property>
<property name="list">
<list>
<value>123</value>
<value>cba</value>
<ref bean="user"></ref>
</list>
</property>
<property name="set">
<set>
<value>3</value>
<value>asd</value>
<ref bean="user1"></ref>
</set>
</property>
<property name="map">
<map>
<entry key="小米9" value="2999"/>
<entry key-ref="user1" value-ref="pet"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="userName">1</prop>
<prop key="userPwd">1</prop>
</props>
</property>
</bean>
</beans>
测试
@Test
public void Test1()
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User user =(User) context.getBean("user");
System.out.println(user);//User{uId=1, uPassWord='1', uName='自来也'}
}
@Test
public void Test2()
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
User1 user1 =(User1) context.getBean("user1");
System.out.println(user1);//User1{uId=2, uPassWord='2', uName='水门', pet=Pet{petName='九尾', petType='尾兽'}}
}
@Test
public void Test3()
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
MultiParameter multiParameter=context.getBean(MultiParameter.class);
System.out.println(multiParameter);//MultiParameter{array=[1, abc, Pet{petName='九尾', petType='尾兽'}], list=[123, cba, User{uId=1, uPassWord='1', uName='自来也'}], set=[3, asd, User1{uId=2, uPassWord='2', uName='水门', pet=Pet{petName='九尾', petType='尾兽'}}],
//map={小米9=2999, User1{uId=2, uPassWord='2', uName='水门' ,pet=Pet{petName='九尾', petType='尾兽'}}=Pet{petName='九尾', petType='尾兽'}}, properties={userPwd=1, userName=1}}
}
Spring注解配置
加入注解就不需要重复在配置文件里添加对应的bean了
@Controller(“类名”)//对应web层
@Service(“类名”)//service层
@Repository(“类名”)//dao层
@Value(value = “1”)可加在属性上,也可以加载set方法上;缺点:破坏封装性,推荐在set方法上使用
@Autowired自动装配,读取配置文件中的bean,多个同名类会影响
@Repository(“类名”)//dao层
@Resource(name = “dog”)
src目录下新建applicationContext_annotation.xml
导入新的jar包
需要引入对应的约束
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
<?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: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">
<!-- 开启组件扫描,扫描包下含有注解的类 -->
<context:component-scan base-package="com.minato.bean"></context:component-scan>
<bean name="monster" class="com.minato.bean.Pet">
<property name="petType" value="通灵兽"></property>
<property name="petName" value="蛞蝓"></property>
</bean>
</beans>
再创建一个新的实体类做测试,在实体类里加入注解@Component(“user”) ,作用等同于配置文件中的
<bean name="user" class="com.minato.bean.User"/>
User2类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component("user2")
public class User2 {
private Integer uId;
private String uPassWord;
private String uName;
private Pet pet;
public User2() {
System.out.println("User2 空参构造");
}
public Pet getPet() {
return pet;
}
//@Autowired
@Resource(name = "monster")
public void setPet(Pet pet) {
this.pet = pet;
}
public Integer getuId() {
return uId;
}
public void setuId(Integer uId) {
this.uId = uId;
}
public String getuPassWord() {
return uPassWord;
}
public void setuPassWord(String uPassWord) {
this.uPassWord = uPassWord;
}
public String getuName() {
return uName;
}
@Value("鸣人")
public void setuName(String uName) {
this.uName = uName;
}
@Override
public String toString() {
return "User2{" +
"uId=" + uId +
", uPassWord='" + uPassWord + '\'' +
", uName='" + uName + '\'' +
", pet=" + pet +
'}';
}
}
执行测试
@Test
public void Test4()
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext_annotation.xml");
User2 user2=context.getBean(User2.class);
System.out.println(user2);//User2 空参构造
//User2{uId=null, uPassWord='null', uName='鸣人', pet=Pet{petName='蛞蝓', petType='通灵兽'}}
}
使用JUnit测试,创建容器
需要新的jar包
在applicationContext.xml中引入applicationContext_annotation.xml
<import resource="applicationContext_annotation.xml"></import>
创建新的测试类test_Junit
使用注解@ContextConfiguration就不需要再通过ApplicationContext接口每次创建spring容器测试功能
import com.minato.bean.Pet;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)//使用JUnit测试,创建容器
//@ContextConfiguration("/applicationContext_annotation.xml")
//选择配置文件
@ContextConfiguration("/applicationContext.xml")
public class test_Junit {
@Resource(name = "dog")
private Pet pet;
@Test
public void Test()
{
// ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext_annotation.xml");
// User user=context.getBean(User.class);
System.out.println(pet);//Pet{petName='蛞蝓', petType='通灵兽'}
}
}
通过Spring框架可以省去很多通过代码来创建各种类的麻烦,作为一个容器,系统的管理了各种需要用到的类。
但是上面所述的每次都需要通过ApplicationContext接口来创建一个Spring容器,实际开发中是不可取的。所以需要在web.xml中再进行初始化创建,下一篇会介绍~~~~~~~传送门:https://blog.csdn.net/qq_38139418/article/details/110236052