Spring学习笔记(一)

导包:
这里写图片描述

1.IOC
控制反转,由程序创建的对象,改为通过工厂创建。

BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml").getBeanFactory();
或者
ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");

通过实现BeanFactoryAware接口获取Bean工厂。

获取bean

beanFactory.getBean("bean id或name");
beanFactory.getBean(User.class);

默认配置文件applicationContext.xml

<bean id="beanID" class="包名.类名" init-method="方法名" destroy-method="方法名" lazy-init="true" factory-method="创建bean的方法" factory-bean="创建bean的工厂类">
</bean>

标签的属性:
id,name表示bean对象的标识符
class表示bean对象的类型,完全限定名
scope表示范围(request,session,singleton单例、prototype多例)

2.DI
依赖注入:程序运行时由容器注入需要的对象。

①serrer(属性)注入

<bean id="类的别名" class="类完全限定名(包.类名)">
   <property name="类的属性" ref="该属性类型bean的id"/> 
   <property name="类的基本类型属性" value="值" />
</bean>

②构造器注入

<bean id="person" class="my.bean.Person">
 	<constructor-arg index="构造方法参数索引" ref="bean的ID" />
 	<constructor-arg value="值" />
 	<constructor-arg value="23" />
</bean>

注意构造方法中参数的顺序,构造方法不要有循环对象

③自动装配

<beans default-autowire="byName">
<bean id="person" class="my.bean.Person" autowire="byName">
</bean>

autiwire的值:no、default不自动装配,byName根据类的属性名去找匹配的beanID。
byType根据类的属性类型去匹配Bean的类型。

④List、Set、Map、Date注入

<bean id="room" class="com.po.Room">
	<property name="rname" value="my room"></property>
</bean>
<bean name="father" abstract="true">
	<property name="room" ref="room"></property>
</bean>
	
<bean id="formatdate" class="java.text.SimpleDateFormat">
	<constructor-arg value="yyyy-MM-dd"></constructor-arg>
</bean>
	
<bean name="person" class="com.po.Person" parent="father">
	<property name="list">
		<list>
			<value>100</value>
			<value>hello</value>
			<value type="java.lang.String">200</value>
		</list>
	</property>
	<property name="set" >
		<set>
			<value>200</value>
			<value>hello set</value>
		</set>
	</property>
	<property name="arr">
		<array>
			<value>300</value>
			<value>400</value>
		</array>
	</property>
	<property name="map">
		<map>
			<entry>
				<key>
					<value>scott</value>
				</key>
				<value>worker</value>
			</entry>
			<entry>
				<key>
					<value>tom</value>
				</key>
				<value>student</value>
			</entry>
		</map>
	</property>
	<property name="birthday">
		<bean factory-bean="formatdate" factory-method="parse">
			<constructor-arg value="2009-06-28"></constructor-arg>
		</bean>
	</property>
</bean>

*完整的配置文件头

<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:tx="http://www.springframework.org/schema/tx"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx.xsd
     http://www.springframework.org/schema/aop
<!-- 启用注解 -->
	<context:annotation-config />
	<!-- 启用自动扫描 -->
	<context:component-scan base-package="包名"></context:component-scan>

注解:
@Service、@Repository、@Component、@Controller作用相同,把被注解的类声明为spring的bean对象。
@Resource:自动装配(byName、byType)

@Component(“beanid”)注册指定beanid的bean
@Resource(name=“beanid”)通过beanid注入

3.AOP(面向切面)
切面简单理解就是代理,切面对应于关注点的行为的前期准备或收尾等工作。
切点(关注点、织点)——行为(方法)

XML配置方式

<aop:config>
	<aop:pointcut expression="execution(* com.biz.*.*(..))" id="自定义切点名"/>
	<aop:aspect ref="切面beanid">
		<aop:after method="切面方法" pointcut-ref="切点名" order="整数"/>
		<aop:after method="方法名" pointcut-ref="切点" />
		<aop:after-returning method=""/>
		<aop:after-throwing method=""/>
		<aop:around method=""/>
		<aop:before method="" pointcut-ref="" />
	</aop:aspect>
</aop:config>

execution表达式允许使用通配符表示类名、返回值、权限或方法名;…表示任意参数 * com.bean..()
com.biz.
.run(…)
com.bie.Obama.*(int)

order属性:控制执行顺序,before是order越大先执行,after是order越小先执行。

around切面方法必须有ProceedingJoinPoint参数,public Object around(ProceedingJoinPoint pjp),ProceedingJoinPoint对象的proceed方法执行切点方法。有返回值Object

<aop:around method="" pointcut-ref=""/>

around环绕通知:在切点执行前、后会执行,pjp.proceed();前的代码会在before之后执行,pjp.proceed();后的代码会在after之后执行。

注解方式:
在spring的xml配置文件中 aop:aspectj-autoproxy/启用aop。
在类中@Aspect注解表示切面,类中的方法使用@Before、@After、@AfterReturning、@AfterThrowing

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值