对于spring来说最重要的两个特性就是老生常谈的IOC和AOP,这两个大哥先放一放。那我就先其中的一个重要小零件Bean来说,来看看spring是对Bean进行多牛逼的管理
=》1、Spring底层会调用类的构造方法来完成对象创建
(这里先不管他是怎么拿到构造方法的,反正就是拿构造方法,默认是无参构造器。这里其实有一个推断构造方法的过程)
1 稍微解释一下构造方法推断
多个构造方法,直接报错(没有构造器被找到)

解决方法在你需要的构造器上加上@Autowired指定spring用;因为spring比较笨所以你要教他做事
2 Spring的设计思想是这样的:
=》 2、对象依赖注入(属性赋值)
=》 3、初始化前 ()
=》 4、初始化
=》 5、初始化后(AOP)
=》 5.1 代理对象【只有当有AOP织入的时候,才会产生代理对象】
=》 6、bean
注:从源码的大的角度出发,就是先读取配置=》生成bean的定义信息(放到一个map里)=》按照bean的定义信息生成bean(也放到map里,要用的时候自取)
spring架构原理

// 扫描该包下的所有组件
@ComponentScan("com.xusj")
public class AppConfig {}
// 组件一、二如下
package com.xusj.future.service;import org.springframework.stereotype.Service;/*** @author xusj*
CreateDate 2022/11/26 22:59*/
@Service
public class DogService {
}
// 这里有一个点值得我们在以后业务需求中可以使用,当项目一启动你要给对应的bean属性赋值,implements InitializingBean 重写afterPropertiesSet,在初始化bean的时候就直接赋值了
package com.xusj.future.service;import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** @author xusj*
CreateDate 2022/11/26 22:58*/
@Service
public class PeopleService implements InitializingBean {@Autowiredprivate DogService dogService;private String initStr;public void testMethod() {System.out.println("111");}// 这里就是你想在bean创建的时候给他赋值implements InitializingBean@Overridepublic void afterPropertiesSet() throws Exception {initStr = "直接在bean初始化的时候就创建了";System.out.println("在创建bean的时候自动和初始化一些值");}
}
// main 函数直接getBean
package com.xusj.future;import com.xusj.future.bean.Person;
import com.xusj.future.config.AppConfig;
import com.xusj.future.service.PeopleService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @author xusj*
CreateDate 2022/5/9 22:15*/
public class MainTest {public static void main(String[] args) {// xml方式
// ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean1.xml");
// Person bean = classPathXmlApplicationContext.getBean(Person.class);
// System.out.println(bean);// 注释方式AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);PeopleService peopleService = (PeopleService) context.getBean("peopleService");peopleService.testMethod();}
}
package com.xusj.future.aspect;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;/*** 切面bean 交给ioc管理** @author xusj*
CreateDate 2022/11/27 0:31*/
@Aspect // 切面
@Component // 切面也是一个bean
public class TestAspect {@Pointcut("execution(public void com.xusj.future.service.PeopleService.testMethod())")public void mypoint() {//切面定义}@Before("mypoint()")public void doAround() {System.out.println("before logic");}}
package com.xusj.future.service;import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** @author xusj*
CreateDate 2022/11/26 22:58*/
@Service
public class PeopleService implements InitializingBean {@Autowiredprivate DogService dogService;private String initStr;// 构造 二// public PeopleService(String initStr) {
// this.initStr = initStr;
// }
// // 构造一
// @Autowired
// public PeopleService(DogService dogService) {
// this.dogService = dogService;
// }public void testMethod() {System.out.println("111");}@Overridepublic void afterPropertiesSet() throws Exception {initStr = "直接在bean初始化的时候就创建了";System.out.println("在创建bean的时候自动和初始化一些值");}
}
// 这里很重要开启AOP代理
package com.xusj.future.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;/*** @author xusj*
CreateDate 2022/11/26 22:58*/
@ComponentScan("com.xusj")
@EnableAspectJAutoProxy // 开启Aop代理
public class AppConfig {
// @Bean
// public PeopleService getPeople() {
// return new PeopleService();
// }
}
// 启动类
package com.xusj.future;import com.xusj.future.config.AppConfig;
import com.xusj.future.service.PeopleService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;/*** @author xusj*
CreateDate 2022/5/9 22:15*/public class MainTest {public static void main(String[] args) {
// ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean1.xml");
// Person bean = classPathXmlApplicationContext.getBean(Person.class);
// System.out.println(bean);AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);PeopleService peopleService = (PeopleService) context.getBean("peopleService");peopleService.testMethod();}
}
在有AOP的情况下,我们getBean拿到是cglib的代理对象,代理对象是怎么调普通对象的方法的

先走前置通知
当正在去调用方法的时候,并不是通过代理对象去调的,而是原普通对象来弄的

// 对于cglib代理来说,就是代理对象去继承被代理对象;为代码如下,这样代理对象就能使用bean中的方法和属性了【代理对象里面是没有值的】
public A extends B{// spring 中private B b;// 这是继承父类的方法public void test(){// 怎么去调用父类的方法// 1、直接去super.test// 2、将B做为属性干到代理类中,spring是这么干的b.test();}
}

对于代理对象来说,以AOP为例子,我们只关注切到对应的方法上面,我们对被代理对象中的属性没有太大关注,所以代理对象是没有值得。
Object target = joinPoint.getTarget();这个完全可以拿到被代理得对象
@Aspect // 切面
@Component // 切面也是一个bean
public class TestAspect {@Pointcut("execution(public void com.xusj.future.service.PeopleService.testMethod())")public void mypoint() {//切面定义}@Before("mypoint()")public void doAround(JoinPoint joinPoint) {// 拿到得是普通对象(被代理对象得值,我们就可以通过这个去得到其中得属性)Object target = joinPoint.getTarget();System.out.println("before logic");}}
// 拿到数据库连接,然后给到事务管理器和jdbc进行操作
/*** @author xusj*
CreateDate 2022/11/26 22:58*/
@ComponentScan("com.xusj")
@EnableAspectJAutoProxy // 开启Aop代理
@EnableTransactionManagement // 开启事务
@Configuration
public class AppConfig {
// @Bean
// public PeopleService getPeople() {
// return new PeopleService();
// }// 拿jdbc@Beanpublic JdbcTemplate jdbcTemplate() {// 将连接交给他return new JdbcTemplate(dataSource());}// 创建数据库连接@Beanpublic DataSource dataSource() {DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setUrl("jdbc:mysql://localhost:3306/study?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai&useInformationSchema=true");dataSource.setUsername("root");dataSource.setPassword("root");return dataSource;}// 交给事务管理@Beanpublic PlatformTransactionManager transactionManager(){DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();dataSourceTransactionManager.setDataSource(dataSource());return dataSourceTransactionManager;}
}
// 添加注解
/*** @author xusj*
CreateDate 2022/11/26 22:58*/
@Service
public class PeopleService implements InitializingBean {@Autowiredprivate DogService dogService;@Autowiredprivate JdbcTemplate jdbcTemplate;@Transactional// 事务注解public void execute() {jdbcTemplate.execute("insert student values (3,s,1)");System.out.println("zhix ");throw new NullPointerException();}}
// 主方法调用
/*** @author xusj*
CreateDate 2022/5/9 22:15*/public class MainTest {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);PeopleService peopleService = (PeopleService) context.getBean("peopleService");peopleService.execute();}
}
这里也是用过代理对象完成事务的,流程如下,类似aop的代理,里面逻辑不一样
public A extends B{// spring 中private B b;// 这是继承父类的方法public void test(){// 1先判断有没有@Transactional这个注解// 2有的话,将conn置为false(默认是true自动提交,这里将他置为手动提交)b.执行sql();// 3没有异常直接commit// 4有异常rollback}
}
@Transactional// 事务注解public void execute() {jdbcTemplate.execute("insert student values (3,s,1)");System.out.println("zhix ");a();}public void a() {jdbcTemplate.execute("insert student values (3,s,1)");System.out.println("zhix ");throw new NullPointerException();}
// 解决方法,我们可以注入自己,然后调用方法,这样的话,我们在ioc拿出来的都是一个代理对象,所以就解决了
@Autowiredprivate PeopleService peopleService;@Transactional// 事务注解public void execute() {jdbcTemplate.execute("insert student values (3,s,1)");System.out.println("zhix ");peopleService.execute();;
// a();}
Spring事务的代理对象执行某个方法时的步骤:
是不是直接被代理对象调用的,如果是则事务会生效,如果不是则失效。下一篇:形容“旗帜”的词语有哪些