[Reserved] Spring Framework summary

 1.IOC 和 OF

IOC: Inversion of control
that is transfer of control, the way we create objects reversed, previously created objects is maintained by our own developers, including dependency injection is their own. After using the spring, and create dependency objects can be created and completed by the spring injection, inversion of control is reversed way to create objects from our own reversal to create a program to create (spring)

DI: Dependency Injection dependency injection
spring this vessel, for you to manage a series of classes, provided that you need these classes to the spring container management, then when you need it, not to define themselves, but directly to the spring container demand, container know when spring after your needs, you will go to find the components it manages, and then directly to the components you need.
implement IOC ideas need support to do DI
injection method: 1.set way to inject 2 . 3. The field constructor injection injection
injection type: 1. 2. The reference value type injection injection type

 

benefit: 

1 . Reduce the coupling between the components, decoupling between software layers.


 2 may provide a number of services such as the container transaction management message service processing and the like. When we use container-managed transactions, developers do not need to manually control transactions and does not need to handle complex transaction propagation 3 . Container provides support for single-case model, developers do not need to write the implementation code. 4 . Containers provide AOP technology , use it very easy to implement, such as permission to intercept runtime monitoring and other functions


 5. the container offers numerous assistant class, so these classes can accelerate the development of applications such as jdbcTemplate HibernateTemplate

2.applicationContext & BeanFactory区别

BeanFactory interface (old version factory class)
(1) of the Spring original interface implementation class for the functionality of the original interface is single
(2) BeanFactory interface container class, characterized by obtaining each time an object will create an object

ApplicationContext interface (new version factory class)
for all objects (1) each time the vessel will start to create a container configuration
(2) provides more features
(3) ApplicationContext there are two implementation classes

    1. Load profile from the class path: the ClassPathXmlApplicationContext
    2. load configuration file from the hard disk the absolute path: FileSystemXmlApplication

3.spring configuration in detail

3.1 Element Properties

    bean element: This element describes managed object requires spring containers
    name attribute: a name to the managed object, obtaining an object getBean ( "name value")
    class properties: a full managed object class name
    id attribute: attribute name and exactly the same, the name can not be repeated, you can not use special characters

Note that some of the points between the name and the id:
. 1, arranged in two identical id or name can not pass.
2, if both are configured id, also configure the name, the two are to take effect. If id and name are not specified, with the class name as full name, such as <bean class = "com.stamen.BeanLifeCycleImpl">you can return to it by the example getBean ( "com.stamen.BeanLifeCycleImpl").
3, if the time when the basic configuration of classes, annotations and configuration files are used, notes and configuration files in the same name, when the two conflict, the configuration file to take effect.
      If you configure the basic class, when notes and configuration files are used, annotations and configuration file name is not the same when the two do not conflict, can take effect.

3.2, bean advanced elements (scope attribute lifecycle property) ----- singleton cases of

(1) scope property
(1) singleton Default   
singleton object: the object is identified as a single instance in the present embodiment the spring will vessel
(2) prototype    
multi Prototype Example: the object is identified as an example of multiple, in each get will be created each time a new object is created
(3) request
the application in a web project, Spring create this class in the future, it will be credited to request this class range.   
(4) session
after application in a web project, Spring create this class, this class will be deposited into session scope.

(5) globalsession: application, must be used under porlet environment in a web project. But without such an environment, with respect to the session.
Summary: In most cases, using a singleton singleton (default value), but in the integration and struts time, be sure to use the prototype more cases, because struts2 in every request to create a new Action, if single cases in under multiple requests, each request is look for spring to take the same action.

(2) life-cycle attributes (understand) --- initialization and destruction of
    (1) a method as a lifecycle configuration initialization method, spring will call the init-method after the object is created immediately
    (2) as a method to configure a life cycle of destruction methods , spring calls destory-method in the container before closing and destruction of all objects in the container
    <bean init-method = "init         " destory-method = "destory"> </ bean> corresponding to the annotated @PostConstruct

    <bean name = "hello" class = " full class name"> </ bean>                                 corresponding to the annotated @PreDestory

(3) modular configuration, i.e. sub-module configuration (configuration file import additional spring)
<Beans>
    <Import Resource = "full path name of the spring configuration file" />
</ Beans>

3.3, three kinds of objects to create a way of spring

(1) null parameter configuration (important)

(2) create static factory (call a static method to create)
calls the static method createUser UserFactory class to create an object named user, into a container

<bean name="user" class="cn.itcats.UserFactory" factory-method="createUser"></bean>

(3) create a factory instance (non-static method call to create) - need to configure two bean, because non-static method can not be called by class name

<bean name="user2" factory-bean="userFactory" factory-method="createUser"></bean>
 
<bean name=“userFactory” class=“cn.itcats.UserFactory”></bean>

3.4, spring injection method

(1) set injection mode (key) by the value ---- value type injection with a reference type injection ref

Set Method common attributes injection
property  tag for attribute configuration Set Method injection
name: name of the parameter
value: Common Data Set
ref: reference data, another bean id values generally

(2) Constructor injection

Properties constructor injection
constructor-arg tag construction method for configuration attributes injection
name: name of the parameter
value: Common Data Set
ref: reference data, another bean id values generally

Of course, the properties of the injection method also supports constructor injection object attributes, also ref corresponding property tag
if there is only a constructor parameter and parameter types matching with the injected bean type that will be injected into the constructor

Injection function
(3) p --- namespace injection injection actually set, spring-specific, in order to simplify the <property> wording

   1, applicationContext.xml in <beans> tag introduced into the head namespace p

xmlns:p="http://www.springframework.org/schema/p"

 2, writing format: Value Type injection - p: attribute name = "value" reference type injection - p: attribute name = -REF "reference <bean> name attribute"

       Run set the name attribute value class is haha, age property is set to 20, the attribute references cited hello <bean name = "hello" class = "..."> </ bean>

<bean name="run2" class="cn.itcats.thread.Run" p:name="haha" p:age="20" p:hello-ref="hello"></bean>

(4)spel注入: spring Expression Language spring表达式语言

<bean name="runSpel" class="cn.itcats.thread.Run">
    <!-- 取bean标签中name为"user"中property为"name"中的value值 --!>
    <property name="name" value="#{user.name}"></property>
</bean>

SpEL特性:(1)、使用Bean的ID来引用Bean;(2)、调用方法和访问对象的属性;(3)、对值进行算术、关系和逻辑运算;(4)、正则表达式匹配;(5)、集合操作

关于spel   https://www.cnblogs.com/goodcheap/p/6490896.html

复杂类型注入

1.array数组的注入

2.list集合的注入

 3.map集合的注入

 4.properties的注入

4、防止创建多个applicationContext取值/并指定记载spring配置文件的位置——web.xml

   1、需要导入包spring-web
   2、在web.xml中配置监听器

5、使用注解方式代替配置文件(官方推荐使用注解)

1.在applicationContext.xml中书写指定扫描注解

 2.在类中书写Component

注意:假如不写括号内的值(即name或id),默认使用类名首字母小写作为搜索,为什么意思呢?

比如Student类中使用了@Component   没有书写括号和值,那么默认搜索id或name为student。

衍生:
@Controller Web层
@Service 业务层
@Repository 持久层
这三个注解是为了让标注类本身的用途清晰

属性注入的注解 ( 可以没有set方法
普通类型属性:@Value

对象类型属性:@Resource (对应bean中的id)= @Autowired(类型)+ @Qualifier(名称)

3.指定对象的作用范围Scope

 

 

 声明Student类对象为多例       下面是对singleton和prototype的一些补充

singleton作用域:当把一个Bean定义设置为singleton作用域是,Spring IoC容器中只会存在一个共享的Bean实例,并且所有对Bean的请求,只要id与该Bean定义相匹配,则只会返回该Bean的同一实例。值得强调的是singleton作用域是Spring中的缺省作用域。

prototype作用域:prototype作用域的Bean会导致在每次对该Bean请求(将其注入到另一个Bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的Bean实例。根据经验,对有状态的Bean应使用prototype作用域,而对无状态的Bean则应该使用singleton作用域。对于具有prototype作用域的Bean,有一点很重要,即Spring不能对该Bean的整个生命周期负责。具有prototype作用域的Bean创建后交由调用者负责销毁对象回收资源。简单的说:

singleton 只有一个实例,也即是单例模式。

prototype访问一次创建一个实例,相当于new。

4.值类型的注入

实际通过反射field赋值

实际通过set方式赋值

5.引用类型的注入

面试题: @AutoWired和@Resource的区别?

@AutoWired默认以类型进行查找,@Resource默认以名称进行查找

@AutoWired(required=false)    +   @Qualifier("user")    ==   @Resource(name="user")

其中@Resource注解是jdk1.6后才有的

 

 

6.创建与销毁方法

 7.spring整合junit测试(spring创建容器)

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration("classpath:applicationContext.xml")    

6、spring中AOP名词解释

JoinPoint(连接点):目标对象中,所有可以增强的方法,就是spring允许你是通知(Advice)的地方,那可就真多了,基本每个方法的前、后(两者都有也行),或抛出异常是时都可以是连接点,spring只支持方法连接点。


Pointcut(切入点):目标对象中,已经被增强的方法。调用这几个方法之前、之后或者抛出异常时干点什么,那么就用切入点来定义这几个方法。


Advice(通知/增强) :增强方法的代码、想要的功能。


Target(目标对象):被代理对象,被通知的对象,被增强的类对象。


Weaving(织入):将通知应用到连接点形成切入点的过程


Proxy(代理):将通知织入到目标对象之后形成的代理对象


aspect(切面):切入点+通知————通知(Advice)说明了干什么的内容(即方法体代码)和什么时候干(什么时候通过方法名中的before,after,around等就能知道),二切入点说明了在哪干(指定到底是哪个方法),切点表达式等定义。

虽然现在都用Maven项目构建,但是不能忘记,使用aop需要用到的包:spring-aop + spring-aspects + springsource.org.aopalliance + springsource.org.aspectj.weaver 

关于AOP看一个小例子:看原文

7、spring中的aop使用注解配置

 1、applicationContext.xml中配置目标对象,通知对象,开启使用注解完成织入

8、spring整合jdbc

9、spring中的aop事务

————————————————
版权声明:本文为CSDN博主「itcats_cn」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/itcats_cn/article/details/81479185

Guess you like

Origin www.cnblogs.com/doyi111/p/11823035.html