春季如果要使用注解开发
必须:applicationConntext.xml文件中添加一个标签添加一个jar包//注解开发测试spring4.0需要多一个jar包
添加一个包扫描配置
<context:component-scan base-package =“”/>
/ *包配置扫描包括添加的那个标签* /
豆的注解开发
@Component(“userService”) //注册Bean实例相当于
<bean name =“userService”class =“....”> </ bean>
与
@Service //用于Service @Rpository //用于DAO层@Controller //用于控制层(表现层)等效@Autowired //默认根据类型注入@Qualifier(“userDao”)//配合@Autowired是根据bean注册名称进行注入@Resource(name =“userDao”)//这一个注解就可以完成根据bean注册名称进行注入@value(“张三”)//普通的属性注入@Scope(“prototype” )//作用域@PostConstruct //相当于init-method =“myInit”初始化配置
Junit4整合
@RunWith(SpringJUnit4ClassRunner.class) // spring整合junit4@ContextConfigguration(locations =“classpath:applicationContext.xml”) //指弹簧配置文件位置
豆的注入
通过IOC模式进行解耦IOC实现原理:反射+配置文件+ 工厂
IOC包含DI依赖注入//依赖注入:在春天创建对象的过程中,对象所依赖的属性通过配置注入对象中
工厂
的BeanFactory与ApplicationContext的可以都进行实例化对象但是只使用ApplicationContext中的方法获取Bean的对象
Bean工厂:延迟加载
的ApplicationContext:配置文件加载时就加载实例类
春天与网络整合
1:需要倒入一个罐子包2:在wenb.xml中配置侦听
<听者> <监听级> org.springframework.web.context.ContextLoaderListener </监听级> </听众>
3:在web.xml文件中配置
<的context-param>
<PARAM名称> contextConfigLocation的</ PARAM名称>
<PARAM值>类路径:applicationContext.xml中</ PARAM值>
</的context-param>
豆的生命周期(大致)
1:加载配置使对象实例化2:封装的Bean的属性3:调用自定义初始化方法方法4:执行业务处理5:调用自定义的破坏法方法(只有范围= “singletion” 有效执行)豆的实例化方式
1:无参数构造方式
一个豆类配置:
<bean name =“”class =“”> </ bean>
2:静态工厂方法
需要一个豆类,一个BeanFactory的配置:
<bean name =“bean”class =“.... beanFactory”factory-method =“method”> </ bean>
3:非静态工厂方法
需要一个豆类,一个BeanFactory的配置:
<bean name =“bean”class =“..... BeanFactory”> </ bean>
<bean name =“bean”factory-bean =“beanFactory”factory-method =“method”> </ bean>
注入方式
构造器注入:
<bean id =“bean”class =“....”>
<constructor-arg index =“0”type =“java.lang.String”value =“奔驰”> </ constructor-arg>
<constructor-arg index =“1”type =“double”value =“1000”> </ constructor-arg>
</豆>
二传手方法注入:
<bean id =“car1”class =“....”>
<property name =“name”value =“宝马”> </ property>
<property name =“price”value =“1000”> </ property>
</豆>
REF属性引入:
<property name =“car”ref =“car1”> </ property>
//注意这里裁判中的内容与上面的ID名相同
//表示在一个豆中引入另一个豆对象
规划环境地政司使用:
豆之间的注入:<property name =“dog”value =“#{dog1}”> </ property>
属性调用及方法使用:
<property name =“name”value =“#{person.name}”> </ property>
<property name =“age”value =“#{person.getAge}”> </ property>
//注意方法调用得到的结果可以进行运算
cp注入:
c名称空间解决了构造器注入
p名称空间解决了set注入
集合属性的注入
名单注入:
<列表>
<值>张三</值> //值用这种
<值> 10 </值>
<ref bean =“car”/> // ref属性用这种(bean对象属性)
</列表>
设置注入:
<设置>
<value> 10 </ value> // value用这种
<值>李四</值>
<ref bean =“car”/> // ref属性用这种(bean对象属性)
</设定>
地图注入:
<地图>
<entry key =“username”value =“李四”> </ entry> // value用这种
<entry key-ref =“person”value-ref =“car”> </ entry> // ref属性用这种(bean对象属性)
</地图>
属性注入:
//一个Map实现类key与value都是String类型
<道具>
<prop key =“company”> ITCAST </ prop>
<prop key =“price”> 1000 </ prop>
</道具>
AOP的一些术语
目标对象 目标 ,连接点 连接点 , 切入点切入点 ,通知 建议 ,切面 方面 ,织入 编织 ,代理 代理 ,引介介绍AspectJ的静态代理(很重要)
注意事项:
1:导入配置的名称空间xmlns:aop =“....”2:导入导一个jar包
使用<aop:config>:代理对象
<aop:pointcut>:声明切点
<aop:advisor>:声明切面
目标与通知还是用原有的配置
实例:( 重要)
<aop:config proxy-target-class =“true”> // proxy-target-class =“”声明使用哪种方法来进行代理jdk还是cglib false是首选jdk,true是cglib
<aop:aspect ref =“userServiceAdvice”>
<aop:pointcut expression =“execution(* * .del(..))”id =“delpointcut”/> //定义切点
<aop:before method =“”pointcut-ref =“delpointcut”/> //前置通知
<aop:after-returns method =“”pointcut-ref =“”/> //后置通知
<aop:after-throwing method =“”pointcut-ref =“”/> //异常抛出
<aop:around method =“”pointcut-ref =“”/> //环绕通知
<aop:after method =“”pointcut-ref =“”/> //最终通知
</ AOP:方面>
</ AOP:配置>
环绕通知:( 必须)
环绕通知方法需要定义一个参数ProceedingJoinPoint pjp还需要对象值= pjp.proceed //执行行为
通知加参数:(一般用来实行日志记录和权限的控制与性能监控,事务管理等)
1前置通知 :JoinPoint(它的方法可以获取目标通知信息)2后置通知 :JointPoint,Object(第二个参数需要在配置时加一个参数returns =“obj”,第二个参数目标方法返回值)
3环出通知 :见上(实现目标行为的调用)
4抛出异常 :JoinPoint,Throwable(第二个参数需要在配置时加一个参数投掷=“ex”,是用来接收抛出的异常)
5最终通知 :JoinPoint(可以完成资源释放)
注解使用的AspectJ
<context:component-scan base-package =“cn.itheima”/> //注解在哪个包下生效
@Component
//将这个类在spring容器中注册
@Aspect //声明当前bean就是一个切面
@Before(“execution(* * .s(...))”) //前置通知里面写切点表达式
@AfterReturning(value =“execution(* * .s(..))”,returns =“value”) //后置通知(JoinPoint jp,Object value)接收返回值returns =“value”
@Around(“执行(* * .s(..))“) //环绕通知
@AfterThrowing(value =”execution(* * .s(...))“,throwing =”ex“) //异常抛出通知(JoinPoint jp) ,Throwable ex)抛出异常throw =“ex”
@After(“执行(* * .s(...))”) //最终通知
<aop:aspectj-autoproxy proxy-target-class =“true”/> //自动代理proxy-target-class =“true”这个是设置用哪种代理方式false是首选jdk,true是cglib
示例:
@Pointcut(“执行(* * .s(..))”)
private void mypointcut(){}
@Pointcut(“execution(* * .del(..))”)
private void mypointcut2(){}
那么下面的就可以直接写
(“mypointcut()”) - 例:@Before(“mypointcut()”)
可以声明多个切点 - 例:@Before(“mypointcut()|| mypointcut2()” )
sprin事务管理(以下都是需要配置的信息)
基于XML配置声明式事务管理方案
第一步:在applicationContext.xml的文件中添加AOP与德克萨斯州的名称空间
<?xml version =“1.0”encoding =“UTF-8”?>
<beans xmlns =“http://www.springframework.org/schema/beans”
xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance”xmlns:context =“http://www.springframework.org/schema/context”
xmlns:aop =“http://www.springframework.org/schema/aop”xmlns:tx =“http://www.springframework.org/schema/tx”
的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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd“>
第二步:在applicationContext.xml的文件中配置
1:声明事务管理 <! - 声明式事务管理 - >
<! - 事务管理器 - >
<bean id =“transactionManager”class =“org.springframework.orm.hibernate5.HibernateTransactionManager”>
<property name =“sessionFactory”ref =“sessionFactory”/>
</豆>
2:配置通知
<! - 通知 - >
<tx:advice id =“txAdvice”transaction-manager =“transactionManager”>
<TX:属性>
<tx:method name =“find *”read-only =“true”/>
<tx:method name =“add”/>
<tx:method name =“update”/>
<tx:method name =“del”/>
</ TX:属性>
</ TX:建议>
名称:必须的,对哪些方法进行事务控制
隔离:可选,事务事务隔离级别默认是DEFAULT
传播:可选,设置事务传播默认值REQUIRED
超时:可选,超时时间默认值-1
只读:可选,默认值是假的,如果不是只读,它可以对插入,UPDATA,删除操作,如果是只读不可以
回滚为:可选,设置一个异常,如果产生这个异常,触发事务回滚
无rolback换:可选,可以设置一个异常,如果产生这个异常,不会触发事务回滚
3:配置切面
<! - 切面 - >
<AOP:配置>
<aop:pointcut expression =“execution(* cn.itheima.service。* .. *(..))”id =“mypointcut”/>
<aop:advisor advice-ref =“txAdvice”pointcut-ref =“mypointcut”/>
</ AOP:配置>
基于注解声明式事务管理方案
需要在applicationContext.xml中中配置开启注解事务<! - 事务注解驱动 - >
<tx:annotation-driven transaction-manager =“transactionManager”/>
可以使用@Transaction来在类或方法上添加声明式事务管理(通常在服务上设置)
@Transaction中也有很多属性可以设置
杂项
JDK动态代理如果目标对象实现了接口优先使用这个
CGLIB动态代理
如果目标对象没有实现接口就是用这个代理方式
DriverManagerDataSource() - 创建数据库连接池
jdbcTemplate.queryForObject(“查询语句”,新的RowMapper <User>(){...})//返回一个复杂的数据类型用户
.query(new RowMapper <User>() {...})// List类型数据类型
new BeanPropertyRowMapper <User>(User.class)//封装成复杂数据类型
(如有错误,欢迎指出,谢谢!)