共 11 篇文章 |
|
有状态会话bean :每个用户有自己特有的一个实例,在用户的生存期内,bean保持了用户的信息,即“有状态”;二、解决有状态bean的线程安全问题。解决办法就是在进行转账操作这个线程中,使用ThreadLocal中获取Connection对象,这样,在调用个人账户减少和银行账户增加的线程中,就能从ThreadLocal中取到同一个Connection对象,并且这个Connec... 阅357 转0 评0 公众公开 20-05-14 19:56 |
1.2、@Configuration启动容器+@Bean注册Bean,@Bean下管理bean的生命周期。public static void main(String[] args) { // @Configuration注解的spring容器加载方式,用AnnotationConfigApplicationContext替换ClassPathXmlApplicationContext ApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); ... 阅42 转0 评0 公众公开 20-05-03 10:38 |
Spring下单例模式与线程安全之间的矛盾解决。就是因为Spring对一些Bean(如RequestContextHolder、TransactionSynchronizationManager、LocaleContextHolder等)中非线程安全状态采用ThreadLocal进行处理,让它们也成为线程安全的状态,因为有状态的Bean就可以在多线程中共享了。线程安全案例。说明:当线程较多时,当一个线程调用该方法时,其... 阅168 转0 评0 公众公开 20-05-02 15:19 |
Spring事务的传播特性和隔离级别。PROPAGATION_REQUIRED.@Transactional(propagation = Propagation.当classB.method()打上PROPAGATION_REQUIRES_NEW的事务标志,执行classA.method()方法,当执行到classB.method()的时候,会检查上下文有没有事务,如果classA.method()有事务,则会挂起calssA.method()的事务,新建一个属于classB.method()的... 阅79 转0 评0 公众公开 20-05-02 14:50 |
public UserService(IUserDao userDao) { System.out.println("这是有一个参数的构造方法"); this.userDao = userDao; } public UserService(IUserDao userDao, User user) { System.out.println("这是有两个参数的构造方法"); this.userDao = userDao; this.user = user; } public void loginUser() { userDao.loginUser(... 阅1511 转0 评0 公众公开 20-05-02 10:11 |
public class SpringBeanLifecycleApplication { public static void main(String[] args) throws InterruptedException { // 为面试而准备的Bean生命周期加载过程 ApplicationContext context = new ClassPathXmlApplicationContext("Bean-Lifecycle.xml"); Book book = (Book)context.getBean("book"); System.out.print... 阅53 转0 评0 公众公开 20-05-01 21:42 |
在Spring中,对象的实例化是通过反射实现的,而对象的属性则是在对象实例化之后通过一定的方式设置的。首先Spring尝试通过ApplicationContext.getBean()方法获取A对象的实例,由于Spring容器中还没有A对象实例,因而其会创建一个A对象,然后发现其依赖了B对象,因而会尝试递归的通过ApplicationContext.getBean()方法获取B对象的实例,但是Spri... 阅74 转0 评0 公众公开 20-05-01 17:19 |
--scope="singleton"(默认就是单例方式) --><bean id="a" class="com.zfx.student.StudentA" scope="singleton"> <property name="studentB" ref="b"></property> </bean> <bean id="b" class="com.zfx.student.StudentB" sc... 阅76 转0 评0 公众公开 20-05-01 17:15 |
} // Thread中的源码 /* ThreadLocal values pertaining to this thread. This map is maintained * by the ThreadLocal class. */ ThreadLocal.ThreadLocalMap threadLocals = null; /* * InheritableThreadLocal values pertaining to this thread. This map is * maintained by the InheritableThreadLocal class. */ ThreadLocal.ThreadLoc... 阅24 转0 评0 公众公开 20-05-01 15:49 |
Spring的两种动态代理:Jdk和Cglib 的区别和实现。package com.lf.shejimoshi.proxy.cglib;//Cglib动态代理,实现MethodInterceptor接口public class CglibProxy implements MethodInterceptor { private Object target;//需要代理的目标对象 //重写拦截方法 @Override public Object intercept(Object obj, Method method, Object[] arr, Meth... 阅37 转1 评0 公众公开 20-03-24 16:16 |