【Spring框架】Spring配置大全(供备忘)

一、IOC配置

1、xml配置bean:

(1)构造器配置

<bean id=”xx” class=”x.x.y”>
	<construction-arg name=”” value=””><construction-arg>
	<construction-arg name=”” value=””><construction-arg>
	<construction-arg name=”” value=””><construction-arg>
</bean>

(2)setter方法配置(属性必须有setter方法)

<bean id=”xx” class=”x.x.y”>
	<property name=”属性名” value=”值”><property >
	<property name=”属性名” value=”值”><property >
	<property name=”属性名” value=”值”><property >
</bean>

(3)factory实例工厂配置

<bean id=”xx” factory-bean=”工厂bean” factory-metho=”工厂方法名”>
	<construction-arg name=”” value=””><construction-arg>
</bean>

(4)factory静态工厂配置

<bean id=”xx” class=”工厂路径” factory-metho=”工厂方法名”>
	<construction-arg name=”” value=””><construction-arg>
</bean>
2、注解配置bean

(1)开启注解扫描

<context:component-scan base-package="com"></context:component-scan>

(2)bean启动扫描注解

@Component(value = “xx”),通用形式

(3)自动装配

@AutoWired: 通用,适合接口只有一个实现类
@Qualifier:根据id自动装配,必须与AutoWired一同使用
@Resource(value=”xx”):与AutoWired类似,相当于AutoWired和Qualifier共用
@Service(value=”xx”):多用于业务层
@Repository(value=”xx”):多用于持久层,即数据访问层
@Controller(value=”xx”):多用于控制器,表现层
3、配置Spring容器注解
@Configuration:作用于普通java类,此类会被当做spring容器
@bean:作用于方法,被注解方法的返回值会被当做bean放入spring容器中
@Value:作用于属性,可以给属性赋值,或读取properties文件中的内容
@Import:作用于Spring类容器,用于添加其他配置信息
@Properties:容器中加入properties文件

二、AOP配置

1、xml配置AOP
<bean id=”xx” class=”xxx”></bean>:声明通知类
<aop:config> 导入aop容器
// 自定义切入点表达式,表示对那个对象的哪个方法进行增强
	<aop:pointcut id="ex1" expression="execution(* service.imp.*.*(..))"></aop:pointcut>
	// 定义通知类型:前置,后置,异常,最终,环绕
	<aop:aspect id="xxx" ref="通知类"> 
		<aop:before method="通知方法" pointcut-ref="表达式">
		</aop:before>
		<aop:after-returning method="通知方法" pointcut-ref="表达式">
		</aop:after-returning>
		<aop:throwing method="通知方法" pointcut-ref="表达式">
		</aop:throwing>
		<aop:after method="通知方法" pointcut-ref="表达式"></aop:after>
		<aop:around method="通知方法" pointcut-ref="表达式"></aop:around>
	</aop:aspect>
</aop:config>
2、注解配置AOP
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>:开启AOP注解扫描
@Aspect:配置切面,即通知类
@Pointcut("execution(* service.imp.*.*(..))")
  private void ex1(){}  :配置切入点表达式
@Before(切入点表达式):配置前置通知
@AfterReturning(切入点表达式):配置后置通知
@AfterThrowing(切入点表达式):配置异常通知
@After(切入点表达式):配置最终通知
@Around(切入点表达式):配置环绕通知。推荐使用
@EnableAspectJAutoProxy:Spring类容器开启AOP支持(全注解使用)

三、事务配置

1、xml配置
<!--配置平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   
	<property name="dataSource" ref="dataSource"></property>
</bean>

<!--配置通知类型-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
       <tx:method name="*" propagation="REQUIRED" read-only="false"/>
       <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
    </tx:attributes>
</tx:advice>

<!--配置切入点表达式和切入点表达式与通知的关联-->
<aop:config>
    <aop:pointcut id="pt1" expression="execution(* com.service.imp.*.*(..))"></aop:pointcut>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
2、注解配置

(1)配置spring事务注解扫描支持:

<tx:annotation-driven transaction-manager="transactionManager"/>

(2)注解配置事务(可作用于接口,类,方法):

@Transactional(propagation = Propagation.REQUIRED,readOnly = false)

Propagation为事务传播,通常使用REQUIRED和SUPPORTS两种,其中REQUIRED表示 如果没有事务就创建一个事务,通常用于增删改操作。而SUPPORTS表示如果没有事务,便以非事务的形式运行(即没有事务),通常用于查看操作。

ReadOnly为是否只读,默认值为false,设置true后无法修改,通常查看操作设置为true, 增删改设置为false。

(3)开启容器事务支持

//开启事务扫描,作用于SpringConfiguration容器类
@EnableTransactionManagement

如果有问题,欢迎指正~

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值