Getting started with Spring 1

/**
Question
spring_day01
1. If multiple types are configured according to the type, an exception will be thrown √
2. Is there any constraint set in the applicationContext.xml configuration file?
3. The three ways to instantiate the Bean:
1. Common
2.3 initialized with some framework
where use?
4. Methods and attributes in
inheritance 5.1. OGNL mainly operates struts2 value stack? Use OGNL expressions in dynamic sql 2.
EL expression: manipulate some objects and related values ​​related to servlet
  3. spEL expression: manipulate bean
6. What is the difference between the static factory method and the configuration method of the instance factory method?

* /


The Spring: hierarchical, javaSE / EE stop (full-stack), lightweight open source framework


1.javaEE hierarchical
presentation layer (the page data, page jump scheduling), e.g. jsp / servlet
service layer (Business processing and functional logic, transaction control), such as service
persistence layer (data access and encapsulation, and database interaction), such as dao, mapper


Web presentation layer (servlet, jsp, struct2, springmvc)
Business business layer (JavaBean, EJB (Enterprise JavaBean))
EIS integration layer (persistence layer) (jdbc, hibernate, mybatis, crud operation of the database)

2. The one-stop
Spring provides a JavaEE layers of solution
presentation layer: struct1, struct2, spring MVC
business layer: Ioc, AOP, things control
persistence: jdbcTemplate, mybatis, springDataJpa frame (object-relational mapping) integration


3. lightweight
?

Spring's architecture 1. Spring is
a layered architecture that contains about 20 modules of functional elements.
Spring Framework Runtime
1.Data Access/Integration(JDBC,ORM,OXM,JMS,Transactions)
2.Web(WebSocket,Servlet,Web.Portlet)
3.core Container(Beans, Core, Context, SpEl)
4.Test, AOP , Aspecs, Instrunentation, Messaging


1. The Core Container (Core Container) includes (Core, Bean, Context, EL modules) 1. The core
and Beans modules provide the most basic functions of Spring. The basic concept here is BeanFactory. The
functions: 1. Provide [IoC] and [Dependency Injection Features]
2. It provides a classic implementation of the Factory pattern to eliminate the need for the procedural singleton pattern,
and truly allows you to separate the dependencies and configuration from the program logic.
2. Context module is built based on Core and Bean


3. Expression Language expression language module


2. Data Access/Integration database access/integration part
1. JDBC module
2. ORM module provides the integration layer of "object/relation" mapping APIs (JPA, JDO, Hibernate, iBatis)
use the ORM package to mix and use all the features provided by Spring for "object/relational"
mapping
3.OXM module, providing an abstraction layer that supports Object and XML mapping
4.JMS module,
5.Transaction module, support for the program through a simple declaration things managed

3.Web
1.Web module provides basic Web functions. For example, multi-file upload, xxxx
2. Web-Servlet module, which provides Model-View-Controller (MVC) implementation of Web applications.
3. Web-Portler module, provides the implementation of MVC in the Portler environment.

4.
AOP 1. The AOP module, which provides aspect-oriented programming implementation that conforms to the AOP Alliance specification, allows you to define method interceptors and entry points.
2. The Aspects module, which provides the integration of AspectJ
3.Instrumentation module, providing some class-level tool support and ClassLoader-level implementation.

5.
Test 1.Test module, providing support for testing Spring components using Junit and TestNG.


# IoC inversion of control implementation (Inverse of Control)
to solve the coupling problem of code
introduced into the plant (third party), the original program created manually dependent UserDaoImpl object management,
to the factory to create an administrative
IoC underlying implementation: Plant Design mode + reflection + configuration file (xml)

# DI dependency injection to achieve
DI: when Dependency injection dependency injection, is responsible for the spring framework created Bean object, the dynamic will depend on
the object injected into the Bean component (simply, you can be another The Bean object is dynamically injected into another Bean)
* IOC: Inversion of control, the right to create and manage objects is given to the spring container, and the objects are created through the spring factory
* DI: Create and manage multiple objects in the spring container through the property tag , Inject the object into the object that needs to depend on
* For example: in this example, the userDao object is injected into the userService object, as long as the userService
* is created in the spring factory, userDao can also be referenced at the same time

/* 
IOC (Inversion of Control):
Control: Controls the right to create and manage objects. Through the spring container (ie Application), the object creates
DI (dependency injection) through the spring factory :
In the applicationContext.xml container, use the property tag to inject the required object dependencies into the corresponding Object.
The realized dependent object is created at the same time as the dependent object?
*/


Spring's factory (understand)
# applicationContext means application context.
Function is: loading a configuration file to the spring framework, to build a factory object Spring, Spring context object that is a container, also
referred Spring container

/ *
applicationContext Spring context object is called a container:
the ApplicationContext new new AC = the ClassPathXmlApplicationContext ( "applictionContext.xml");
Object Object = ac.getBean (the above mentioned id / name);
on: ac for information on the image created from the configuration file (xml).
under: the object is created by ac
meaning: from a configuration file To the context of the project creation object
*/


# ApplicationContext is just a sub-interface of BeanFactory (Bean factory, Bean is a java object)


# ApplicationContext BeanFactory is extended to provide more functionality


# BeanFactory take is lazy loading, will initialize the first time getBean
usage:
BeanFactory bf = new new the XmlBeanFactory (new new FileSystemResource (D: \\ applicationContext.xml ")) ;
# ApplicationContext more powerful, so now basically no one developed using the BeanFactory.


three ways to instantiate the bean #
/ *
1. no argument constructor
all bean instances manner described above
2. static factory method
1. create a BeanFactory class
this is a static method which has: (when the static profile references to factory class, for instance when the specified return object
instance factory-method = "static factory class methods", which automatically calls the static method, returns the object)
use : spring container in the example of chemical class, i.e., initialization code may be other objects?
3. instance factory method
1. Create a factory class
which is a common method to return the object instance
<bean id = "bean3" factory- bean="bean3Factory" factory-method="getBean3"/>
When the call to be in the spring when bean3, equivalent references bean3Factry, then call a method wherein the getBean3
returns example objects, enabling instantiation bean3
individual: a static factory method and configuration example of why different factory method:
different :
1. A static factory directly assigns its own full path class name to the bean that needs to be instantiated, and then calls its own static method.
2. The instance factory is when the bean is created by reflection, the bean references the instance factory class to instantiate the object.

*/


# Scope of Bean.
  Under what circumstances are the bean objects created by spring valid.
  Singleton: There is only one Bean instance in the Spring IoC container. Bean exists in a singleton.
Prototype: Every time the bean is called from the container, Return a new instance, that is, every time getBean() is called, it is equivalent to executing
new XxxBean()
/*
Summary: singleton: singleton, initialized in spring container, bean is initialized, and the object obtained each time is the same Value
  prototype: In many cases, it is initialized during getBean, and the bean obtained each time is not the same value.
  
Note: Singleton singleton type, even if it is not called getBean to create an object of this class, the object will initialize its value.
Performing embodied constructor initializes the
reason: when the initialization spring containers, Singleton singleton type has been initialized bean
* /


# Bean Life Cycle
/ *
specified in the initialization bean in xml configuration file, the destruction method
lifecycle necessarily includes (initialization, destruction)
Initialization: xml: init-method = "corresponds to the initialization method name of the bean in the"
destruction: xml:destroy-method = "The name of the destroy method in the corresponding bean"
Note: Only the bean whose scope type is singleton can manually call the method to delete
*/


# Dependency injection of bean properties
/*
2 ways (constructor injection, the setter injection property)
1. constructor
tag <constructor-Arg>
2.setter property
tag <property>
when <ref> bean assigned object
simply: that is, when initializing, a bean need to assign
* /


# P name use of space
/ *
new version p namespace spring2.5 were introduced in
an improved property of the label is dependent on the injection property setter
by reference in beans xmlns tags: p = "http://www.springframework.org/schema/ p"
through specific rules
p: <attribute name> = "xxx" incorporated constant value
p: <attribute name> _ref = "xxx" refer to other objects Bean
to simplify the property tags in place
* /


# sqEL expression
/ *
Spring Expression Language Language expresion
spring3.x the new version of the characteristics
common usage:
# {} bean reference bean
# {} bean.value reference bean property
# {bean.function ()} method reference bean
calculation:
# 1 + {1}
character operation:
# { " tom".toUpperCase()}
.....
Scope of use:
(related to the operation of the bean)
The bean tag in the applicationContext.xml configuration file is used to query the data of the bean object, and is used in conjunction with the
p namespace to assign values ​​to the bean object. .
*/


/*
DI (dependency injection); dependency injection
1, bean injection (inject information of other beans in a bean)
*This bean is the bean in the tag, and the outer bean is the injected bean object
bean tag type using <property>: implanting setter method according to class
name: outer generally named the bean id / name, the name of the method according to the final in the actual writing of the bean setter prevail
example: the present method of the bean setter The suffix name is lowercase, for example, setterXxx corresponds to name:xxx.
ref: refers to a bean object, the value is id/name

* p name space instead of <property> tag


2, bean property injection (inject information in a bean)
a, setter injection
using <property> tag
name: setter methods of the bean lowercase suffixes, such as name attribute value SetXxx- then xxx.
value: simple data
ref: complex data (referring to other beans)

* p namespace instead of <property> tag
p: <property name>="xxx" introduce constant value
p:<property name>_ref="xxx" refer to other Bean object
b, constructor injection
using <constructor-arg>
Location attribute: index name type
simple assignment attribute: value
of complex valued property: REF
* /
 
 

Guess you like

Origin blog.csdn.net/a1_HelloWord/article/details/80900178
Recommended