Spring IoC 自动装载 autowire:
自动装载是Spring提供的一种更加简单的方式,来完成DI,不需要手动配置property ,IoC容器会自动选择Bean玩成注入。
自动装载俩种:
-
byName ,通过属性名完成自动装载
-
byType,通过属性对应的数据类型完成自动装载
byName
1.创建实体类
package com.southwind.entity;import lombok.Data; @Data public class Presson { private Integer Id; private String name; private Car car; }
2.在spring.xml中配置Car和Person的Bean,通过自动装载完成依赖注入
<bean id="person" class="com.southwind.entity.Presson" autowire="byName">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="car" class="com.southwind.entity.Car">
<constructor-arg name="num" value="1"> </constructor-arg>
<constructor-arg name="brand" value="奥迪"></constructor-arg>
</bean>
测试:
package com.southwind.test;
import com.southwind.entity.Presson;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test7 {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-autowire.xml");
Presson preson =(Presson)applicationContext.getBean("person");
System.out.println(preson);
}
}
byType:
1.实体类
2.在spring.xml中配置Car和Person的Bean,通过自动装载完成依赖注入
<bean id="person" class="com.southwind.entity.Presson" autowire="byType">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="car" class="com.southwind.entity.Car">
<constructor-arg name="num" value="1"> </constructor-arg>
<constructor-arg name="brand" value="奥迪"></constructor-arg>
</bean>
注意:
使用byType进行自动装载时,多个会有异常。
Spring IoC基于注解的开发
SpingIoC的作用帮助开发者创建项目中所需要的Bean,同时完成Bean之间的依赖注入关系。DI
实现功能有两种方式:
- 基于xml
- 基于注解
基于注解的配置有两部:-
配置自动扫包
<context:component-scan base-package="com.southwind.entity"></context:component-scan> -
添加注解: @Component (默认首字母类第一个字母小写为名字)
package com.southwind.entity;import lombok.Data; import org.springframework.stereotype.Component; @Data @Component public class Repository { private DataSouse dataSouse; }改名字:@Component(value = "myrepo")
package com.southwind.entity;import lombok.Data; import org.springframework.stereotype.Component; @Data @Component(value = "myrepo") public class Repository { private DataSouse dataSouse; }分析注入得到类:
String[] s= applicationContext.getBeanDefinitionNames();
for(String name 😒){
System.out.println(name);
}
-
DI:
package com.southwind.entity;
import lombok.Data;
import org.springframework.stereotype.Component;
@Data
@Component
public class DataSouse {
private String user;
private String password;
private String url;
private String driverName;
}
自动装载:@Autowired(默认时byType)
package com.southwind.entity;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Data
@Component
public class Repository {
@Autowired
private DataSouse dataSouse;
}
要改变为byName()配合 @Qualifier(value="DS")
@Data
@Component
public class Repository {
@Autowired
@Qualifier(value="DS")
private DataSouse dataSouse;
}
实体类中的普通的成员变量可以通过@Value进行赋值
@Data
@Component(value = "DS")
public class DataSouse {
@Value("root")
private String user;
@Value("root")
private String password;
@Value("jdbc:mysql://localhost:3306/library")
private String url;
@Value("com.mysql.cj.jdbc.Driver")
private String driverName;
}
浙公网安备 33010602011771号