Spring
Spring
Spring概述
Spring 是最受欢迎的企业级 Java 应用程序开发框架,Spring 框架来创建性能好、易于测试、可重
用的代码。
Spring 框架是一个开源的 Java 平台,它最初是由 Rod Johnson 编写的,并且于 2003 年 6 月首次
在Apache 2.0 许可下发布。
Spring 是轻量级的框架。
三层架构
- A 表现层 web层 MVC是表现层的一个设计模型
- B 业务层 service层
- C 持久层 dao层
Spring 的优良特性
- 非侵入式:基于Spring开发的应用中的对象可以不依赖于Spring的API
- 控制反转:IOC——Inversion of Control,指的是将对象的创建权交给 Spring 去创建。使用 Spring 之前,对象的创建都是由我们自己在代码中new创建。而使用 Spring 之后。对象的创建都是给了 Spring 框架。
- 依赖注入:DI——Dependency Injection,是指依赖的对象不需要手动调用 setXX 方法去设置,而是通过配置赋值。
- 面向切面编程:Aspect Oriented Programming——AOP
- 容器:Spring 是一个容器,因为它包含并且管理应用对象的生命周期
- 组件化:Spring 实现了使用简单的组件配置组合成一个复杂的应用。在 Spring 中可以使用XML和Java注解组合这些对象。
- 一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上 Spring 自身也提供了表现层的 SpringMVC 和持久层的 Spring JDBC)
Spring框架特点
- 方便解耦,简化开发
- 方便集成各种优秀框架
- 降低Java EE API的使用难度
- 方便程序测试
- AOP编程的支持
- 声明式事务的支持
Spring的使用
创建Maven,在pom.xml导入spring
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jsoft</groupId>
<artifactId>spring20220920</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.22</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>
创建User类,Address类
@Data
@NoArgsConstructor
public class User {
private String name;
private Address address;
private String[] hobbies;
private List<String> duties;
private Set<String> carts;
private Map<String,String> map;
private Properties properties;
public User(String name) {
this.name = name;
}
public void say() {
System.out.println("Hello:" + name);
}
public User(String name, Address address) {
this.name = name;
this.address = address;
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Address {
private String info;
}
resource下创建application.xml
把对象放进容器中,对象一旦放入容器中,整个对象在当前项目就可以共享,并且是单例的
id:这个对象在IOC容器中唯一标识
class:要放到spring的IOC容器的全类名
在这里User这个类的对象放进了spring的IOC容器,交给了spring管理User类的对象
这个user对象实际上就是通过调用User类的无参构造器创建出来的
属性注入:
1、构造器注入:根据参数名,根据索引位置,根据参数类型
2、setter注入:调用set方法来进行属性注入(常用)
如果我们的bean要注入的属性也是一个对象,那这个对象也要先注册到IOC容器中
align:对齐
ref:依赖,当使用ref属性,依赖于当前容器中已经存在的bean,进行属性的注入
scope:定义bean的作用域
- prototype:原型
- singleton:单例(默认)
- request:一次请求
- session:一次会话
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<bean id="address" class="com.jsoft.entity.Address" scope="singleton">
<property name="info" value="高新区"></property>
</bean>
<bean id="address1" class="com.jsoft.entity.Address" scope="singleton">
<property name="info" value="南关区"></property>
</bean>
<bean id="user" class="com.jsoft.entity.User">
<constructor-arg value="jsoft" name="name"></constructor-arg>
<property name="name" value="JSOFT"></property>
<property name="address" ref="address"></property>
<property name="hobbies">
<array value-type="java.lang.String">
<value>篮球</value>
<value>足球</value>
</array>
</property>
<property name="duties">
<list value-type="java.lang.String">
<value>董事长</value>
<value>总经理</value>
</list>
</property>
<property name="carts">
<set value-type="java.lang.String">
<value>韭菜</value>
<value>鸡蛋</value>
</set>
</property>
<property name="map">
<map>
<entry key="父亲" value="马云"></entry>
<entry key="母亲" value="张瑛"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="阿里巴巴">达摩院</prop>
</props>
</property>
</bean>
</beans>
测试类
public class UserTest {
@Test
public void IOCTest() {
// 加载spring的配置文件
ApplicationContext ioc =
new ClassPathXmlApplicationContext("application.xml");
// 这个user对象是从ioc容器中获取到的
// User user = (User) ioc.getBean("user");
// User user1 = (User) ioc.getBean("user");
// user.setName("羊了个羊");
User user = ioc.getBean(User.class);
user.say();
System.out.println(user);
// System.out.println(user == user1); // true
}
}
我们希望能在java里做的事情最好不要在别的里做,并且上面的方式会把属性写死,所以引入了注解
注解
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启自动装配注解 -->
<context:component-scan base-package="com.jsoft"></context:component-scan>
</beans>
autowire:自动装配
byName:根据属性名去IOC容器中找名字相同的bean进行自动注入
byType:根据属性的类型自动注入
@Autowired
自动装配,注入,根据name
@Qualifier("address")
注入不同的name
<bean id="address" class="com.jsoft.entity.Address" scope="singleton">
<property name="info" value="高新区"></property>
</bean>
<bean id="address1" class="com.jsoft.entity.Address" scope="singleton">
<property name="info" value="南关区"></property>
</bean>
上面两个由spring提供,除这两个之外还有一个@Resource) 由JDK提供
@Component或使用配置类
把对象放进IOC容器:
1、使用@Component,对象没有初始状态
2、使用配置类,对象有初始值
Spring中,把一个类放进IOC容器:
* 1、Component
* 2、Controller
* 3、Service
* 4、Repository
* 5、Configuration
/*
配置类
*/
@Configuration
public class Beans {
/*
* 方法名就是id
* */
@Bean
public Address address() {
return new Address("绿园区");
}
@Bean
public User user(Address address) {
return new User("张三",address);
}
}
@Data
@NoArgsConstructor
// @Component
public class User {
private String name = "admin";
// 引用数据类型,地址
@Autowired
// @Qualifier("address1") // 上面两个spring提供
// @Resource // jdk提供
private Address address;
private String[] hobbies;
private List<String> duties;
private Set<String> carts;
private Map<String,String> map;
private Properties properties;
}
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启自动装配注解 -->
<context:component-scan base-package="com.jsoft"></context:component-scan>
</beans>

浙公网安备 33010602011771号