Spring是一个开源的轻量级Java开发框架,旨在简化企业级应用开发。它最初由Rod Johnson在2003年创建,现由Pivotal团队维护。
核心特点:
主要模块:
Spring官方发布的压缩包主要包含以下目录结构:
在pom.xml
中添加Spring依赖:
org.springframework
spring-context
5.3.9
org.springframework
spring-webmvc
5.3.9
org.springframework
spring-jdbc
5.3.9
在build.gradle
中添加Spring依赖:
implementation 'org.springframework:spring-context:5.3.9'
implementation 'org.springframework:spring-webmvc:5.3.9'
implementation 'org.springframework:spring-jdbc:5.3.9'
在src/main/resources
下创建applicationContext.xml
:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
// XML方式
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 注解方式
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
控制反转是一种设计原则,将传统上由应用程序代码直接控制的对象的创建和管理权,转移给外部容器或框架。在Spring中,IoC容器负责创建、配置和管理对象及其之间的依赖关系。
核心优势:
依赖注入是IoC的一种实现方式,由容器将依赖关系注入到组件中:
Spring提供两种IoC容器:
BeanFactory:
ApplicationContext:
ApplicationContext主要实现类:
ClassPathXmlApplicationContext
:从类路径加载配置文件FileSystemXmlApplicationContext
:从文件系统加载配置文件AnnotationConfigApplicationContext
:基于Java注解配置WebApplicationContext
:用于Web应用的上下文最常用的方式,通过默认构造方法创建Bean:
或使用Java配置:
@Bean
public UserService userService() {
return new UserServiceImpl();
}
通过调用静态工厂方法创建Bean:
对应的工厂类:
public class ServiceFactory {
public static UserService createUserService() {
return new UserServiceImpl();
}
}
Java配置方式:
@Bean
public UserService userService() {
return ServiceFactory.createUserService();
}
通过调用实例工厂的方法创建Bean:
对应的工厂类:
public class ServiceFactory {
public UserService createUserService() {
return new UserServiceImpl();
}
}
Java配置方式:
@Bean
public ServiceFactory serviceFactory() {
return new ServiceFactory();
}
@Bean
public UserService userService() {
return serviceFactory().createUserService();
}
Bean的scope属性定义了Bean的作用域和生命周期:
@Bean
@Scope("singleton")
public UserService userService() {
return new UserServiceImpl();
}
@Bean
@Scope("prototype")
public UserService userService() {
return new UserServiceImpl();
}
@Bean
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public UserForm userForm() {
return new UserForm();
}
@Bean
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public UserPreferences userPreferences() {
return new UserPreferences();
}
@Bean
@Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public AppConfig appConfig() {
return new AppConfig();
}
通过构造函数参数注入依赖:
Java代码:
public class UserServiceImpl implements UserService {
private final UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
}
Java配置:
@Bean
public UserService userService(UserDao userDao) {
return new UserServiceImpl(userDao);
}
通过setter方法注入依赖:
Java代码:
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
Java配置:
@Bean
public UserService userService() {
UserServiceImpl service = new UserServiceImpl();
service.setUserDao(userDao());
return service;
}
直接在字段上使用@Autowired
注解:
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
}
通过特定接口实现依赖注入:
public interface DaoAware {
void setDao(Object dao);
}
public class UserServiceImpl implements UserService, DaoAware {
private UserDao userDao;
@Override
public void setDao(Object dao) {
this.userDao = (UserDao) dao;
}
}
让Spring自动解析依赖关系:
常用的自动装配模式:
byName
:根据属性名匹配BeanbyType
:根据属性类型匹配Beanconstructor
:根据构造参数类型匹配Bean静态代理是一种结构型设计模式,代理类和被代理类在编译时就已经确定。
1. 定义接口
public interface UserService {
void addUser(String username);
void deleteUser(String username);
}
2. 实现目标类
public class UserServiceImpl implements UserService {
@Override
public void addUser(String username) {
System.out.println("添加用户: " + username);
}
@Override
public void deleteUser(String username) {
System.out.println("删除用户: " + username);
}
}
3. 实现代理类
publi