目录

1,管理对象(IOC详解)

1.1 :什么是IOC

1.2:Bean创建

1.2.1:Bean相关注解

1.3:依赖注入(DI)

1.3.1:什么是DI

1.3.2:按照名称注入

1.3.3:按照类型注入

1.3.4:注入简单数据:@Value

1.3.5:properies数据注入

1.4:@Bean注入第三方类

1.4.1:按照类型

1.4.2:按照名称

1.4.3:参数类型:引入数据

1.4.4:参数类型:简单数据

1.5:Bean作用域

1.5.1:概述

1.5.2:单例

1.5.3:多例

1.5.4:常量

1.6:生命周期

1.6.1:什么是生命周期

1.6.2:生命周期详解

1.6.3:方式一:详解-初始化&销毁

1.6.4:方式二:第三方@Bean

1.6.5:生命周期函数有什么用吗?


1,管理对象(IOC详解)

 

1.1 :什么是IOC

好处: 可以实现解耦, 让类和类之间的耦合度降低,  将对象的创建权交给Spring管理 

1.2:Bean创建

1.2.1:Bean相关注解

注解 描述
@Component 将修饰的资源交予spring管理。 value属性:为资源命名(唯一标识)
@Controller 衍生注解,与@Component作用和属性相同。特用于修饰==表示层==的资源。
@Service 衍生注解,与@Component作用和属性相同。特用于修饰==业务逻辑层==的资源。
@Repository 衍生注解,与@Component作用和属性相同。特用于修饰==数据访问层==的资源。

1.3:依赖注入(DI)

1.3.1:什么是DI

依赖注入(Dependency Injection,DI)Spring 容器在创建被调用者的实例时,会自动将调用者需要的对象实例注入给调用者。

注解 描述 修饰位置
@Resource(name=”…”) 按照指定名称注入对象 字段、setter方法
@ Resource 按照类型注入对象 字段、setter方法
@Value 注入简单值 字段、setter方法、参数
@PropertySource 加载properties配置文件

1.3.2:按照名称注入

public class 类名{
    @Resource(name="名称")
    private 类型 变量;
}

字段注入

    @Resource(name = "studentService4")
    private StudentService studentService;

 setter方法注入

    private StudentService studentService;
    @Resource(name = "studentService4")
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

1.3.3:按照类型注入

public class 类名{
    @Resource
    private 类型 变量;
}

1.3.4:注入简单数据:@Value

@Value 可以给成员变量注入、也可以给属性注入(getter/setter)

步骤1:目标类,User,进行普通数据注入

@Component
public class User {
    @Value("jack")
    private String username;
    @Value("18")
    private Integer age;
	//....
}

步骤2:配置类

@Configuration
@ComponentScan(basePackages = "com.czxy.demo05_di_value.domain")
public class Demo05Configuration {
}

 步骤3:测试类

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Demo05Configuration.class)
public class TestDemo05 {
    @Resource
    private User user;
    @Test
    public void testDemo5() {
        System.out.println(user);
    }
}

1.3.5:properies数据注入

编写properties文件,key=value

#key=value
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db2
jdbc.username=root
jdbc.password=1234 

使用@PropertySource("classpath:properties文件")加载properties文件,使用@Value("${key}")进行注入

 

 测试类

1.4:@Bean注入第三方类

注解 描述
@Bean 将第三方对象,添加到spring容器中,方法名为默认名。
@Bean(name = "") 按照指定名称,将第三方对象,添加到spring容器中。

1.4.1:按照类型

1.4.2:按照名称

1.4.3:参数类型:引入数据

@Bean
public 返回值 方法名(参数类型 参数名) {		//主动注入参数对象
}
public interface StudentDao {
    public void selectAll();
}
public class StudentDaoImpl implements StudentDao {
    @Override
    public void selectAll() {
        System.out.println("demo10 student dao ");
    }
}

模拟类:service,没有注解  

public interface StudentService {
    public void selectAll();
}
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao;
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
    @Override
    public void selectAll() {
        System.out.println("demo10 student service");
        studentDao.selectAll();
    }
}

 配置类

@Configuration
@ComponentScan(basePackages = {"com.czxy.demo10_bean_param"})
public class Demo10Configuration {
    @Bean
    public StudentDao studentDao() {
        return new StudentDaoImpl();
    }
    @Bean
    public StudentService studentService10(StudentDao studentDao) {
        StudentServiceImpl studentService = new StudentServiceImpl();
        studentService.setStudentDao(studentDao);
        return studentService;
    }
}

 测试类

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Demo10Configuration.class)
public class TestDemo10 {
    @Resource(name = "studentService10")
    private StudentService studentService;
    @Test
    public void testDemo() {
        studentService.selectAll();
    }
}

1.4.4:参数类型:简单数据

properties配置文件

配置类  

 

测试类  

1.5:Bean作用域

1.5.1:概述

注解 取值
@Scope singleton 默认值,单例的。整个spring容器只有一个
prototype 多例的。每获得一次创建一份

1.5.2:单例

dao,确定作用域方式

@Repository
@Scope("singleton")
public class UserDao {
}

配置类  

 测试类,注入2次,打印结果一样的。

1.5.3:多例

修改单例代码

1.5.4:常量

1.6:生命周期

1.6.1:什么是生命周期

1.6.2:生命周期详解

1.6.3:方式一:详解-初始化&销毁

 

配置类:  

 测试类:

1.6.4:方式二:第三方@Bean

 

 配置类,使用@Bean注册第三方对象,通过 initMethod 和 destroyMethod 两个属性设置初始化和销毁

测试类

 

1.6.5:生命周期函数有什么用吗?

释放资源:

public class 类名 {
    @Bean(destroyMethod="close")
    public DataSource datasource() {
        return new DruidDataSource();
    }
}

发表回复