Spring知识点小结(二)
一、配置非自定义的Bean(数据源DataSource模型)
DBCP数据源:
导入dbcp的jar包:dbcp+pool+connector
代码实现:
//创建数据源对象
BasicDataSource dataSource = new BasicDataSource();
//设置数据库的基本参数
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///test");
dataSource.setUsername("****");
dataSource.setPassword("****");
//从数据源中获得连接资源
Connection connection = dataSource.getConnection();
//jdbc操作
System.out.println(connection);
C3P0数据源:
导入c3p0的jar包:c3p0+connector
代码实现:
//创建数据源对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//设置数据库的基本参数
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///test");
dataSource.setUser("****");
dataSource.setPassword("****");
//从数据源中获得连接资源
Connection connection = dataSource.getConnection();
//jdbc操作
System.out.println(connection);
在spring中配置数据源对象:
<!-- 将jdbc.properties文件加载到spring的容器中 需要用到context命名空间
classpath:类加载路径 开发环境下就是src
-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
二、Spring的注解开发
注解与xml配置文件的优缺点?
xml优点:解耦合 缺点:配置繁琐
注解优点:快 缺点:耦合
1、Spring原始注解
出现的目的主要是对自定义的Bean的xml配置的替代
1.1开发步骤:
1、导入额外jar:spring-aop.jar
2、在xml中开启组件扫描
<!-- 组件扫描:告知spring容器哪些包下的bean需要被扫描 -->
<context:component-scan base-package="com.cyxz"></context:component-scan>
3、在实体Bean上使用注解进行开发
@Component("customerDao")
public class CustomerDaoImpl implements CustomerDao
@Component("customerService")
public class CustomerServiceImpl implements CustomerService
@Autowired
private CustomerDao customerDao;
4、测试
1.2 注解的详解
IoC:创建对象
@Component(该Bean对象的标识/id):在需要被spring进行实例化Bean上,参数字符串可以不写,默认是当前类名的首字母小写
@Controller:控制器 使用web层的Bean上 例如Action
@Service:服务 使用service层的Bean 例如CustomerServiceImpl
@Repository: 仓库 使用dao层的Bean上 例如CustomerDaoImpl
DI:依赖注入,注解的注入可以省略set方法
@Autowired:自动按照实体的类型进行注入
@Qualifier("customerDao") //此处@Qualifier是按照名称匹配 但是在此处@Qualifier结合@Autowired一起使用
@Resource(name="customerDao") //@Resource=@Autowired+@Qualifier
@Value(值)
其他:
@Scope:配置bean的作用范围,取值singleton(默认)和prototype 注解配置默认是prototype,xml配置默认是singleton
@PostConstruct:指定初始化方法 //在构造之后执行
PreDestroy:指定销毁方法 //在销毁之前执行
注意;重点注解:@Controller、@Service、@Repository、@Autowired、@Scope
注意:开发准则:Bean是自定义那么使用注解配置、Bean是非自定义的那么使用xml配置
2、Spring新注解(次重点)
Spring的新注解出现的目的完全替代xml配置文件
使用配置类 替代 xml文件
使用注解 替代 标签
@Configuration //标注该类是一个spring的配置类
@ComponentScan("com.cyxz") //组件扫描
@Import({DataSourceConfiguration.class}) //引入其他的配置类
@PropertySource({"classpath:jdbc.properties"}) //加载jdbc.properties文件
@Value("${jdbc.driver}") //匹配spring容器中的key
@Bean(name="dataSource") //将该方法的返回值以指定的名称存储到spring容器中
三、Spring集成Junit
原有的测试类:
@Test
//测试从spring中获得Service实例对象
public void test1() throws Exception{
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService customerService = (CustomerService) app.getBean("customerService");
customerService.save();
}
spring集成junit:
开发步骤:
1、导入额外的jar
spring-test.jar
spring-aop.jar
junit.jar
2、通过注解的形式指定测试类 和 配置文件
执行测试类:@RunWith(SpringJUnit4ClassRunner.class)
指定配置文件(类):@ContextConfiguration("classpath:applicationContext.xml")
3、测试spring容器中的哪个对象就通过注解注入哪个对象
4、编写测试方法
代码实现:
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:applicationContext.xml") //加载配置文件
@ContextConfiguration(classes={SpringConfiguration.class}) //加载配置类
public class SpringJunitTest {
@Autowired
private CustomerDao customerDao;
@Autowired
private CustomerService customerService;
@Test
public void test1(){
customerDao.save();
}
@Test
public void test2(){
customerService.save();
}
}
Spring知识点小结(二)的更多相关文章
- Spring知识点小结(四)
一.JdbcTemplate(jdbc模版--抽取的工具) web阶段DBUtils: QueryRunner runner = new QueryRunner(dataSource); ...
- Spring知识点小结(三)
一.aop的简介 aop:面向切面编程 aop是一种思想,面向切面编程思想,Spring内部提供了组件对aop进行实现 aop是在运行期间使用动态代理技术实现的思想 aop是oop延 ...
- Spring知识点小结(一)
一.Spring的简介 1.spring是一个full-stack轻量级开源框架 2.spring的两大核心 IoC: inverse of control 控制反转:反转是对象 ...
- Spring知识点小结汇总
Spring部分 1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建UserService对象的控 ...
- Spring知识点总结(二)之Spring IOC
1.创建bean类,并在spring中进行配置交由spring来管理1. IOC(DI) - 控制反转(依赖注入) 所谓的IOC称之为控制反转,简单来说就是将对象的创建的权利及对象的生命周期的管 ...
- Hibernate知识点小结(二)
一.持久化对象和标识符 1.持久化类 配置完关系后,操作的实体对应的类,成为持久化类 (Customer) 2.持久化类标识符(oid:object id) 3.持久 ...
- Struts2知识点小结(二)
一.结果视图的配置 <result name="success">/success.jsp</result> 1.局部结果视图 ...
- SpringBoot 系列教程之事务隔离级别知识点小结
SpringBoot 系列教程之事务隔离级别知识点小结 上一篇博文介绍了声明式事务@Transactional的简单使用姿势,最文章的最后给出了这个注解的多个属性,本文将着重放在事务隔离级别的知识点上 ...
- disruptor笔记之四:事件消费知识点小结
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
随机推荐
- kotlin语法
https://try.kotlinlang.org/#/Examples/Hello,%20world!/Simplest%20version/Simplest%20version.kt /** * ...
- 20.远程分支&跟踪分支
远程分支 远程引用是对远程仓库的引用(指针),包括分支.标签等等. 你可以通过 git ls-remote (remote) 来显式地获得远程引用的完整列表,或者通过 git remote show ...
- Linux下分布式项目部署环境搭建与使用(druid-1.0.25.jar)数据库连接加密
一.JDK安装 1.执行命令:cd Downloads/ 2.上 传:jdk-8u111-linux-x64.tar.gz 到Downloads 3.执行命令:tar -zxvf jdk-8u111- ...
- JDBC事务和数据库事务嵌套的讨论 .
首先必须执行con.setAutoCommit(false)方法,将JDBC事务设置为手动提交,否则手动提交con.commit()无效,手动回滚con.rollback()引发SQLExceptio ...
- 【Leetcode】【Medium】Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- myeclipse 复制项目不包含svn或CVS目录
目前只记录到2个方法:(SVN和CVS都适用) 方法一:导出法 1.右击需要cp的目录,点击export,General/File System 2.next 3.确认你选择的目录,并勾选:Creat ...
- Hibernate、Mybatis与Spring Data JPA
从零开始集成Springboot+MyBatis+JPA https://www.jianshu.com/p/e14c4a6f6871 MyBatis 与Hibernate的区别 http://xhr ...
- nginx的一些文章
[译] Nginx如何做流量控制 https://legolasng.github.io/2017/08/27/nginx-rate-limiting/ Nginx特性及原理介绍 http://www ...
- easyui学习笔记4—panel的实现
这篇看看easyui是怎么实现panel的,就是类似一个容器,里面可以装具体内容或者其他的easyui控件. 1.这里先看看引用的资源文件 <link rel="stylesheet& ...
- web 应用程序转化为多租户 SaaS 解决方案
web 应用程序转化为多租户 SaaS 解决方案 https://www.ibm.com/developerworks/cn/cloud/library/cl-multitenantsaas/inde ...