你的位置:首页 > 软件开发 > Java > spring 基本操作总结主要是aop以及依赖注入的基本配置

spring 基本操作总结主要是aop以及依赖注入的基本配置

发布时间:2016-02-05 00:00:04
一所需架包spring commom-logging.jar spring.jar注解common-annotation.jaraop面向切面aspectjrt.jar aspectjweaver.jar cglibb-nodep.j ...

一所需架包

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.

<?

<beans 

       

       

       

       

     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"  destroy-method="destory" lazy-init="true" init-method="init"></bean>

1.2通过静态工厂(factory-method)

<bean id="persionBean2"  lazy-init="true" factory-method="creBeanFactory"></bean>

1.3通过工厂非静态

<bean id="persionBean3"  ></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;

 

}

}

使用

   <aop:aspectj-autoproxy/>

    <bean id="persionServiceBean" ></bean>

     <bean id="interceptor" > </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"  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)>

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

 </bean>

以注解方式

<tx:annotation-driven transaction-manager="txManager" />(启动注解)

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

详细见例子

 


原标题:spring 基本操作总结主要是aop以及依赖注入的基本配置

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录