示例 依赖 <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.6.RELEASE</version> <relati.... Spring Boot入门 Java
BeanFactoryPostProcessor BeanFactoryPostProcessor:BeanFactory的后置处理器,在BeanFactory标准初始化之后调用,所有的Bean定义已经保存加载到BeanFactory,但是Bean的实例还未创建 执行时机: (1)IOC创建容器对象 (2)invokeBeanFactoryPostProcessors(beanFactory) -- 执行BeanFactoryProcessor 如何找到所有的BeanFactoryProcessor? 直接在BeanFactory中找到所有类型是BeanFactoryPostProcessor的组件,并执行他们的方法 在初始化创建其他组件之前执行 @Component public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableLi.... Spring注解驱动开发拓展 Spring
简单使用 1.导入相关依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.6.RELEASE</version> </dependency> 2.配置数据源、JdbcTemplate(Spring提供的简化数据库操作工具) @Bean public DataSource dataSource() throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setUser("root"); dataSource.setPassword("458974tl"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?useSSL=f.... Spring注解驱动开发之声明式事务 Java
AOP指在程序运行期间动态地将某段代码切入到指定位置进行运行的编程方式 -- 动态代理 简单使用 步骤: 1.导入AOP模块:Spring AOP -- spring-aspects 2.定义一个业务逻辑类,运行期间打印日志 3.定义一个日志切面类 通知方法: (1)前置通知(@Before):在目标方法运行之前运行 (2)后置通知(@After):在目标方法运行之后运行(无论方法正常结束还是异常结束) (3)返回通知(@AfterReturning):在目标方法正常返回之后运行 (4)异常通知(@AfterThrowing):在目标方法出现异常以后运行 (5)环绕通知(@Around):动态代理,手动推进目标方法就行 4.给切面类的目标方法标注何时何地运行(通知注解) 5.将切面类和业务逻辑类都加入到容器中 6.告诉Spring哪个类是切面类 -- 给切面类加上 @Aspect 注解 7.开启AspectJAutoProxy -- 在配置类加上 @EnableAspectJAutoProxy 注解 //配置类 @Configuration @EnableAspectJAutoProx.... Spring注解驱动开发之AOP Java
传统配置文件 (1)编写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"> <!-- 包扫描,只要标注了@.... Spring注解驱动开发 Java
核心包:beans、context、core、expression 日志包:com.springsource.org.apache.commons.logging-1.1.1 老版要求的包:com.springsource.org.apache.log4j-1.2.15 配置文件 applicationContext.xml(位置和名称任意,建议用这个并放在src下) <?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"> <!-- 配置对象 --> <.... Spring Spring
点击 springinit-> 到最后选择 Spring Web,spring2.1 以上的版本从 Web 选项变成了 Spring Web pom 文件 <dependencies> <!-- web启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId&.... SpringBoot SpringBoot