通过一天的spring学习总结一下自己的学习内容
pom.xml配置
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.22</version>
</dependency>
使用spring-webmvc version 5.3.22版本的架包
初学者来说这一个架包已经可以满足
想要下载5.3.22 springframework全部可以到官网下载
配置好了之后载入架包就可以使用啦
写一个hello代码测试一下吧
首先是一个实体类
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
bean.xml文件开头复制过去 在设置自己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-3.0.xsd">
<!-- 使用spring 创建对象-->
<bean id="Hello" class="实体类位置">
<property name="str" value="Spring"/>
</bean>
</beans>
这样就可以测试自己的第一个spring的hello代码啦
public static void main(String[] args) {
//ioc 测试
/* //获取Spring的上下文对象
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Hello hello = (Hello) context.getBean("Hello");
System.out.println(hello.toString());*/
}
==第一个hello完成 ==
紧接着就是介绍一下ioc和di和autoVired
ioc是控制反转一个编程想法 在hello代码块中就可以体现出来 用户在需要修改需求的时候 只需要在bean.xml 进行修改就可以 不需要程序员修改代码来达到需求。
di 依赖注入 通过set进行注入
spring 提供了标签注入
提供 name=“hobbys” 对应set的
提供array提过给Strng[]
<array>
<value>英语</value>
<value>数学</value>
<value>语文</value>
</array>
提供list标签给List<>使用
<list>
<value>羽毛球</value>
<value>乒乓球</value>
<value>篮球</value>
</list>
提供map
<map>
<entry key="身份证" value="12121212"/>
<entry key="银行卡" value="23232323"/>
<entry key="学生卡" value="34343434"/>
</map>
还有 set props 各种各样的标签可以使用
autowired 自动装配功能了
<bean class="com.chen.pojo.autowired.cat"/>
<bean class="com.chen.pojo.autowired.dog"/>
<bean id="person" class="com.chen.pojo.autowired.person" autowire="byType">
<property name="name" value="shabi"/>
</bean>
<bean id="cat" class="com.chen.pojo.autowired.cat"/>
<bean id="dog" class="com.chen.pojo.autowired.dog"/>
<bean id="person" class="com.chen.pojo.autowired.person" autowire="byName">
<property name="name" value="shabi"/>
</bean>
autowire 自动装配提供了byName 和byType可以选择
byType 通过class进行装配的 在xml中只能有一个class类
byName 是通过id和set一致进行装配的 要达到id和set名称一致
在介绍一个import标签 这是一个多人开发bean.xml使用的标签
在通过import标签可以吧所有的bean.xml文件整合到一个文件 这样就可以实现多人开发的功能 引用的时候只需要调用总的xml就可以实现