欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > Spring DI

Spring DI

2025/6/2 2:04:51 来源:https://blog.csdn.net/m0_74249133/article/details/141003060  浏览:    关键词:Spring DI
一、SpringDI的理解
1.DI

DI(Dependecy Inject,依赖注入)是对IOC概念的不同角度的描述,是指应用程序在运行时,每个Bean对象都依赖IOC容器注入当前bean对象所需要的另一个bean对象。

2.作用

将spring ioc容器所创建的各个组件,使用DI的语法进行关联,耦合(胶水)

3.DI使用步骤
  • 思考,什么方式,什么数据类型
  • 给属性提供set、构造方法
  • 编写配置文件
4.DI数据类型
  • 基本类型与String
  • JavaBean
  • 复杂类型,list set array map properties(构造注入不支持)
二、Bean对象

bean代表被IOC容器管理的对象,通过配置文件或者注解,告诉IOC容器哪些是需要管理的对象。

三、DI实现方式
1.set注入

1.1 set方法:使用seeter方法

public void setStuName(String stuName) {this.stuName = stuName;
}public void setStuAge(int stuAge) {this.stuAge = stuAge;
}public void setStuHobby(String stuHobby) {this.stuHobby = stuHobby;
}

1.2 set配置

<property name value/ref>


<bean id="xxx" class="xxx"><property name="xxx" ref="xxx"></property>
</bean><bean id="xxx" class="xxx"><property name="xxx" value="xxx"/>
</bean>
2.构造注入

1.构造方法 :写出有参构造、无参构造

public Student(String stuName, int stuAge, String stuHobby) {this.stuName = stuName;this.stuAge = stuAge;this.stuHobby = stuHobby;
}public Student() {
}

2.构造配置

<constructor-arg name type index/value/ref>

<bean id="student2" class="com.apesource.pojo.Student">
<constructor-arg name="stuName" value="张三"/>
<constructor-arg name="stuAge" value="18"/>
<constructor-arg name="stuHobby" value="睡觉"/>
</bean><bean id="student3" class="com.apesource.pojo.Student">
<constructor-arg type="java.lang.String" value="张三"/>
<constructor-arg type="int" value="18"/>
<constructor-arg type="java.lang.String" value="睡觉"/>
</bean><bean id="student4" class="com.apesource.pojo.Student">
<constructor-arg index="0" value="张三"/>
<constructor-arg index="1" value="18"/>
<constructor-arg index="2" value="睡觉"/>
</bean>
3.注解注入

spring的配置

3.1 spring2.5前==xml(采用xml配置文件)
<bean id="" class="" init-method="" destroy-method="" scope="" autowire=""><property></property><constructor-arg></constructor-arg>
</bean>
3.2 spring2.5后==xml+annotation(采用配置文件和注解)
  • 注入类

替换:<bean id="" class=""></bean>

位置:写在类上

语法:

@Component(value="注入容器中的id,如果省略id为类名且首字母小写,value属性名称可以省略")

写在配置文件中:

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

含义:扫描所有被@Component注解所修饰的类,注入容器

@Repository=====>注入数据访问层

@Service========>注入业务层

@Controller=====>注入控制层

以上三个注解与@Component功能语法一致

  • 注入基本数据

@Value

含义:注入基本数据

替换:<property></property>

位置:成员变量或对应的set方法

语法:@Value("数据内容")

@Value("${动态获取}")

<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

含义:将动态参数写在properties文件中,通过此方法注入到容器中

@Autowired

语法:@Autowired(required = "true-默认、false、是否必须进行装配")

修饰:成员变量或对应的构造方法

含义:按照通过set方法进行“类型装配”,set方法可以省略

注意:

1.默认是按照类型装配且同set方法

2.若容器中有一个类型可以与之匹配则装配成功,若没有一个类型可以匹配则报错

NoSuchBeanDefinitionException

3.若容器中有多个类型可以与之匹配,则自动切换为按照名称装配,若名称没有对应,则报错

NoUniqueBeanDefinitionException

  • 其他注解

@Primary

含义:首选项,当类型冲突的情况下,此注解修饰的类被列为首选(备胎扶正)

修饰:类

注意:不能单独使用,必须与@Component....联合使用

@Qualifier(value="名称")

含义:按照名称装配

修饰:成员变量

注意:不能单独使用,必须与@Autowired联合使用

@Service
public class UserServiceImp implements IUserService {//当有多个类与之匹配,可使用名称装配@Autowired//类型装配@Qualifier("userDaoImp2")//名称装配IUserDao dao;@Overridepublic void save() {System.out.println("service.method");dao.save();//此时输出的是dao2.mothod}
}@Repository
public class UserDaoImp implements IUserDao {@Overridepublic void save() {System.out.println("dao.mothod");}
}@Repository
public class UserDaoImp2 implements IUserDao {@Overridepublic void save() {System.out.println("dao2.mothod");}
}

@Resource(name="名称")

含义:按照名称装配

修饰:成员变量

注意:单独使用

@Scope

含义:配置类的作用域

修饰:类

注意:不能单独使用,必须与@Component....联合使用

@Scope("prototype")

@Scope("singleton")

@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)

@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)

@PostConstruct:初始化,修饰方法

替换:init-method

@PreDestroy:销毁,修饰方法

替换:destory-method

msg1=李四
msg2=25
@Component
@Scope("prototype")//单例
public class Student {//动态传参@Value("${msg1}")private String name;@Value("${msg2}")private int age;@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}@PostConstructpublic void donint(){System.out.println("初始化");}@PreDestroypublic void dostory(){System.out.println("销毁");}
}
3.3 spring3.0后==annotation+JavaConfig配置类(采用注解和配置类)

@Configuration

作用:指定当前类是一个配置类

细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。

@ComponentScan(value="")

作用:用于通过注解指定spring在创建容器时要扫描的包

属性:value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包。

替换:<context:component-scan base-package="com.apesource"></context:component-scan>

@PropertySource(value="")

作用:用于指定properties文件的位置

属性:value:指定文件的名称和路径。

配合:@Value使用

替换:<context:property-placeholder location="classpath:message.properties"></context:property-placeholder>

@Bean

作用:用于把当前方法的返回值作为bean对象存入spring的容器中

属性:name:用于指定bean的id。当不写时,默认值是当前方法的名称

@Import(xxx.class)

作用:用于导入其他的配置类

属性:value:用于指定其他配置类的字节码。

//配置类
@Configuration
//扫描的包
@ComponentScan(basePackages = "apesourse")
//指定properties文件的位置
@PropertySource(value = "classpath:message.properties")
//导入其他配置类
@Import(testconfig.class)public class applicationconfig {//方法1:传入方法//把IUserDao作为bean对象存入spring的容器中@Beanpublic IUserDao dao(){return new UserDaoImp();}//把IUserService作为bean对象存入spring的容器中@Beanpublic IUserService service(){return new UserServiceImp(dao());}//把IUserContraller作为bean对象存入spring的容器中@Beanpublic IUserContraller contraller(){return new UserContrallerImp(service());}//方法2:传入参数(@Autowired可省略不写)@Beanpublic IUserDao dao(){return new UserDaoImp();}@Beanpublic IUserService service(@Autowired IUserDao dao){return new UserServiceImp(dao);}@Beanpublic IUserContraller contraller(@Autowired IUserService service){return new UserContrallerImp(service);}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词