Spring学习知识点

xiaoxiao2021-02-28  90

1,如何导入约束       新建Spring的配置xml文件     //可以在任意位置         通过catalog 导入约束文件 约束名是去掉文件名的部分       写《beans》《/beans> 根元素 标签        切换到设计视图         点击beans 点击add           选择 xsi点击 OK   再点击 add    选择 specify new namespace  选择brows 从配置的中找到 约束问价beans    2, BeanFactory:每次在获得对象时才会创建对象      applicationContext:启动时就会创建配置的对象,并提供更多的功能    在web开发中使用applicationContext 在 资源匮乏的环境用beanFactory‘ 3,<bean></bean> : id :不能重复 不能有重复字符 和name的作用一样 name可以重复 能用特殊字符  4,Spring 创建对象有三个个方法       1,构造函数    <bean name="user" class="类名"></bean>           2,工厂创建    静态工厂创建: <bean name="user2" class="com.shi.day1.MyFactory" factory-method="createUser"></bean> com.shi.day1.MyFactory:工厂类中有产生对象的createUser()静态方法  实例工厂 <!-- 实例工厂 -->        <bean name="myfactory" class="com.shi.day1.MyFactory"></bean>    先创建myfactory对象        <bean name="user3" factory-bean="myfactory" factory-method="createUser2"></bean> 再通过myfactory 对象调用他的非静态方法 createUser2 创建 user对象 5,<bean标签 scope 属性:        1,值 : singleton  : 单例对象 默认   2,值:prototype:多列 :每次创建都是新对象  整合Struts2 必须整合为多列   3,值request: web 生命周期和request一致   4,值session :web 生命周期和session一致     6 bean 标签生命周期属性:      1,初始化方法 在Spring 创建bean对象 时被调用          init-method=""  2,销毁方法  在Spring 销毁bean对象时 被销毁        destroy-method="" 7 Spring模块化配置    调用其他Spring配置文件 <import resource="相对路径"/> 8 Spring 属性注入:    1,setter <!-- 为user注入值 -->                  <!-- 为user注入值 -->           <bean name="user" class="com.shi.day1.User"> <property name="name"  value="tom"></property> <property name="age" value="12"></property> 引用类型 <property name="car" ref="car"></property> </bean> 2,构造该方法     类里有带参数的够着方法   <bean name="user" class="com.shi.day1.User"> <constructor-arg name="name" value="Judy" ></constructor-arg> 引用类型 <constructor-arg name="car" ref="car"></constructor-arg> </bean constructor-arg: 参数index=“0” 构造函数的第一个参数是该名字的  参数 type=“Java.lang.interger” 构造函数参数类型是 interger的 3,p名称空间注入    1,导入p命名空间   xmlns:p= "http://www.springframework.org/schema/p" 2 创建对象    <bean  name="user" class="com.shi.day1.User" p:name="tom" p:age="18" p:car-ref="car"></bean> 4,spel 注入    spel :Spring Expression language   spring表达式语言 <bean name="user" class="com.shi.day1.User"> <property name="name"  value="#{user1.name}"></property> <property name="age" value="#{user2.age}"></property> 引用类型 <property name="car" ref="car"></property> </bean> 9,复杂类型注入    数组类型、List、 set、Map,properties Array(数组)   <property name="arr" ></property>   1,如果 数组Arr 只有一个值 可以用 value 和 ref   2,多个     <property name="arr" >   <array>     <value>11</value> <value>tom</value> <ref bean="user2">   </array> </property> list,set <property name="mList" value="haha" ></property>   1,如果 数组Arr 只有一个值 可以用 value 和 ref   2,多个     <property name="mList" >   <array>     <value>11</value> <value>tom</value> <ref bean="user2">   </array> </property> map    <property name="mMap" >   <map>     <entry key="name" value="briup"></entry> <entry key="user" value-ref="user3"></entry> <entry  key-value="user1" value-ref="user2"></entry>   </map> </property> properties  <property name="prop" >   <props>     <prop key="name>briup</prop> <prop key="user">briup</prop>   </props> </property>     10,spring容器随项目的启动而启动    <listener>  <listener-class> org.springframework.web.context.ContextLoaderListener</listener> </listener> Spring 配置文件的位置 <context-parame>   <parame-name>contextConfiglocation</parame-name>  <parame-value> classpath:applictonContext.xml</parame-value> </context-parame> 11,在项目中获得Spring容器   在application域中获得即可       1,获得servletContext对象      ServletContext sc= ServletActionContext.getServletContext(); 2,获得容器 WebApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(sc); 12,注解开发 包 aop      导入context    <context:component-scan base-package="com.shi.day2"></context:component-scan>   扫描com.shi.day2这个包的所有路径   在类中写入注解    13,bean 的注解    @component   //早期注解 创建对象 @service     //用于表明是 service层的bean注解 和其他功能没有区别 @controller   //用于表明是 web层的bean @repository    //用于表明是dao层的bean  14,bean 的属性注解      @scope  在类上面加,不在 13条的里面 是并列的  @scope(scopeName=“prototype”) //默认是单列的 加这个改成多列的 普通属性 在属性上面 或者在setter方法上 @value() 给属性赋值  区别 : 属性上通过 反射flied赋值   //破坏了对象的封装性          setter 上通过set赋值  应用类型 在属性上 写  @Autowired 主动装配 //如果一个类型多个对象,无法选择注入那个对象  @Qualifier("对象名") // 告诉对象用哪个对象 与autowired连用    @resource(name=“对象名”) 手动注入    15,bean 的方法的注解               1,加@PostConstruct 在对象创建后调用                2,加 @PreDestroy 在对象销毁前调用    16,spring 与Junit 联合测试 包test      @runwith(SpringJUnit4ClassRunner.class) //帮我们创建容器   @Contextconfiguration("classpath:applicationContext.xml")//指定创建容器所用的配置文件  17,aop    横向重复,纵向抽取 能够为Spring容器管理的代理对象产生动态代理对象 cglib 代理 对目标对象产生继承代理 能对任何对象进行代理(没有被final) spring 这两种代理方式都使用: 有接口使用jdk代理  和目标对象无关 无接口使用cglib    是继承目标对象 <!-- jdk 代理关键代码>    private UserService us;//目标对象 public UserServiceProxyFactory(UserService us) { super(); this.us = us; //获得目标对象 } public UserService getUserServiceProxy(){ UserService uProxy= (UserService) Proxy.newProxyInstance(UserServiceProxyFactory.class.getClassLoader(),  UserServiceImpl.class.getInterfaces(),this );  //实现InvocationHandler接口写通知/增强 return uProxy; } <!--cglib 代理 关键代码> Enhancer  eh= new Enhancer(); //帮助生成代理对象 eh.setSuperclass(UserServiceImpl.class); //设置代理的类 eh.setCallback(this);   //callback的子接口 MethodInterdept  实现它 (处理代理后的操作) UserServiceImpl us = (UserServiceImpl) eh.create(); 18,aop 相关名词的解释 joinpoint(连接点)  目标中可以增强的点(对象的方法) piontcut(切入点)   目标中已经增强的点(对象要被代理的方法) advice(通知/增强)    增强的代码(比原有方法多出来的功能) Target(目标对象)   被代理的对象 要被添加处理的类 weaving(织入)    把通知应用到切入点的过程 proxy(代理)  把通知织入目标对象 形成代理 切面   切面=切入点+通知 19,Spring 的通知(自定一个通知类 定义一些通知方法)    1,前置通知    在目标方法运行前调用 2,后置通知     在目标方法调用后调用(出现异常不调用) 3,环绕通知      在目标方法调用前后都调用 4,异常通知 如果出现异常就会调用 5,后置通知      在目标方法出现后调用(出现异常也调用) 20,如何把通知类织入目标对象 <!-- 配置目标对象 --> <bean name="userService" class="com.shi.day2.UserServiceImpl"></bean> <!-- 配置通知对象 --> <bean name="myAdvice" class="com.shi.day2.MyAdvice"></bean> <!-- 将通知织入目标对象 --> <aop:config> <!-- 配置切入点  public void com.shi.day2.UserServiceImpl.save() *com.shi.day2.UserServiceImpl.save() * com.shi.day2.UserServiceImpl.*()这个类的所用无参方法 * com.shi.day2.UserServiceImpl.*(..)这个类的所用方法 参数不限制 *com.shi.day2.*ServiceImpl.*(..) 后面为ServiceImpl类的所用方法 --> <aop:pointcut expression="execution(* com.shi.day2.*ServiceImpl.*(..))" id="pc"/> <!-- 描述通知 --> <aop:aspect ref="myAdvice"> <aop:before method="before" pointcut-ref="pc"/> <aop:after-returning method="afterRunning" pointcut-ref="pc"/> <aop:around method="around" pointcut-ref="pc"/> <aop:after-throwing method="afterException" pointcut-ref="pc" /> <aop:after method="after" pointcut-ref="pc"/> </aop:aspect>         </aop:config>    21,Spring 注解开发aop       在配置文件中加入   <!-- 将通知织入目标对象 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 在通知类上注解 @Aspect  //表示该类是通知类 在前置通知前加 @before // 指定该方法是前置通知 并指定切入点           @Before("MyAdvice2.pointCut()")  //向MyAdvice2的pointCut()方法获取切入点   向MyAdvice2的pointCut()方法应注释 @Pointcut("execution(* com.shi.day2.*ServiceImpl.*(..))") public void pointCut(){}   在后置通知前加   //后置通知 出现异常不会调用                 @AfterReturning("execution(* com.shi.day2.*ServiceImpl.*(..))") //手写切入点 22,Spring 整合Jdbc    Spring 提供了一个整合jdbc的对象,对象封装了jdbc技术  JDBCTemplate:模板对象  1,用c3p0提供线程池 提供数据源     引入c3p0的jar 包 //准备连接池       ComboPooledDataSource dataSource= new ComboPooledDataSource();     dataSource.setDriverClass("com.mysql.jdbc.Driver");     dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8");    dataSource.setUser("root");    dataSource.setPassword("admin"); //创建jdbc模板对象    JdbcTemplate jt= new JdbcTemplate(); jt.setDataSource(dataSource); //书写sql语句并且执行 String sql="insert into  user values('张三','天王盖地虎','二班')"; jt.update(sql); //在spring容器中配置 数据源 和JDBC模板 <!-- 将连接池放入 Spring 容器 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 在xml配置文件中配置数据库utl时,要使用&的转义字符也就是 &--> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"></property> <property name="driverClass"  value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="admin"></property> </bean> <!-- 将jdbcTemple 放入Spring 容器 --> <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean>    dao层继承JDBCDaoSupport 会根据线程池创建模板 这是的dao不在依赖于jdbc模板 而是依赖于 线程池 所有在Spring的容器中配置为 <!-- 将连接池放入 Spring 容器 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8"></property> <property name="driverClass"  value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="admin"></property> </bean> <!-- 将UserDao放入 Spring 容器  从连接池子中获取JDB模板--> <bean name="userDao" class="com.shi.day3.jdbcTemplate.UserDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> 在Dao层 的类中获取Jdbc的方法是 : super.getJDBCTample();      23,Spring 读取外部的配置文件        <!-- 读取外部的配置文件 --> <context:property-placeholder location="com/shi/day3/db.properties"/>  24,Spring aop事务管理      Spring封装了 打开事务的代码  提交事务的代码 回滚的事务  Spring为各个平台的事务管理提供了接口 PlatformTransactionManager;  为不同的平台提供不同的类:JDBCTransactionManager、HibernateTransactionManager  25,spring 管理实务的属性      1,事务的隔离级别  2,事务是否只读  3,事务的传播行为     业务方法之间(service 方法) 互相调用事务如何处理?(Spring 提供)     PROPAGATION_REQUIRED  :支持当前事务不存在就创建一个事务 (默认 使用这个就行)   26,管理事务的方法 1,代码管理   在配置文件中配置事务管理模板 <!-- 读取外部的配置文件 -->   <context:property-placeholder location="com/shi/day3/db.properties"/>   <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">   <property name="dataSource" ref="dataSource"></property>   </bean>   <!-- 事务管理模板-->  <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">   <property name="transactionManager" ref="transactionManager"></property>  </bean>    在service声明 事务管理对象  private TransactionTemplate tt;  并调用   tt.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus arg0) { // TODO Auto-generated method stub ad.addMoney(to, money);  int i=1/0;   ad.derceMoney(from, money); } }); 2,xml管理aop 事务 <!--事务通知   <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes>  <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>   <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>    <!--  统配前缀为save的方法--> </tx:attributes> </tx:advice>--> <!-- 配置织入  <aop:config> <aop:pointcut expression="execution(* com.shi.day3.transactionMarrager.AccountServiceImpl.*(..))" id="txPc"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/> </aop:config>--> 3,注解管理aop 事务     配置文件中  注册自动                    <tx:annotation-driven/> 在上使用为该方法的事务 在类上加注解 是类里所有方法的事务 (有特殊的可以在方法上在控制) @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
转载请注明原文地址: https://www.6miu.com/read-73821.html

最新回复(0)