Spring uses

bean life cycle

1, singleton: single embodiment, the system is running only one instance of
the spring loading of the container when the good bean instance, only instantiated once.
spring bean container to manage the default is singleton.
2, prototype: multiple cases, each visit must create an instance.
When you call applicationContext.getBean method will create an instance.

Focus:! ! !
Dao: single embodiment, do not add attributes (member variables data field) having a state in which DAO
-Service: single embodiment, do not add attributes (member variables data field) having a state of-Service
the Action: Multi Example
Servlet: single embodiment
when the class can be a single embodiment, no member variables when the thread-safe data field (state having 0-1 || '0' - attribute '1')

annotation

It provides @component logo is a bean in the spring.
@Component can be used on any of the bean.
Each annotation can be arbitrarily used, but in order to develop standardized, uniform annotation label.
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

@Autowired: injection by type
1 may be identified on the property, to the type of spring according to the attribute of the same type of container to find bean, bean found then injected into the property.
@ Autowired // by type injection
private CustomerDao customerDao;
Note: If the container in the same type of bean If you have multiple, use Autowried error, find more of the same type of bean, use @Qualifier and Autowired configuration, Qualifier specify which bean injection room.
Here Insert Picture Description
@Resource: by name injected
Here Insert Picture Description
and autoWired difference: Resource annotation is under jdk, while autoWired spring is provided.

@Transactional: @Transactional transaction control the use of this method to identify the service method

Component scans

component-scan may scan the annotations: @ controller, @ Service, @ Repository, @ Component
specified scanning package cn.itcast.crm, the class may be scanned subpackages
<context: component-scan base- package = "cn.itcast. crm "> </ context: component -scan>
principle:
when loading spring containers, according to the package path top configuration, and the sub next scan package bag under the category, if identified: @ controller, @ Service, @ Repository, @Component instantiated.

Context: annotation-config configuration used: in order to activate and autowire qualifier annotations.
And context: component-scan configuration difference is a large range of the latter. All annotations included.

spring and struts2 integration

Create an instance of action changed StrutsSpringObjectFactory factory that creates logic:
struts2 framework beanname start spring container to find there is no such bean, if there is to take from the container, if not then create your own.

Jdk configuration or cglib

In aop agent configuration, the can be controlled through the use of proxy-target-class proxy mode:
proxy-target-class = to false default value, using jdk Agent
aop proxy configuration
<aop: config proxy-target-class = "to false">
Proxy -target-class = true agent used cglib
<AOP: config = proxy-target-class "to true">
jdk: an object-based interface generation agent
generates a proxy object the method according to the interface, the original enhanced proxy object
to provide a reflective implemented using jdk

cglib: generating agent class object based on
generating a subclass (proxy object) according to class, enhanced proxy object of the parent class (sub-class).
Use CGLib achieve
spring underlying the use of jdk and cglib, if the original object implements an interface, spring use jdk, otherwise the cglib generate a proxy.
If this does not implement the interface, you can only use cglib proxy! ! !

Published 81 original articles · won praise 5 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_36205206/article/details/105080884