Spring学习笔记(一)
来源:互联网 发布:算法导论 mobi 中文 编辑:程序博客网 时间:2024/06/10 07:02
导包:
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。
在类中@Aspect注解表示切面,类中的方法使用@Before、@After、@AfterReturning、@AfterThrowing
- Spring学习笔记(一)
- Spring 学习笔记(一)
- spring学习笔记(一)
- Spring学习笔记(一)
- Spring学习笔记(一)
- spring学习笔记(一)
- Spring 学习笔记(一)
- spring学习笔记(一)
- Spring 学习笔记(一)
- Spring学习笔记(一)
- Spring学习笔记(一)
- spring学习笔记(一)
- Spring学习笔记(一)
- spring学习笔记(一)
- Spring学习笔记(一)
- Spring 学习笔记(一)
- Spring学习笔记(一)
- Spring 学习笔记(一)
- RobotFrameWork控制流之if语句——Run Keyword If
- live555 传输方式(TCP&UDP)
- 服务器配置--基础知识--菜鸟备用
- python 格式对齐很重要!!!
- java基础,流程控制
- Spring学习笔记(一)
- RabbitMQ消息队列入门篇(环境配置+Java实例+基础概念)
- ElasticSearch基础教程
- unity3d使用感触(一个新公司的项目负责人的感触)
- 上课解除教师机控制(红蜘蛛)超详细
- Exsi6.5密码更改
- 使用数据泵跨版本导出导入数据
- 递归整理及几个经典题目
- fastjson报autotype is not support