定义Bean

@Component 泛指组件,当组件不好归类时,可使用这个注解
@Repository 对应 DAO 层的 Bean 定义
@Service 对应 Service 层的 Bean 定义
@Controller 对应 Spring MVC 控制器的 Bean 定义

@Repository("bookDao")
public class BookDaoImpl implements BookDao {
    @Override
    public void save() {
        System.out.println("BookDao saving....");
    }
}

最后记得在配置文件扫描一下包就可以了~

<context:component-scan base-package="com.itheima"/>

纯注解开发的时候也不需要spring的配置文件了
在config包下新建一个类, 叫SpringConfig
@Configuration代表这是spring的配置文件 @ComponentScan("com.itheima")扫描的是bean 扫描多个包用数组{}

package com.itheima.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.itheima")
public class SpringConfig {
}

获取容器对象

ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);

注解控制bean生命周期

@Scope("singleton") 单例
@Scope("prototype") 多例
@PostConstruct 初始化方法
@PreDestroy 销毁方法

注解注入依赖

@Autowired 自动装配 同时也不需要提供setter
@Qualifier("bookDao") 指定需要注入的bean的名称

//具体来讲, 就是像这样用~
package com.itheima.service.impl;

import com.itheima.dao.BookDao;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service("bookService")
public class BookServiceImpl implements BookService {

    @Autowired
    @Qualifier("bookDao")
    private BookDao bookDao;

    @Override
    public void save() {
        System.out.println("bookservice save.....");
        bookDao.save();
    }
}

@Value("${name}") 注入基本数据类型
@PropertySource("jdbc.properties") 读取properties文件

@Configuration
@ComponentScan("com.itheima")
@PropertySource("jdbc.properties")
public class SpringConfig {
}

管理第三方bean

config 软件包下再建一个类, 存放第三方bean

package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class JdbcConfig {

    //定义一个方法返回你需要的第三方bean
    //加上@Bean表示当前方法的返回值是一个bean
    @Bean
    public DataSource dataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/druid");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        return druidDataSource;
    }
}

原来的 SpringConfig 导入新的类就可以了

@Import(JdbcConfig.class)

Spring整合Mybatis

只要把SqlSessionFactoryBean和MapperScannerConfigurer作为bean加入容器即可

SqlSessionFactoryBean负责连接DataSource
MapperScannerConfigurer负责扫描dao

package com.itheima.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MybatisConfig {

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setTypeAliasesPackage("com.itheima.domain");
        ssfb.setDataSource(dataSource);
        return ssfb;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.itheima.dao");
        return mapperScannerConfigurer;
    }
}

最后务必记得在spring的配置文件中导入mybatis的配置文件!!

package com.itheima.config;

import org.springframework.context.annotation.*;

@Configuration
@ComponentScan("com.itheima")
@PropertySource("jdbc.properties")
@Import({JdbcConfig.class, MybatisConfig.class})
public class SpringConfig {


}

Sprin整合junit

首先当然要在pom.xml中导入junit给spring量身打造的依赖~

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>

然后测试类这么写

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {

    @Autowired
    private BookService bookService;

    @Test
    public void testSave(){
        bookService.save();
    }
}