配置文件

<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean name="user" class="cn.spring.test01.User" scope="singleton" lazy-init="default" init-method="initUser" destroy-method="distoryUser"></bean></beans>      

测试代码 /*** * 对象创建 scope =singleton 默认值 单例模式 * scope=protoytpe 原形 不是单例模式 * 区别 protoytpe 只有使用时候才创建对象 * singleton 默认启动的就创建 并且只创建一个 * lazy-init 默认为false 不延迟创建 启动的时候就创建(只对singleton有效) * lazy-init true 在用到对象的时候 才创建对象 * init-method 初始化方法的调用 * destroy-method 销毁方法的调用(注意要调用destory方法) * * */

    public static void main(String[] args) {// TODO Auto-generated method stubClassPathXmlApplicationContext ac  =new ClassPathXmlApplicationContext("cn/spring/test01/applicationContext.xml");System.out.println("创建容器了....");User user =(User) ac.getBean("user");System.out.println(user);ac.destroy(); }

bens的创建和scope模式有关 单利模式 下的懒加载 就是在需要的时候才创建对象 否则就是启动的时候就自动去创建对象 而且是唯一的单例对象 prototype模式是原形模式在需要的时候才创建对象

实体类对象

package cn.spring.test01;public class User {public int getId() {return id;}public User(){System.out.println("创建对象了...");}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}private int id;private String name;public void initUser(){System.out.println("初始化======");}public void distoryUser(){System.out.println("销毁对象=====");}
}

----------------------------------------------IOC容器的创建----------------------------------------------------------- (1)通过构造函数创建 配置文件如下

<!-- 调用有参函数创建对象 -->

  <bean id="user2" class="cn.spring.test01.User"><constructor-arg value="1" index="0" type="int" name="id"></constructor-arg><constructor-arg value="jakec" index="1" type="java.lang.String" name="name"></constructor-arg></bean>
有参构造函数```public User(int id,String name){System.out.println("调用有参构造函数"+id+"---"+name);this.id=id;this.name=name;}
(2)通过set方法依赖注入,最常用的方式
   <bean id="user" class="cn.spring.test02.User"><property name="id" value="555"></property><property name="name" value="yunzhishang"></property></bean>
 需要有对象属性的set方法public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}
(3)通过工厂模式

<!-- -工厂类实例方法创建对象 -->

 <bean id="factory" class="cn.spring.test01.UserFactory"></bean><bean id="user3" factory-bean="factory" factory-method="getInstance"></bean>
工厂模式中创建方法getInstance
private User getInstance(){       return new User(100, "通过工厂模式调用实例创建对象");
}
(4)通过set方式创建的两种方式demo
    <bean id="userDao" class="cn.spring.test02.UserDao"></bean><bean id="userService" class="cn.spring.test02.UserService"><property name="userDao" ref="userDao"></property></bean><bean id="userAction" class="cn.spring.test02.UserAction"><property name="userService" ref="userService"></property></bean>//内部bena的用法<bean id="userAction1" class="cn.spring.test02.UserAction"><property name="userService"><bean class="cn.spring.test02.UserService"><property name="userDao"><bean class="cn.spring.test02.UserDao"></bean></property></bean></property></bean>
(5)p标签的方式实现
  <bean id="userDao" class="cn.spring.test02.UserDao"></bean><bean id="userService" class="cn.spring.test02.UserService" p:userDao-ref="userDao"></bean><bean id="userAction" class="cn.spring.test02.UserAction" p:userService-ref="userService"></bean>
(6)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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"      //注意加入此命名xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">
   <!-- 开启注解扫描 --> <context:component-scan base-package="cn.spring.aop"></context:component-scan><!--开启AOP注解模式 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans> ``` 切片类: ``` @Component //加入IOC容器 @Aspect //声明为切片类 public class Aop {

 //切片表达式 表示 在UserDao下面的所有方法前都加入改方法
@Before("execution( * cn.spring.aop.UserDao.*(..))")
public void begin(){System.out.println("核心代码提交开始...");
}//切片表达式 表示 在UserDao下面的所有方法后都加入改方法
@After("execution( * cn.spring.aop.UserDao.*(..))")
public void end(){System.out.println("核心代码提交完成...");
}

}


(7)aop的xml 的配置
bean.xml的配置
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"     xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="UserDao" class="cn.spring.aopxml.UserDao"></bean>  <bean id="OrderDao" class="cn.spring.aopxml.OrderDao"></bean><bean id="aop" class="cn.spring.aopxml.Aop"></bean><aop:config><!-- 表示 对cn.spring.aopxml包下面的所有类 所有方法拦截 注意切片表达式是定位到方法--><aop:pointcut expression="execution(* cn.spring.aopxml.*.*(..))" id="pt"/><aop:aspect ref="aop"><!-- 在方法之前插begin方法 --><aop:before method="begin" pointcut-ref="pt"/><!-- 在方法之后插入end方法 --><aop:after method="end" pointcut-ref="pt"/></aop:aspect></aop:config>
</beans>
(8)spring 的jdbc的方法xml的配置如下
配置spring的dataSource的配置
    <!----注意 dataSource的class是com.mchange.v2.c3p0.ComboPooledDataSource 需要引入c3p0-><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql:///test"></property><property name="user" value="root"></property><property name="password" value="123456"></property><property name="initialPoolSize" value="3"></property><property name="maxPoolSize" value="10"></property><property name="maxStatements" value="100"></property><property name="acquireIncrement" value="2"></property></bean>
spring中的模板对象JdbcTemplate
      <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean>
  <bean id="userDao" class="cn.spring.jdbc.UserDao"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean>
代码:
public class UserDao {private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}public int save(){String sql="insert into trole(ID,TEXT) values(?,?)";int res=-1;res= jdbcTemplate.update(sql,UUID.randomUUID().toString(),"哈哈哥");return res;}}
(9)spring中的事物操作xml配置如下
<?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:p="http://www.springframework.org/schema/p"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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql:///test"></property><property name="user" value="root"></property><property name="password" value="123456"></property><property name="initialPoolSize" value="3"></property><property name="maxPoolSize" value="10"></property><property name="maxStatements" value="100"></property><property name="acquireIncrement" value="2"></property></bean>   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><bean id="userDao" class="cn.spring.transtion.UserDao"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean><bean id="userService" class="cn.spring.transtion.UserService"><property name="userDao" ref="userDao"></property></bean>   <!-- -事物管理器类 --><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean>  <!-- -配置事物增强 -->    <tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="get" read-only="true"/><tx:method name="find" read-only="true"/><tx:method name="*" read-only="false"/></tx:attributes></tx:advice> <!-- -aop配置  表示拦截- -cn.spring.transtion.UserService.Save(..) 应用事物><aop:config><aop:pointcut expression="execution (* cn.spring.transtion.UserService.Save(..))" id="pt"/><aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/></aop:config>
</beans>      

核心代码

public class UserService { @Resource private UserDao userDao; public int Save(){ int res=-1; res=userDao.save();

    return res;
}

}

public class UserDao {

private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;
}
@Transactional
public int save(){String sql="insert into trole(ID,TEXT) values(?,?)";int res=-1;res= jdbcTemplate.update(sql,UUID.randomUUID().toString(),"哈哈哥yeyey");return res;
}

}

(10)通过注解模式操作事物

<?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:p="http://www.springframework.org/schema/p" 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">

  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql:///test"></property><property name="user" value="root"></property><property name="password" value="123456"></property><property name="initialPoolSize" value="3"></property><property name="maxPoolSize" value="10"></property><property name="maxStatements" value="100"></property><property name="acquireIncrement" value="2"></property></bean>      <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property>
</bean><!-- 事务管理器类 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property>
</bean><!-- -开启扫描 --><context:component-scan base-package="cn.spring.annotion"></context:component-scan>

<tx:annotation-driven transaction-manager="txManager"/> </beans>

UserDao层

@Repository public class UserDao {

@Resource
private JdbcTemplate jdbcTemplate;public int save(){String sql="insert into trole(ID,TEXT) values(?,?)";int res=-1;res= jdbcTemplate.update(sql,UUID.randomUUID().toString(),"哈哈哥yeyey");return res;
}

}

UserService层

@Service public class UserService {

@Resource
private UserDao userDao;@Transactional   //加入事物
public int Save(){int res=-1;res=userDao.save();return res;
}

}


Action层
public static void main(String[] args) {ApplicationContext ac=new ClassPathXmlApplicationContext("cn/spring/annotion/bean.xml");UserService service=(UserService) ac.getBean("userService");int res= service.Save();if(res!=-1)System.out.println("保存成功");elseSystem.out.println("保存失败");
}

转载于:https://my.oschina.net/mclongyi/blog/755444

Sping的基础知识总结(01)相关推荐

  1. Java基础知识复习01:从基础知识---面向对象(后续会更新)

    在我的资源里有:pdf对应的完整内容需要的可以自行下载,无偿分享给大家 一.快捷键 Alt + / : 1)在java类中,要输出main方法,只要输入main然后用此快捷键按回车即可: 2)要输出S ...

  2. 安全 --- 内网基础知识(01)

    内网基础知识 (1)概念 内网也称局域网(Local Area Network,LAN)是指在某一工作区域内由多台计算机互联形成的计算机组,一般是方圆几千米内.局域网可实现文件管理.应用软件共享.打印 ...

  3. android des ecb加密_Android逆向 | 基础知识篇 01

    常见加密算法 编码 Base64 所有的数据都能被编码为只用65个字符就能表示的文本. 标准的Base64每行为76个字符,每行末尾添加一个回车换行符(\r\n).不论每行是否满76个字符,都要添加一 ...

  4. HTML基础知识笔记-01

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.HTML是什么? 二.使用步骤 1.注释的快捷键 2.段落标签 3.换行标签 4.水平线标签 5.文本格式化标签 ...

  5. IOS开发之——数据库-基础知识介绍(01)

    一 概述 IOS中数据存储方式 SQLite数据库 使用Navicat执行表格操作 二 IOS中数据存储方式 Plist(NSArray\NSDictionary):只能存数据和字典 Preferen ...

  6. 大数据_02【大数据基础知识】

    大数据_02[大数据基础知识] 01 什么是服务器 02 服务器类型 03 存储磁盘(硬盘) 04 什么是RAID RAID特点 RAID种类 05 什么是集群 06 什么是计算机网络 07 什么是交 ...

  7. 大数据_03【大数据基础知识】

    大数据_03 [大数据基础知识] 01 大数据概述 02 什么是大数据?(Big Data) 03 传统数据与大数据的对比 04 大数据的特点 4.1 传统数据与大数据处理服务器系统安装对比 4.2 ...

  8. Java_计算机基础知识

    Java_计算机基础知识汇总 01计算机基础知识 计算机 计算机硬件 计算机软件 软件开发 计算机语言 人机交互方式 键盘功能键及快捷键介绍 常用的DOS命令 02 Java语言概述 03 JDK的下 ...

  9. Oracle:ADG基础知识学习一20230515

    作者 | 夜说 个人公众号 | 夜说的数据库笔记 原文链接 | Oracle:ADG基础知识学习一20230515 目录 01前言 02ADG基础知识一 01前言 以下为20230515所学习的adg ...

最新文章

  1. 撩课-Web大前端每天5道面试题-Day7
  2. jni invalid jobject
  3. masonry的约束应该写在哪里_规划奇思|“中心城区”到底在哪里?和城镇开发边界是什么关系?...
  4. cad输入法自动切换_百度输入法 Linux 版本发布,支持 Ubuntu/Deepin
  5. [RabbitMQ]队列持久化
  6. Keras】基于SegNet和U-Net的遥感图像语义分割
  7. 基于LINQ to SQL的WEB开发三层架构(1)
  8. ssh框架http后台乱码问题
  9. matlab之结构体的创建与存储(CSV数据)
  10. OceanBase云平台简介
  11. python计算坐标点欧式距离_计算机视觉课堂笔记-4
  12. appium+python自动化98-非select弹出选择框定位解决
  13. 【NOIP2013】【Luogu1969】积木大赛(贪心,差分)
  14. AutoDesk EAGLE 9.6.2 Free版 在win10下闪退的解决方法
  15. C语言如何调用REFPROP软件,coolprop调用refprop计算流体的热力学性质和传输特性
  16. AM、FM、PM调制技术
  17. 一级路由器映射二级路由器端口映射
  18. 使用TimerOne库
  19. UIPinchGestureRecognizer 放大、缩小手势
  20. 从“黑五”看亚马逊海外购的变与不变

热门文章

  1. 2021-08-031179 最大的最大公约数
  2. 计量大学计算机学院,计算机科学与技术
  3. label标签的使用
  4. 编译原理学习笔记(十八)~LL(1)文法
  5. selenium学习——问卷星(可控比例)
  6. 面试官: 说一下前端组件设计的原则
  7. canvas画的北斗七星和大熊座
  8. C#二维码条码生成存入文本加缓存
  9. JAVASE相关知识点
  10. 久坐伤身?关于程序员站立式办公的体验,你会发现...