Spring basic operations are summarized mainly by the basic configuration of aop and dependency injection. springaop
A required rack
Spring
Spring. jar commom-logging.jar
Annotation
Common-annotation.jar
Aop Aspect-Oriented
Aspectjrt. jar aspectjweaver. jar cglibb-nodep.ja (permission with proxy)
Jdbc database
Common-pool.1.6.jar common-dbhp.jar mysql-connector-bin-*. jar
II
Configuration file template beans. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: tx = "http://www.springframework.org/schema/tx"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: aop = "http://www.springframework.org/schema/aop"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
</Beans>
The preceding template introduces the namespace for aspect-oriented (aop), permission proxy (context), and database operation (tx ).
Use the triplicate Technology
1. Create a bean object
1.1 set
<Bean id = "persionBean" class = "com. my. persionBean. impl. persionBean "destroy-method =" destory "lazy-init =" true "init-method =" init "> </bean>
1.2 Through static factory-method)
<Bean id = "persionBean2" class = "com. my. PersionBean. impl. PersionBeanFactory" lazy-init = "true" factory-method = "creBeanFactory"> </bean>
1.3 non-static via Factory
<Bean id = "persionBean3" class = "com. my. PersionBean. impl. PersionFactory"> </bean>
<Bean id = "persionBean4" factory-bean = "persionBean3" scope = "prototype" factory-method = "createOrderFactory"> </bean>
2. Inject bean
<Property name = "persion" ref = "persionBean4"> </property>
3. Inject other attributes
<Property name = "name" value = "principal"> </property>
<Property name = "age" value = "15"> </property>
<Property name = "habits">
4. Inject List <> attributes
<Property name = "habits">
<List>
<Value> first </value>
<Value> second </value>
<Value> third </value>
</List>
</Property>
5. Use <constructor-arg index = "0" value = "xiaozhang"> </constructor-arg> to construct the index parameter index of the function)
6. Create a scope for the object
Scope = "prototype" entity mode (multiple objects)
Scope = "singleton" singleton mode (default) has only one object in the container
There are also session and request fields.
Injection Through Annotation
<Context: annotation-config/> // enable annotation
Use @ Resource (name = "student") to specify the bean id injection through @ Autowired @ Qualifier (value = "student") (name id) Injection
Create bean by Annotation
@ Component (any)
@ Repository (value = "student") (Data Access)
@ Controller)
@ Service)
. @ Scope ("prototype") (specify the domain)
@ PostConstruct (initialization method)
@ PreDestroy (destruction method)
7. aop aspect
Use jdk for permission proxy
Required APIs
@ Aspect
Public class MyIntercepter {
@ Pointcut ("execution (* com. my. aop. impl. PersionServiceBean .*(..))")
Private void anyMethod () {// declare the start point
}
@ Before ("anyMethod () & args (name )")
Private void doAccessCheck (String name ){
System. out. println ("this is a pre-notification ");
}
@ AfterReturning (pointcut = "anyMethod ()", returning = "result ")
Private String doAfterReturn (String result ){
System. out. println ("this is a post-notification" + result );
Return result;
}
@ AfterThrowing ("anyMethod ()")
Private void doAfterThrow (){
System. out. println ("this is an exception notification ");
}
@ After ("anyMethod ()")
Private void doAfter (){
System. out. println ("this is the final notification ");
}
@ Around ("anyMethod ()")
Public Object around und (ProceedingJoinPoint point) throws Throwable {
System. out. println ("this is a surround notification ");
Object resultObject = point. proceed ();
System. out. println ("released ");
Return resultObject;
}
Use cglib annotation <aop: aspectj-autoproxy/> (enable annotation)
Public class MyIntercepter {
@ Pointcut ("execution (* com. my. aop. impl. PersionServiceBean .*(..))")
Private void anyMethod () {// declare the start point
}
@ Before ("anyMethod () & args (name )")
Private void doAccessCheck (String name ){
System. out. println ("this is a pre-notification ");
}
@ AfterReturning (pointcut = "anyMethod ()", returning = "result ")
Private String doAfterReturn (String result ){
System. out. println ("this is a post-notification" + result );
Return result;
}
@ AfterThrowing ("anyMethod ()")
Private void doAfterThrow (){
System. out. println ("this is an exception notification ");
}
@ After ("anyMethod ()")
Private void doAfter (){
System. out. println ("this is the final notification ");
}
@ Around ("anyMethod ()")
Public Object around und (ProceedingJoinPoint point) throws Throwable {
System. out. println ("this is a surround notification ");
Object resultObject = point. proceed ();
System. out. println ("released ");
Return resultObject;
}
}
Use xml
<Aop: aspectj-autoproxy/>
<Bean id = "persionServiceBean" class = "com. my. aop. impl. PersionServiceBean"> </bean>
<Bean id = "interceptor" class = "com. my. aop. impl. myInterceptor"> </bean>
<Aop: config>
<Aop: aspect id = "aspect" ref = "interceptor">
<Aop: pointcut expression = "execution (* com. my. aop. impl. PersionServiceBean. * (..)" id = "mycut"/>
<Aop: before method = "doAccessCheck" pointcut-ref = "mycut"/>
<Aop: around method = "around" pointcut-ref = "mycut"/>
</Aop: aspect>
</Aop: config>
Jdbc operations by spring
<Context: property-placeholder location = "classpath: jdbc. properties"/> (introduce the property file)
(Create dataSource) <bean id = "dataSource" class = "org. apache. commons. dbcp. BasicDataSource" destroy-method = "close">
<Property name = "driverClassName" value = "$ {driverClassName}"> </property>
<Property name = "url" value = "$ {url}"> </property>
<Property name = "username" value = "$ {username}"> </property>
<Property name = "password" value = "$ {password}"> </property>
<Property name = "maxActive" value = "$ {maxActive}"> </property>
<Property name = "initialSize" value = "$ {initialSize}"> </property>
<Property name = "maxIdle" value = "$ {maxIdle}"> </property>
<Property name = "minIdle" value = "$ {minIdle}"> </property>
</Bean>
<Bean id = "txManager" (create transaction bean) class = "org. springframework. jdbc. datasource. cetcetransactionmanager">
<Property name = "dataSource" ref = "dataSource"> </property>
</Bean>
Annotated
<Tx: annotation-driven transaction-manager = "txManager"/> (enable annotation)
Use xml
<Aop: config>
<Aop: pointcut expression = "execution (* com. my. jdbc. service. impl. StudentService. * (..)" id = "transactionPointCut"/>
<Aop: advisor advice-ref = "txAdvice" pointcut-ref = "transactionPointCut"/>
</Aop: config>
<Tx: advice id = "txAdvice" transaction-manager = "txManager">
<Tx: attributes>
<Tx: method name = "get *" propagation = "NOT_SUPPORTED"/>
<Tx: method name = "*"/>
</Tx: attributes>
</Tx: advice>
For more information, see examples.