spring 基本操作總結主要是aop以及依賴注入的基本配置,springaop

來源:互聯網
上載者:User

spring 基本操作總結主要是aop以及依賴注入的基本配置,springaop

一所需架包

spring 

commom-logging.jar  spring.jar

註解

common-annotation.jar

aop面向切面

aspectjrt.jar    aspectjweaver.jar  cglibb-nodep.ja(許可權帶代理proxy)

jdbc database

common-pool.1.6.jar common-dbhp.jar mysql-connector-bin-*.*.*.jar

二

設定檔的模板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>

以上模板引入了面向切面(aop),許可權代理(context)以及資料庫操作(tx)的命名空間

三基本使用

1.建立bean對象

1.1預設通過set

<bean id="persionBean" class="com.my.PersionBean.impl.PersionBean" destroy-method="destory" lazy-init="true" init-method="init"></bean>

1.2通過靜態工廠(factory-method)

<bean id="persionBean2" class="com.my.PersionBean.impl.PersionBeanFactory" lazy-init="true" factory-method="creBeanFactory"></bean>

1.3通過工廠非靜態

<bean id="persionBean3" class="com.my.PersionBean.impl.PersionFactory" ></bean>

<bean id="persionBean4" factory-bean="persionBean3" scope="prototype" factory-method="createOrderFactory"></bean>

2.注入bean

<property name="persion" ref="persionBean4" ></property>

3.注入其他屬性

<property name="name" value="校長"></property>

<property name="age" value="15"></property>

<property name="habits">

4.注入List<>屬性

<property name="habits">

<list>

<value>第一個</value>

<value>第二個</value>

<value>第三個</value>

</list>

</property>

5.通過<constructor-arg index="0" value="xiaozhang"></constructor-arg>(建構函式index參數索引)

6.對象的建立scope

scope="prototype" 實體模式(多個對象)

scope="singleton"單例模式(default)在容器只有一個對象

另外還有session,request 域

    通過註解方式注入

<context:annotation-config/>//開啟註解

通過@Resource(name="student")指定bean的id注入 通過@Autowired@Qualifier(value="student")(命名id)注入

通過註解方式建立bean

@Component (任何)

@Repository(value="student")(資料訪問)

@Controller(控制)

@Service(服務)

  . @Scope("prototype")(指定域)

@PostConstruct(初始化方法)

@PreDestroy(銷毀方法)

7.aop切面

使用jdk進行許可權代理

必須有介面

@Aspect

public class MyIntercepter {

@Pointcut("execution (* com.my.aop.impl.PersionServiceBean.*(..))")

private void anyMethod() {//聲明切入點

 

}

@Before("anyMethod()&&args(name)")

private void doAccessCheck(String name){

System.out.println("這是前置通知");

}

@AfterReturning(pointcut="anyMethod()",returning="result")

private String doAfterReturn(String result){

System.out.println("這是後置通知"+result);

return result;

}

@AfterThrowing("anyMethod()")

private void doAfterThrow(){

System.out.println("這是例外通知");

}

@After("anyMethod()")

private void doAfter(){

System.out.println("這是最終通知");

}

@Around("anyMethod()")

public Object around(ProceedingJoinPoint point) throws Throwable{

System.out.println("這是環繞通知");

Object resultObject=point.proceed();

System.out.println("推出");

return resultObject;

 

}

 

使用cglib註解方式  <aop:aspectj-autoproxy/>(啟動註解)

 

public class MyIntercepter {

@Pointcut("execution (* com.my.aop.impl.PersionServiceBean.*(..))")

private void anyMethod() {//聲明切入點

 

}

@Before("anyMethod()&&args(name)")

private void doAccessCheck(String name){

System.out.println("這是前置通知");

}

@AfterReturning(pointcut="anyMethod()",returning="result")

private String doAfterReturn(String result){

System.out.println("這是後置通知"+result);

return result;

}

@AfterThrowing("anyMethod()")

private void doAfterThrow(){

System.out.println("這是例外通知");

}

@After("anyMethod()")

private void doAfter(){

System.out.println("這是最終通知");

}

@Around("anyMethod()")

public Object around(ProceedingJoinPoint point) throws Throwable{

System.out.println("這是環繞通知");

Object resultObject=point.proceed();

System.out.println("推出");

return resultObject;

 

}

}

使用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>

 

 

spring對jdbc操作

<context:property-placeholder location="classpath:jdbc.properties"/>(引入屬性檔案)

     (建立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" (建立事物bean)class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

  <property name="dataSource" ref="dataSource"></property>

 </bean>

以註解方式

<tx:annotation-driven transaction-manager="txManager" />(啟動註解)

以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>

詳細見例子

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.