天天看点

Spring IOC 配置总结Spring IOC手动对象依赖注入自动对象的依赖注入Bean标签属性

Spring IOC

手动对象依赖注入

一、添加 IOC 核心坐标

​ pom.xml 文件中引入架包

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>


    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.8.1</version>
    </dependency>
           

二、基本的xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="userDao" class="com.dzy.Dao.userDao"></bean>
    <bean id="对象的唯一标识" class="业务对象全路径"></bean>
    
</beans>
           

三、Set注入

标签:

property

属性:

name

业务类中属性名

ref

引用类型唯一标识

value

基本类型赋值

注:属性须提供Set方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="userService" class="com.dzy.Service.userService">
    <bean id="userDao" class="com.dzy.Dao.userDao">
    	<property name="userService" ref="userService"></property>
        <property name="userName" value="admin"></property>
    </bean>
        
    <bean id="对象的唯一标识" class="业务对象全路径">
        <property name="userService" ref="引用类型唯一标识"></property>
        <property name="userName" value="值"></property>
     </bean>
    
</beans>
           

四、构造器注入

标签:

constructor-arg

一个construtor-arg标签对应构造器的参数

属性:

name

业务类中属性名

ref

引用类型唯一标识

value

基本类型赋值

注:循环引用时通过set注入解决

<bean id="goodsDao" class="com.dzy.dao.GoodsDao">
    <constructor-arg name="userDao" ref="userDao"></constructor-arg>
    <constructor-arg name="name" value="admin"></constructor-arg>
</bean>
           

GoodDao代码

package com.dzy.dao;

public class GoodsDao {
    private UserDao userDao;
    private String name;

    public GoodsDao() {
    }

    public GoodsDao(UserDao userDao, String name) {
        this.userDao = userDao;
        this.name = name;
    }
	
    public void test(){
        System.out.println("Godds...test"+","+name);
        userDao.test();
    }
}

           

自动对象的依赖注入

环境引入配置xml文件

xmlns:context=“http://www.springframework.org/schema/context”

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
	
   <!-- 配置扫描包范围-->
    <context:component-scan base-package="com.dzy"/>
    
</beans>
           

通过注解实现

自动装配注解:

@Resource

声明级别: 类级别 属性级别 方法级别 常用 属性(推荐) | 方法(Set 方法)

装配规则:

默认按照属性名装配

如果属性名对应的Bean 对象未找到 使用类型(Class 类型)实现装配

如果显式的声明Resource 的name 的value 值 内部装配按照value 执行装配 如果存在该value 对应的对象装配成功 如果不存在装配失败

如果接口存在多个实现时 需要显式的声明name 的value ,根据value 实现装配

@Autowired

声明级别: 常用 属性级别(推荐) 构造器 set

装配规则: 默认按照class 类型执行装配 如果按照名称实现装配 可以配合Qualifier 注解 声明value 值

自动装配bean对象:

声明级别:类级别

​ 持久层(dao层):

@Repository

​ 业务层(Service):

@Service

​ 视图层(Controller):

@Controller

​ 组件:

@Component

Bean标签属性

lazy-init

: 懒加载

默认值:false 容器启动时 Bean对象就会实例化 (存在单例缓存池)

​ true 容器启动时 不会实例化,使用时才会对对象实例化

作用(false):

  1. 提前发现应用程序潜在的问题
  2. 提高应用执行的性能

scope

:作用域

默认值:singleton 缓存到单例缓存池

​ prototype 不会缓存到缓存池,每次调用都创建一个新的bean并返回

销毁bean对象

xml代码

<bean id="userServiceImpl"class="com.shsxt.service.impl.UserServiceImpl" init-
method="init" destroy-method="destroy"> 
</bean> 
           

java代码

AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml"); 
ctx.close();