一、全xml
- spring除了管理自己编写的资源,还可以管理第三方的资源。
步骤:
- 导入连接池和mysql驱动,jdbcTemplate的坐标(在pom.xml文件中)
- 将第三方的对象交给spring的ioc容器
<!-- 自己编写的资源也可以交给ioc容器创建并管理
accountDao-->
<bean id="accountDao" class="com.daoimpl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--accountService-->
<bean id="accountService" class="com.serviceimpl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
- 通过spring的依赖注入(DI)设置属性值。
- 优化:
-
- 在spring的配置文件中加载外部的数据库连接信息properties 配置文件,在内部通过el表达式${key}的方式获取。
<!--让spring去加载第三方的jdbc.properties配置文件
标签:<context>
使用:el表达式${key}
-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
- 连接池
<!-- druid -->
<bean id="druid" class="com.alibaba.druid.pool.DruidDataSource">
<!--set 属性-->
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--JdbcTemplet
new JdbcTemplet();
new JdbcTemplet("连接池");
-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--通过构造器设置属性-->
<constructor-arg name="dataSource" ref="druid"></constructor-arg>
</bean>
- 测试类:
DruidDataSource druidDataSource = new DruidDataSource();
//使用mysql驱动
druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
druidDataSource.setUrl("jdbc:mysql:///day02_spring");
druidDataSource.setUsername("root");
druidDataSource.setPassword("root");
DruidPooledConnection connection = druidDataSource.getConnection();
System.out.println(connection);
二、半xml半注解方式
2.1 需求:要使用ioc的 半xml半注解 的方式完成数据库的操作
-
半xml半注解:自己的资源用注解代替,第三方的资源全部保留xml配置
-
纯注解方式: 自己的资源和第三方资源全部用注解的方式。
-
半xml半注解的使用条件:
-
- spring对注解默认是关闭的,需要配置一个注解扫描器,开启注解并扫描资源。
-
- 注解扫描器的作用: 在指定的包路径下(包含子路径)解析添加的注解。使用标签< context >开启注解。
<!--开启扫描器-->
<context:component-scan base-package="com" ></context:component-scan>
- 加载第三方资源
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
- 创建druid连接池并与JdbcTemplate绑定
<bean id="druid" class="com.alibaba.druid.pool.DruidDataSource">
<!--set 属性-->
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--JdbcTemplet
new JdbcTemplet();
new JdbcTemplet("连接池");
-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--通过构造器设置属性-->
<constructor-arg name="dataSource" ref="druid"></constructor-arg>
</bean>
2.2 注解整理
2.2.1 创建对象的注解
- @Component
- @Controller — web层(控制层)
- @Service —service层(业务逻辑层)
- @Repository ----dao层(持久层)
- 需要配置在类上
- 默认该对象在spring容器中的唯一标识(id):就当前类名首字母小写
- 如果使用Value属性:可以指定唯一标识(id)
- 这4个注解可以随便用,但是建议按照分层方式对应注解。
@Component(value = "accountService")
//<bean id="accountService" class="com.serviceimpl.AccountServiceImpl">
public class AccountServiceImpl implements AccountSerivce {
//spring解析到这个注解,会自动去自己的ioc容器中找当前这个类型(接口)的对象,并自动赋值
@Autowired
@Qualifier(value = "accountDao1")
private AccountDao accountDao;
public void delete() {
//调用dao
accountDao.delete();
}
2.2Spring提供的对象属性的依赖注入注解
- @Autowired:
-
- 默认按照类型(接口)从容器中查找对象并注入。如果该容器中有多个该接口的对象,可以使用下面注解配套
-
- @Qualifier:
-
-
- 按照指定的唯一标识从容器中查找对象并注入。value: 指定唯一标识(id)
-
@Repository(value = "accountDao1")
public class AccountDaoImpl implements AccountDao {
@Autowired //依赖注解
private JdbcTemplate jdbcTemplate;
public void delete() {
String sql="delete from account";
jdbcTemplate.update(sql);
}
2.3 JDK提供的依赖注入注解(@Autowired+@Qualifier(value=id唯一标识名))
- @Resource: 按照唯一标识从容器中查找对象并注入。
- · Name: 指定唯一标识
2.4 简单属性的依赖注解
- @Value:
-
- 注入基本数据类型
-
- 注入被spring容器管理的properties文件中的内容
-
-
- 将properties交给spring容器管理
-
-
-
- 使用spring-el表达式获取,@Value="${properties文件中的key}"
-
- AccountDaoImpl2类下
@Repository(value = "accountDao2")
@Scope(value = "singleton") // 默认为单例
public class AccountDaoImpl2 implements AccountDao {
// @Value(value = "com.mysql.jdbc.Driver") 直接赋值
@Value(value = "${jdbc.driver}") //条件:spring必须加载过第三方的jdbc.properties文件
private String driver;
//@Value(value = "jdbc:mysql:///day02_spring")
@Value(value = "${jdbc.url}")
private String url;
//@Value(value = "root")
@Value(value = "${jdbc.username}")
private String username;
//@Value(value = "root")
@Value(value = "${jdbc.password}")
private String password;
}
2.5 spring提供的生命周期的注解
xml方式:
- Scope: 对象的作用域
- Destory-method :销毁方法
- Init-method :初始化方法
注解的方式:
-
@Scope:对象的作用, 配置在类上
-
- Value属性:(singleton | prototype)
-
@POSTConstruct:配置到方法上
-
- 配置初始化方式
-
@PreDestory:配置到方法上
-
- 配置销毁方法
-
AccountDaoImpl2类下
//初始化的方法
@PostConstruct
public void start(){
System.out.println("这是初始化的方法");
}
//销毁的方法
@PreDestroy
public void end(){
System.out.println("这是销毁的方法");
}
三、spring的注解编程
纯注解: 替换xml配置文件。
- 条件:使用配置类替换xml配置文件。
-
- 代码前端不在加载配置文件, 而是加载配置类。
3.1 注解
- @Configuration: 声明配置类
- @ComponentScan: 开启包扫描
- PropertySource: 将properties零配置文件交给spring容器管理。
3.2 步骤
3.2.1 测试类
- 加载配置类
- 获取accountService对象,并放入容器中
public static void main(String[] args){
// 加载配置文件---没有---不能再用ClassPathXmlApplicationContext
// 配置类代替配置文件---不在加载配置文件了,要加载配置类---AnnotationConfigApplicationContext
//AnnotationConfigApplicationContext作用:加载配置类
ApplicationContext applicationContext=new AnnotationConfigApplicationContext(SpringConfig.class);
//问容器要service对象
AccountService accountService=(AccountService)applicationContext.getBean("accountService");
accountService.delete();
}
3.3 SpringConfig配置类
@Configuration //声明当前类是一个配置类,还会自动调用类中所有方法
@ComponentScan(basePackages ="com")
//要把自己的资源用注解代替---开启一个扫描器
//<context:component-scan base-package="cn.itcast"></context:component-scan>
//加载外部的第三方配置文件 <context:property-placeholder location="classpath:jdbc.properties">
@PropertySource(value = "classpath:jdbc.properties")
@Import(value = JdbcConfig.class)//集成别的配置类
public class SpringConfig {
}
3.4 AccountServiceImpl类
- 根据配置类中开启的扫描器,定位到此类
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public void delete() {
//调用dao
accountDao.delete();
}
}
3.5 AccountDaoImpl 层
@Repository(value = "accountDao")
public class AccountDaoImpl implements AccountDao {
@Autowired //依赖注入
private JdbcTemplate jdbcTemplate;
public void delete() {
String sql= "delete from account ";
jdbcTemplate.update(sql);
}
3.3 @Bean
- @Bean : 配置到方法上,表明此方法的返回值交给spring容器管理
-
- 将第三方对象交给spring容器管理的时候使用@Bean
* @Bean
* 使用:需要配置到方法上,此方法需要返回值
* 含义:将此方法的返回值交给spring容器管理
* 流程:当程序启动的时候(创建spring容器),扫描所有配置了@bean注解的方法,
* 执行相关方法得到返回值,将返回值存入spring容器
* name:唯一标识
*/
/**
* 创建dataSource
*/
@Bean(name="dataSource")
public DataSource getDataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setUsername("root");
ds.setPassword("111111");
ds.setUrl("jdbc:mysql:///springdb");
ds.setDriverClassName("com.mysql.jdbc.Driver");
return ds;
}
四、 spring整合junit(单元测试的注解)
4.1 环境搭建的步骤
- 导入SpringJunit的坐标
- 通过注解指定谁去加载配置文件
- 通过注解指定配置文件的位置
- 记载成功,可以注入并测试
4.2 SpringJunit
-
导入坐标
-
- Junit:单元测试
-
- Spring-test:spring整合junit的支持包
-
@Runwith(SpringJunit4ClassRunner.class) : 声明spring提供的单元测试环境
-
@ContextConfiguration : 声明spring的配置信息
-
- Locations:xml配置文件
-
- Classes:配置类的字节码
// 加载配置文件或者配置类 ioc容器中才会有个个对象
@RunWith(value = SpringJUnit4ClassRunner.class) //加载配置文件或者是配置类
//@ContextConfiguration(locations = "classpath:bean.xml") //指向配置文件的位置
@ContextConfiguration(classes = SpringConfig.class) //指向配置类
public class Demo2_springJunit {
@Autowired // ioc容器有对象
private AccountService accountService;
@Test //做删除
public void t1(){
accountService.delete();
}
}