Spring GetStarted

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.0.xsd"
    default-init-method="init" default-destroy-method="destroy" default-autowire="default/byType/byName/constructor/no">  
	
	<!--
		Author:guoliao
		Date:2018-03-06 19:00:00
	-->
	
	<!-- 开启注解扫描 -->
	<context:component-scan base-package="pkgName" />
	<!-- 
		properties配置文件
		该xml文件中可以使用${key}
		也可以在类中使用注解@ImportResource{"classpath: file: http:"}
	-->
	<context:property-placeholder location="file: classpath: http:"/>
	
	<!-- 构造器注入index指定构造器索引,type构造器参数类型 
		init-method="" destroy-method=""
		对应Bean可以实现InitializingBean和DisposableBean接口
	-->
    <bean id="name" class="java.lang.String">
        <constructor-arg index="" type="" value="guoliao" />
    </bean>
    <bean id="str" class="cn.com.infosec.bean.StringBean"></bean>
	
	<!-- 属性注入,包含list、map、porp注入 -->
    <bean id="collection" class="cn.com.infosec.bean.CollectionBean">
        <property name="list">
            <list>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="key 1" value="A"></entry>
                <entry key="key 2" value-ref="list"></entry>
                <entry key="key 3">
                    <bean class="java.lang.String">
                        <constructor-arg value="test"/>
                    </bean>
                </entry>
            </map>
        </property>
        <property name="prop">
            <props>
                <prop key="1">one</prop>
                <prop key="2">two</prop>
                <prop key="3">three</prop>
            </props>
        </property>
    </bean>
    <bean id="list" class="java.util.ArrayList">
        <constructor-arg>
            <list>
                <value type="java.lang.Integer">12</value>
                <value type="java.lang.String">12</value>
            </list>
        </constructor-arg>
    </bean>
	
	<!--
		注解使用:
		@Component("beanID")		使用beanID注册该类实例
		@Value("val")				配置属性值,可用于String、primary type、wrapper class
		@Service					注解数据Service
		@Repository					注解dao
		@Configuration @Bean注解用于该类的方法上,表示返回一个实体类
		@Autowired + @Qualifier("bean") = @Resource(name="")  自动赋值,可以使用byName/byType/constructor/default/no
		@Scope						配置范围定义,可以使用singleton/prototype/session/request/global session
		Aware接口的目的是为了能够让实现该接口的类获取到ApplicationContext上下文
	-->

        <bean id="calcImpl" class="studio.guoliao.util.CalcUtil"></bean>
	<bean id="timeAspectBean" class="studio.guoliao.util.StudyAspect"></bean>
	<aop:config>
		<aop:aspect id="timeAspect" ref="timeAspectBean">
			<aop:pointcut expression="execution(* studio.guoliao.util.CalcUtil.add(int, int))
				and args(a, b)" id="addPointCut" />
			<aop:pointcut expression="execution(* studio.guoliao.util.CalcUtil.subtraction(int,int))
				and args(a, b)" id="subPointCut" />
			<aop:pointcut expression="execution(* studio.guoliao.util.CalcUtil.multi(int,int)) and args(a, b)" id="multiPointCut" />
			<aop:pointcut expression="execution(* studio.guoliao.util.CalcUtil.devide(int,int)) and args(a, b)" id="devidePointCut" />
			<aop:pointcut expression="execution(* studio.guoliao.util.CalcUtil.*(..)) and args(a, b)" id="allPointCut"/>
			<!-- 
				可以在一个aspect中声明多个pointcut,pointcut指切入点,具体进行aop的方法
				其中allPointCut中的第一个*指:返回值类型;第二个*指所有的方法名;..指所有的参数
			 -->
			<aop:before method="before" pointcut-ref="addPointCut" />
			<aop:before method="before" pointcut-ref="subPointCut" />
			<aop:before method="before" pointcut-ref="multiPointCut" />
			<aop:before method="before" pointcut-ref="devidePointCut" />
			<!-- before指在执行方法前进行调用的方法 -->
			<aop:around method="around" pointcut-ref="allPointCut"/>
			<aop:around method="notBeZero" pointcut-ref="devidePointCut"/>
			<!-- around通知,该方法对应的第一个参数必须为ProProceedingJoinPoint类,具体的代理执行方法 -->
			<aop:after-throwing method="afterThrowing" pointcut-ref="allPointCut" throwing="e"/>
			<!-- 捕捉到异常执行的方法 -->
			<aop:after method="printTime" pointcut-ref="allPointCut"/>
			<!-- 执行完代理方法后执行的方法 -->
			<aop:after-returning method="printTime" pointcut-ref="allPointCut"/>
			<!-- 在返回后执行的方法 -->
		</aop:aspect>
	</aop:config>
</beans>

@Component可以和@Named互换

@Configuration @ComponentScan(basePackages="") 设置基本的包路径

@Bean为方法注解,将方法返回值保存为一个bean

转载于:https://my.oschina.net/u/3106243/blog/1631148

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值