1.Spring概述
Spring官网
Spring,顾名思义,java软件行业的春天,彻底解放了程序员从原生的开发中,一个轻量级的非侵入式的框架。
特点:控制反转 IoC , 面向切面 Aop
组成
组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下:
2.IoC基础
2.1 Spring创建一个入门程序
之前的mybatis的父工程SSM添加新的子工程Spring。
之前的pom是
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.12.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
1.引入Spring的maven仓库
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
spring-webmvc我引人spring别的相关依赖,我们引入个最大的就行。
2.写一个hello类
package com.liu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hello {
private String name;
public void show(){
System.out.println("Hello,"+ name );
}
}
3.编写Spring核心配置文件beans.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就是java对象 , 由Spring创建和管理-->
<bean id="hello" class="com.liu.pojo.Hello">
<property name="name" value="hello world"/>
</bean>
</beans>
我们将hello这个类交给spring管理,id代表着唯一标识,class包名
property 代表属性注入,在项目启动得时候就给Hello 类中得name属性赋值hello world
3.测试
@Test
public void test(){
//解析beans.xml文件 , 生成管理相应的Bean对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//getBean : 参数即为spring配置文件中bean的id .
Hello hello = context.getBean("hello",Hello.class);
hello.show();
}
运行结果
以上可以看出,我们不需要new一个对象,初始化,仅仅是在配置文件交给Spring托管即可,在程序运行过程中getBean得方式即可获得对象。
2.2 Spring配置详解
1.alias 设置别名 , 为bean设置别名 , 可以设置多个别名
<!--设置别名:在获取Bean的时候可以使用别名获取-->
<alias name="hello" alias="shuxiansheng"/>
这样我们通过别名依旧可以取到bean的值
2. Bean的配置
id 是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
如果配置id,又配置了name,那么name是别名
name可以设置多个别名,可以用逗号,分号,空格隔开
如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;
class是bean的全限定名=包名+类名
3. import
<import resource="{path}/beans.xml"/>
2.3 依赖注入(DI)+ 自动装配
依赖注入
这块我们工作都是注解 配置这块后面遇到再看。
3.使用注解开发
因为之后我们基本上都是SpringBoot开发,放弃了繁琐得配置,都是纯注解得方式,这里我们也用注解得方式进行开发。
3.1 属性注入
1.我们删除之前得bean.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"
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.liu.pojo"/>
<!-- 开启注解支持-->
<context:annotation-config/>
</beans>
加上要扫描得包和开启注解得配置。
2.bean属性如何注入-Component
我们原来得Hello类在配置文件中属性注入得,现在只需要在类上加Component注解
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class Hello {
@Value("刘涛")
private String name;
public void show(){
System.out.println("Hello,"+ name );
}
}
3.测试
@Test
public void test(){
//解析beans.xml文件 , 生成管理相应的Bean对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
//getBean : 参数即为spring配置文件中bean的id .
Hello hello = context.getBean("hello",Hello.class);
hello.show();
}
注意这里getBean是默认类名得小写
我们也可以像id一样命名。
@Component("test")
这样
Hello hello = context.getBean("test",Hello.class);
修改着两块得代码结果一样
3.2 衍生注解
@Component三个衍生注解
自动装配注解@Autowired
@Autowired是按类型自动转配的,不支持id匹配。
@Resource(name = “name2”)不能单独使用,只能配合Autowired一起使用。
3.3基于Java类进行配置@Configuration
我们完全脱离配置文件,进行纯注解开发。
我们新建一个MyConfig配置类,放在com.liu.config包下
@Configuration //代表这是一个配置类
@ComponentScan("com.liu")
public class MyConfig {
@Bean //通过方法注册一个bean,这里的返回值就Bean的类型,方法名就是bean的id!
public Hello helo(){
return new Hello();
}
}
@Configuration代表这 是一个配置文件
@ComponentScan配置要扫描包得路径,相当于 <context:component-scan base-package=“com.liu.pojo”/>
测试类
@Test
public void test(){
ApplicationContext applicationContext =
new AnnotationConfigApplicationContext(MyConfig.class);
//getBean : 参数即为spring配置文件中bean的id .
Hello hello = applicationContext.getBean("test",Hello.class);
hello.show();
}
这样我们没有配置文件,纯注解开发,也可以正常输出。
完成IOC控制反转,所有扫描得包都托管给spring,当然是值加上Component注解得类。