Spring学习

一 spring的IoC容器

Ioc 控制反转 inversion of control

DI 依赖注入 Dependency injection

控制反转理解:

类的生成不在通过手动new,而是将其控制权交给spring管理。依赖注入则是对象或简单类型数据作为属性注入到另一个对象中。

IOC 他的反转,反转在让事必躬亲转变为享受服务(因为有依赖注入)。

依赖注入的方式_构造方法注入

public A(B b,C c){

    this.b=b;

    this.c=c;

}

依赖注入的方式_setter方法注入

FXNewsProvider

{

private IFXNewsListener newsListener;

private IFXNewsPersister newPersistener;

public IFXNewsListener getNewsListener()

{

return newsListener;

}

public void setNewsListener(IFXNewsListener newsListener)

{

this.newsListener = newsListener;

}

public IFXNewsPersister getNewPersistener()

{

return newPersistener;

}

public void setNewPersistener(IFXNewsPersister newPersistener)

{

this.newPersistener = newPersistener;

}

}

依赖注入的方式_接口方法注入(强制被注入对象实现不必要的接口,带有侵入性,不提倡)。

https://blog.csdn.net/it_man/article/details/4402245

https://www.jianshu.com/p/dff5484f4c01

依赖注入管理方式 配置文件方式

ApplicationContext context=new ClassPathXmlApplicationContext("");

Context.getBean(xxx);

Scope:

Singleton、prototype 、

request 、session 、global session(后三种只能在web中使用)

有状态和无状态:https://www.cnblogs.com/xubiao/p/6567349.html

有状态:有数据存储功能,有状态对象就是有实例变量的对象,可以保存数据,非线程安全的。

无状态就是一次操作,不能保存数据,无状态对象就是没有实例变量的对象,线程安全。

无状态的Bean适合单实例模式,有状态的Bean因为多线程操作不安全,适合prototype原型模式。

SpringMvc默认是单例默认,Struts2默认的实现是Prototype模式。

request

XmlWebApplicationContext会为每个HTTP请求创建一个全新的RequestProcessor对象供当前请求使用,当请求结束后,该对象实例的生命周期即告结束。当同时有10个HTTP 请求进来的时候,容器会分别针对这10个请求返回10个全新的RequestProcessor对象实例,且它们 之间互不干扰。从不是很严格的意义上说,request可以看作prototype的一种特例,除了场景更加具体 之外,语意上差不多。

使用ConfigurableBeanFactory的以下方法注册自定义scope:

代码注册:Scope threadScope = new ThreadScope(); beanFactory.registerScope("thread",threadScope);

在配置文件中注册:

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">

    <property name="scopes">

        <map>

            <entry key="thread" value="com.foo.ThreadScope"/>

        </map>

    </property>

</bean>、

Spring的IoC容器主要有两种,即BeanFactory和ApplicationContext.

 

 

 

 

 

 

 

SpringBoot

 

@Configuration

@ComponentScan

<context:component-scan>

@PropertySource @PropertySources

例如:

不低于Java 8 版本

    @Configuration

    @PropertySource("classpath:1.properties")

    @PropertySource("classpath:2.properties")

public class Xconfiguration{

 

}

低于java 8 版本

@PropertySources({

    @PropertySource("classpath:1.properties"),

    @PropertySource("classpath:2.properties"),

})

 

@Import @ImportResource

Xml中:<import resource="xxx.xml">

 

@Import(xxx.class)

public class Xconfiguration{

}

 

SpringBoot是Spring框架对 "约定优先于配置"理念的最佳实践的产物。

@SpringBootApplicationà@Configuration @EnableAutoConfiguration @ComponentScan

 

SpringFactoriesLoader

 

https://blog.csdn.net/sinat_33625560/article/details/78605367 spring的监听事件ApplicationListener和ApplicationEvent的用法。

 

JPA 是一组用于将数据存入数据库的类和方法的集合

 

@Autowired 按照类型匹配bytype

@Qualifier("xxx") 实际上是ByName自动绑定的注解版

@Resource与@ Autowired不同,他遵循的是byName自动绑定形式的行为准则,Ioc容器将根据@Resource所指定的名称,到容器中查找beanname与之对应的实例,然后将查找到的对象实例注入到@Resource所标注的对象,@Resource(name="xxx")

不管是@Autowired还是@Resource都需要添加相应的BeanPostProcessor,

<context:annotation-config/>

classpath-scanning功能可以从某一顶层 包(base package)开始扫描。当扫描到某个类标注了相应的注解之后,就会提取该类的相关信息,构 建对应的BeanDefinition,然后把构建完的BeanDefinition注册到容器。这之后所发生的事情就不 用我说了,既然相关的类已经添加到了容器,那么后面BeanPostProcessor为@Autowired或者 @Resource所提供的注入肯定是有东西拿咯!

classpath-scanning功能的触发是由决定的。

<context:component-scan base-package="com.xxx.xxx">

<context:component-scan>默认开启annotation-config

 

Spring AOP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/joan-HTY/p/9365978.html