Spring框架
优点
- 是一个开源的免费框架
- 是一个轻量级的,非入侵的框架
- 控制反转(IOC),面向切面编程(AOP)
- 支持事务的处理
- 想学springboor和springcloud就必须学会spring和springMVC作为基础
一. IOC的理解 (对象由spring来创建,管理,支配)
- 个人理解 控制反转:就是依赖对象的方式变了,原来是程序员来控制服务的实现,现在是由用户来实现,整体结构没有变!
- 不用再new对象了,全都交给spring去创建
- 要实现不同的操作,只需要在xml配置文件中改动即可
无参构造只能用有参干掉
1. DI 依赖注入
- 构造器注入
- set方式注入(重点)
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象的所有属性由容器注入
2. 第一个spring程序的注入
1. 创建一个正常的Maven项目
2. 导包:导入spring-webmvc的包即可,因为此包含有所有spring所用的基础包
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
</dependencies>
3. 编写实体类 学生类和作为实体类注入对象的Address类
学生类
package com.wang.pojo;
import java.util.*;
/**
* @author Charlie Puth
* @version 1.0
**/
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobby;
private Map<String,String> card;
private Set<String> games;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobby=" + hobby +
", card=" + card +
", games=" + games +
'}';
}
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> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
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;
}
}
对象类:
package com.wang.pojo;
/**
* @author Charlie Puth
* @version 1.0
**/
public class Address {
private String address;
public String getAddress() {
return address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
public void setAddress(String address) {
this.address = address;
}
}
4. 编写bean.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-4.3.xsd">
<bean id="address" class="com.wang.pojo.Address">
<property name="address" value="浙江大学"></property>
</bean>
<bean id="student" class="com.wang.pojo.Student">
1.普通注入
<property name="name" value="Charlie"></property>。
2. 对象注入
<property name="address" ref="address"></property>
3.数组注入
<property name="books">
<array>
<value>红楼梦</value>
<value>水浒传</value>
<value>西游记</value>
<value>三国演义</value>
</array>
</property>
4.list注入
<property name="hobby">
<list>
<value>听儿歌</value>
<value>打小孩</value>
<value>吃棒棒糖。。。</value>
</list>
</property>
5.map注入
<property name="card">
<map>
<entry key="身份证" value="18985534846"></entry>
<entry key="银行卡" value="48486498"></entry>
</map>
</property>
6.set集合注入
<property name="games">
<set>
<value>LOL</value>
<value>TiMi</value>
</set>
</property>
</bean>
</beans>
5.测试类
import com.wang.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author Charlie Puth
* @version 1.0
**/
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Student student = (Student) context.getBean("student");
System.out.println(student);
}
}
3. Bean标签的常用配置
- id是指该bean的唯一标识符
- class是指bean对象对的限定名:包名加类名
- import可以将多个bean整合到一个bean中去
- alias是起别名的意思
- name也是起别名
4.spring默认是单例模式,原型模式每次从容器中获取的时候就会产生一个新的对象
5. Bean的自动装配
- xml中显示配置
- 在java中显示配置
- 隐式的自动装配【重要】
手动装配
autowire 自动装配分为
ByName 根据bean的id 要保证id唯一
ByType 根据对象属性相同的bean 要保证class唯一
选用后者时可以省略id
使用注解自动装配
- 需要导入约束
- 在实体类属性前添加@Autowired注解即可
- Autowired先按照类型注入,在按照名字注入
- 若@Autowired(required=false)则说明此属性可以为null
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
<context:annotation-config></context:annotation-config>
6.使用注解开发
bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
开启注解扫描
<context:component-scan base-package="com.wang.pojo"></context:component-scan>
<context:annotation-config></context:annotation-config>
</beans>
实体类
@Component
public class User {
public String name = "Charlie";
}
测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
User user = context.getBean("user", User.class);
System.out.println(user.name);
}
}
注意知识点:
- xml方式要优于注解方式,因为它更全能,更具有灵活性
- 注解方式只负责注入属性,还是很方便的
- 使用注解时一定要开启注解扫描!!
java方式配置spring
当采用此种方式时
实体类
package com.wang.pojo;
import org.springframework.beans.factory.annotation.Value;
/**
* @author Charlie Puth
* @version 1.0
**/
public class User {
private String name;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
public String getName() {
return name;
}
@Value("Charlie")
public void setName(String name) {
this.name = name;
}
}
config类
@Configuration
public class SpringConfig {
@Bean
public User user(){
return new User();
}
}
测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}
注意new的时候用AnnotationConfigApplicationContext
而不是
ClassPathXmlApplicationContext
- @Configuration表示该类为配置类
- @Bean相当于bean标签