Spring学习笔记-第二天:Spring数据源的创建/配置/Spring注解开发(1)
Spring配置数据源(连接池)
数据源(连接池)的作用
1.提高程序性能
2.实现实例化数据源,初始化部分连接资源
3.使用连接资源时从数据源中获取
4.使用完毕后将资源归还给数据源
常见的数据源(连接池):DBCP、C3P0、BoneCP、Druid等
数据源的手动创建
-
先声明变量
-
设置基本的连接参数(四种)
c3p0类型
ComboPooledDataSource dataSource=new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/bookstore");
dataSource.setUser("root");
dataSource.setPassword("123");
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
druid类型
DruidDataSource dataSource=new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/bookstore");
dataSource.setUsername("root");
dataSource.setPassword("123");
DruidPooledConnection connection=dataSource.getConnection();
System.out.println(connection);
connection.close();
参数字符串与实际数据库耦合了,因此参数可以抽取到properties。
之后
1. 设置jdbc.properties 配置路径,用户名,密码等
2. 使用ResourceBundle函数,调用其中的getBundle函数,获取properties
3. 其中getBundle函数内部加载路径只要基本名称:jdbc
4. get到之后,可以调用getString等函数,输入key,获取对应参数
5. 获取到的参数可以被数据源调用,进而实现解耦
ResourceBundle rb=ResourceBundle.getBundle("jdbc");
String driver=rb.getString("jdbc.driver");
String url=rb.getString("jdbc.url");
String username=rb.getString("jdbc.username");
String password=rb.getString("jdbc.password");
//创建数据源对象,设置连接参数
ComboPooledDataSource dataSource=new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
Connection connection=dataSource.getConnection();
System.out.println(connection);
connection.close();
Spring配置数据源
c3p0或druid,本身不是我们所定义的,是第三方的,也可以自己Spring定义数据源
回到Spring搭建的步骤
- 导入坐标
- 创建service,dao等(但这里因为是导入第三方jar包,所以不用再创建)
- 创建配置文件-applicationContext.xml
- 创建bean,导入id=dataSource注入,class使用函数的地址
- 内部数值注入,property 注入driver url user password等
- 在代码中使用getBean的方式调用
- 通过对应id调用
- 通过对应对象的类型调用
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bookstore"></property>
<property name="user" value="root"></property>
<property name="password" value="123"></property>
</bean>
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
//DataSources dataSource =(DataSources) app.getBean("dataSource");
DataSource dataSource = app.getBean(DataSource.class);
//DataSource是Javax.sql中的
Connection connection = dataSource.getConnection();
System.out.println(connection);
connection.close();
在设置Xml配置时,要注意C3P0与druid的不同,设置名称(set后面的名字)
也可以依此解耦,不用修改源码。
但是在实际开发中,让数据库的配置,分开properties,进一步解耦
若想让xml加载properties,首先要加载,得引入Context的命名空间(xmlns)
xmlns="http://www.springframework.org/schema/beans"
->
xmlns:context="http://www.springframework.org/schema/context"
还要改schema,把beans的东西改成Context的
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
然后通过context标签 property-placeholder进行加载 location确定路径
<context:property-placeholder location="classpath:jdbc.properties"/>
然后就在property中引用值
<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>
Spring注解开发
Spring轻代码重配置,配置比较繁重,注解代替xml配置文件可以简化配置,提高开发效率
Spring原始注解
Spring原始注解主要是替代
包括:
- @Component 使用在类上用于实例化Bean
- @Controller 使用在web层类上用于实例化Bean
- @Service 使用在service层上用于实例化Bean
- @Repository 使用在dao层类上用于实例化Bean
- @Autowired 使用在字段上用于根据类型注入
- @Qualifier 结合Autowired一起根据名称进行依赖注入
- @Resource 相当于上面两个相加,根据名称进行注入
- @Value 注入普通属性
- @Scope 标注Bean的作用范围
- @PostConstruct 使用在方法上标注该方法是Bean的初始化方法
- @PreDestroy 是用在方法上标注该方法是Bean的销毁方法
对比xml配置开发与注解开发
xml开发:
- dao层设置UserDao与UserDaoImpl的函数功能
- service设置接口与实现,并在实现中注入dao,并调用函数
- xml配置:applicationContext中加入bean,设置id为userDao与id为userService ,并在service中设置property,引入name与ref
- web层使用Application构造引入,getBean获取,后使用save函数
注解开发
取消掉xml配置中的这一行
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"></bean>
而是在UserDao实现里面加上 @Component("userDao")
同样改掉UserService
剪掉
<property name="userDao" ref="userDao"></property>
然后在service中注入
@Autowired
@Qualifier("userDao"),来实现注入
然后“通知”Spring注解->在xml中配置组件扫描:
在使用注解进行开发时,需要在applicationContext.xml中配置组件扫描。作用是指定哪个包及其子包下的Bean需要进行扫描,以便识别使用注解配置的类,字段和方法
<context:component-scan base-package="com.itheima"/>
Component可以创建Bean对象,但并不能区分是哪一层,因此使用后面三个来实例化Bean
- @Controller 使用在web层类上用于实例化Bean
- @Service 使用在service层上用于实例化Bean
- @Repository 使用在dao层类上用于实例化Bean
然后对Dao和service进行替换
如果使用xml配置,set方法是要写的(反射)
若使用注解方式,set方法可以不写
@Qualifier也可以注释掉
因为只写Autowired时,根据数据类型从Spring容器中进行匹配的
但若userDao中有多个Bean就不能正常注入了
若想要进行按名称匹配,@Qualifie可以按照id的值从容器中进行匹配,但注意此处要结合Autowired一起用
若两个都注释掉,可以用@Resource(name="")也可以注入。Resource相当于两个相加
今日idea快捷键:
在接口功能实现时 alt+shift+enter快速生成对应函数体
psvm:快速生成主函数

浙公网安备 33010602011771号