简介
文档
导入依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.9</version>
</dependency>
优点
- Spring是一个开源免费的框架
- Spring是一个轻量级、非入侵式的框架
- 控制翻转 IOC、面向切面编程AOP
- 支持事务处理,对框架整合的支持
缺点
- 由于过度集成,配置十分繁琐
Spring是一个轻量级的面向切面、控制翻转的框架
组成
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Dfj8RY1G-1645076688557)(figures/Spring/image-20210815125152603.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lkhbuQyK-1645076688558)(figures/Spring/image-20210815125307226.png)]
IOC
略
第一个Spring程序
源文件
package com.rjx.pojo;
import lombok.Data;
@Data
public class Hello {
private String str;
}
配置文件
<?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代表一个对象-->
<bean id="hello" class="com.rjx.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
<!-- more bean definitions go here -->
</beans>
测试文件
package com.rjx.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
@Test
public void test1(){
//获取Spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello)context.getBean("hello");
System.out.println("hello");
}
}
IOC创建对象的方式
- 使用无参构造方法创建对象 默认方法
- 使用有参构造,但此时需要在配置中声明
Spring配置
别名
<alias name="origin name" alias="alias"/>
Bean配置
- id:bean的唯一标识符
- class:bean对象的类名
- name:相当于id的别名 可以通过逗号、空格、分号等符号分割取多个别名
作用域
- 按对象个数分类
- singleton:全局唯一 默认为单例
- prototype:每个原型一个
- MVC中
Import
一般用于团队开发,可以将多个配置文件导入合并为一个配置文件
语法
<import resource="resource path"/>
依赖注入
实体类
package com.rjx.pojo;
import lombok.Data;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@Data
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private Properties info;
private String wife;
}
package com.rjx.pojo;
import lombok.Data;
@Data
public class Address {
private String address;
}
测试类
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student);
}
构造器注入
见第一个Spring程序
注入方式
值注入
配置文件
<property name="name" value="passnight"/>
Bean注入
配置文件
<bean id="address" class="com.rjx.pojo.Address">
</bean>
<property name="address" ref="address"/>
数组注入
配置文件
<property name="books">
<array>
<value>book1</value>
<value>book2</value>
<value>book3</value>
<value>book4</value>
</array>
</property>
List注入
配置文件
<property name="hobbies">
<list>
<value>hobby 1</value>
<value>hobby 2</value>
<value>hobby 3</value>
<value>hobby 4</value>
<value>hobby 5</value>
</list>
</property>
字典注入
配置文件
<property name="card">
<map>
<entry key="card-key1" value="card-value1"/>
<entry key="card-key2" value="card-value2"/>
<entry key="card-key3" value="card-value3"/>
<entry key="card-key4" value="card-value4"/>
</map>
</property>
集合注入
配置文件
<property name="games">
<set>
<value>game1</value>
<value>game2</value>
<value>game3</value>
<value>game4</value>
</set>
</property>
空值注入
配置文件
<property name="wife">
<null/>
</property>
properties注入
配置文件
<property name="info">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
<prop key="key4">value4</prop>
</props>
</property>
c命名注入
通过构造器注入
<?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:P="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 一个bean代表一个对象-->
<bean id="user" class="com.rjx.pojo.User" C:age="18" C:name="name"/>
<!-- more bean definitions go here -->
</beans>
p命名注入
<?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:P="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 一个bean代表一个对象-->
<bean id="user" class="com.rjx.pojo.User" P:age="18" P:name="name"/>
<!-- more bean definitions go here -->
</beans>
Bean自动装配
- 自动装配是Spring满足bean以来的一种方式
- Spring会在上下文寻找,并给bean装配属性
三种装配方式
- 在xml中显示配置
- 在java中显示配置
- 隐式自动装配
非自动装配
package com.rjx.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
public class Dog {
public void shout(){
System.out.println("a dog shout");
}
}
package com.rjx.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
public class Cat {
public void shout(){
System.out.println("a cat shout");
}
}
package com.rjx.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private Cat cat;
private Dog dog;
private String name;
}
<?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="cat" class="com.rjx.pojo.Cat"/>
<bean id="dog" class="com.rjx.pojo.Dog"/>
<bean id="person" class="com.rjx.pojo.Person">
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
<property name="name" value="name"/>
</bean>
</beans>
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("animals.xml");
Person person = context.getBean("person", Person.class);
System.out.println(person);
person.getCat().shout();
person.getDog().shout();
}
自动装配
By Name自动装配
<?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="cat" class="com.rjx.pojo.Cat"/>
<bean id="dog" class="com.rjx.pojo.Dog"/>
<bean id="person" class="com.rjx.pojo.Person" autowire="byName">
<property name="cat" ref="cat"/>
</bean>
</beans>
By Type自动装配
<?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="cat" class="com.rjx.pojo.Cat"/>
<bean id="dog" class="com.rjx.pojo.Dog"/>
<bean id="person" class="com.rjx.pojo.Person" autowire="byType">
<property name="cat" ref="cat"/>
</bean>
</beans>
使用注解自动装配
前提
- 导入约束
- 配置注解支持
使用
package com.rjx.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="dog" class="com.rjx.pojo.Dog"/>
<bean id="cat" class="com.rjx.pojo.Cat"/>
<bean id="person" class="com.rjx.pojo.Person"/>
</beans>
@AutoWired可以忽略set方法使用,也可以使用在set方法上, 但只能通过byname实现
找不到name, 可以使用@Qualifier()指定装配id
也可以使用@Resource(name=“name value”, 且同时可以byname和bytype)
使用注解开发
自动装配注解
配置
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<!-- 指定扫描这个包,这个包下的组件就会生效(@Component自动生效)-->
<context:component-scan base-package="com.rjx.pojo"/>
</beans>
类
@Data
//相当于<bean id="小写" class="反射获取"/>
@Component
//设置作用域
@Scope("singleton")
public class User {
//相当于bean下的property下的value
@Value("passnight")
private String name;
}
测试
package com.rjx.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
}
}
Dao层的通常使用@Repository代替@Component
Service层的通常使用@Service代替@Component
Controller层的通常使用@Controller代替@Component
基于java的开发
配置
package com.rjx.config;
import com.rjx.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config {
@Bean
public User getUser(){
return new User();
}
}
源文件
package com.rjx.pojo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Data
@Component
public class User {
@Value("passnight")
private String name;
}
测试文件
@Test
public void test1(){
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
System.out.println(context.getBean("getUser"));
}
bean id 与配置文件当中@Bean的方法名相同
相关参数
@ComponentScan("path") 相当于<context:component-scan base-package="path"/>
@Import(Config.class) 相当于 <import resource="c.xml"/>
ringframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Data
@Component
public class User {
@Value(“passnight”)
private String name;
}
## 测试文件
```java
@Test
public void test1(){
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
System.out.println(context.getBean("getUser"));
}
bean id 与配置文件当中@Bean的方法名相同
相关参数
@ComponentScan("path") 相当于<context:component-scan base-package="path"/>
@Import(Config.class) 相当于 <import resource="c.xml"/>