Spring简介
文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#spring-core
- Spring是一个免费开源的框架(容器)
- 轻量级非入侵式的框架
- 控制翻转(IOC)、面向切面编程(AOP)
- 对框架整合的支持
理解:
- 对象交给spring管理
Spring配置
依赖注入
- 方式一:构造器注入
- 方式二:set注入(重点)
- 方式三:拓展方式
代码
导入依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
pojo
Address.java
package com.wangqi.pojo;
public class Address {
private String address;
public Address() { }
public Address(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
User.java
package com.wangqi.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String, String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
配置文件xml
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">
<!-- 有参构造,可以通过位置、类型、参数名 -->
<bean id="address" class="com.wangqi.pojo.Address">
<constructor-arg name="address" value="China"/>
</bean>
<!-- 无参构造 -->
<!--<bean id="address" class="com.wangqi.pojo.Address">
<property name="address" value="China"></property>
</bean>-->
<bean id="student" class="com.wangqi.pojo.Student">
<!-- 第一种,普通值注入,value -->
<property name="name" value="小王"/>
<!-- 第二种,Bean注入,ref -->
<property name="address" ref="address"/>
<!-- 数组注入,-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!-- list注入 -->
<property name="hobbys">
<list>
<value>听歌</value>
<value>写代码</value>
<value>看电影</value>
</list>
</property>
<!-- map注入 -->
<property name="card">
<map>
<entry key="卡号" value="123456"/>
<entry key="密码" value="654321"/>
</map>
</property>
<!-- set注入 -->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!-- 空值注入 -->
<!--<property name="wife" value=""/>-->
<!-- null值注入 -->
<property name="wife">
<null/>
</property>
<!-- Properties -->
<property name="info">
<props>
<prop key="学号">E11614095</prop>
<prop key="性别">男</prop>
<prop key="账号">admin</prop>
<prop key="密码">123456</prop>
</props>
</property>
</bean>
</beans>
其他配置:
- alias:别名(没什么用)
- import:导入其他的applicationContext.xml,一般将多个合并成一个总的,导入这个总的就可以
测试类
MyTest.java
import com.wangqi.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext conntext = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) conntext.getBean("student");
System.out.println(student);
}
}
bean scope(bean的作用域)
- 单例模式(默认)
<bean id="student" class="com.wangqi.pojo.Student" scope="singleton">
- 原型模式
每次getBean时候都会产生一个新对象!
<bean id="student" class="com.wangqi.pojo.Student" scope="prototype">
其余的几种,只能在web开发中使用到。