1.装配概念
《spring实战》中给装配下了一个定义:创建应用对象之间协作关系的行为称为装配。也就是说当一个对象的属性是另一个对象时,实例化时,需要为这个对象属性进行实例化。这就是装配。
依赖注入的本质就是装配,装配是依赖注入的具体行为。这就是两者的关系
2.方式
在 Spring 中提供了 3 种方法进行配置:
在 XML 文件中显式配置
在 Java 的接口和类中实现配置
隐式 Bean 的发现机制和自动装配原则
2.1.在xml文件中显示配置
①构造器注入
User.java
public class User {
private String name;
public User(String name){
this.name=name;
}
public void show(){
System.out.println("name="+name);
}
}
applicationContext.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
id: bean的唯一标识符,也就是相当于我们学的对象名
class:bean 对象所对应的全限定名:包名+类名
name:也是别名 可以同时取多个别名
-->
<bean id="user" class="com.sxw.pojo.User" name="user2 u2">
<constructor-arg name="name" value="xw"></constructor-arg>
</bean>
</beans>
test.java
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User)context.getBean("u2");
user.show();
}
}
结果:
②set注入
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is .
测试pojo类 :
Address.java
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
Student.java
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private Properties info;
private String wife;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public void show(){
System.out.println("name="+ name
+ ",address="+ address.getAddress()
+ ",books="
);
for (String book:books){
System.out.print("<<"+book+">>\t");
}
System.out.println("\n爱好:"+hobbys);
System.out.println("card:"+card);
System.out.println("games:"+games);
System.out.println("wife:"+wife);
System.out.println("info:"+info);
}
}
1.常量注入
<bean id="student" class="com.sxw.pojo.Student">
<property name="name" value="xw" />
</bean>
2.Bean注入 注意点:这里的值是一个引用,ref
<bean id="address" class="com.sxw.pojo.Address">
<property name="address" value="西安" />
</bean>
<bean id="student" class="com.sxw.pojo.Student">
<!-- 第一种 普通值注入 value -->
<property name="name" value="xw" />
<!-- 第二种 bean注入 ref -->
<property name="address" ref="address" />
</bean>
3.数组注入
<property name="books">
<array>
<value>java</value>
<value>c语言</value>
<value>数据结构</value>
</array>
</property>
4.List注入
<property name="hobbys">
<list>
<value>打游戏</value>
<value>跑步</value>
<value>敲代码</value>
</list>
</property>
5.Map注入
<property name="card">
<map>
<entry key="身份证" value="123456789"></entry>
<entry key="银行卡" value="888888888"></entry>
</map>
</property>
6.Set注入
<property name="games">
<set>
<value>王者荣耀</value>
<value>王者荣耀</value>
<value>红色警戒</value>
</set>
</property>
7.Null注入
<property name="wife">
<null/>
</property>
8.Properties注入
<property name="info">
<props>
<prop key="学号">2019113</prop>
<prop key="性别">男</prop>
</props>
</property>
测试:
③命名空间装配
p命名和c命名注入
User.java
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
P命名空间注入 : 需要在头文件中加入约束文件
xmlns:p="http://www.springframework.org/schema/p"
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.sxw.pojo.User" p:name="xw" p:age="18"></bean>
c 命名空间注入 : 需要在头文件中加入约束文件
xmlns:c="http://www.springframework.org/schema/c"
<!--C(构造: Constructor)命名空间 , 属性依然要设置set方法-->
<bean id="user2" class="com.sxw.pojo.User" c:name="xw" c:age="18" scope="prototype"></bean>
2.2.通过注解装配Bean
在更多的时候我们考虑用注解去装配Bean
在spring4之后,想要使用注解形式,必须得要引入aop的包,在配置文件当中,还得要引入一个context约束
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
在applicationContext.xml还需要加入以下配置
<!-- 开启对注解的支持 -->
<context:annotation-config />
<!-- 指定要扫描的包 这个包下的注解就会生效 -->
<context:component-scan base-package="com.sxw" />
使用注解注入属性
1、可以不用提供set方法,直接在直接名上添加@value("值")
@Component("user") //等价于 <bean id="user" class="com.sxw.pojo.User"></bean>
public class User {
//相当于 <property name="name" value="xw" />
@Value("xw")
public String name;
}
2、如果提供了set方法,在set方法上添加@value("值");
@Component("user") //等价于 <bean id="user" class="com.sxw.pojo.User"></bean>
public class User {
public String name;
@Value("xw") //相当于 <property name="name" value="xw" />
public void setName(String name) {
this.name = name;
}
}
@Component三个衍生注解
为了更好的进行分层,Spring可以使用其它三个注解,功能一样,目前使用哪一个功能都一样。
@Controller:web层
@Service:service层
@Repository:dao层
写上这些注解,就相当于将这个类交给Spring管理装配了!
2.3.基于java类进行配置
1、新建一个User.java
//这个注解的意思 就是说明这个类被spring接管了 注册到了容器中
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("xw")
public void setName(String name) {
this.name = name;
}
}
2、编写XConfig配置类
@Configuration //这个也会被spring 容器托管 注册到容器中 因为它本来就是一个@Component,@Configuration代表这是一个配置类,就和我们之前看到的beans.xml一样
@ComponentScan("com.sxw.pojo")
public class XConfig {
//注册一个bean 就相当于我们之前写的一个bean标签
//这个方法的名字,就相当于bean标签中的id属性
//这个方法的返回值,就相当于bean标签中的class属性
@Bean
public User user(){
return new User(); //就是返回要注入到bean的对象
}
}
3、测试
public void test01(){
//如果完成使用了配置类方式去做,我们就只能通过AnnotationConfig 上下文获取容器 通过配置类的class对象加载
ApplicationContext context = new AnnotationConfigApplicationContext(XConfig.class);
User user = (User)context.getBean("user");
System.out.println(user.getName());
}
测试正常
如果需要导入其他配置类在这个类上加一个注解
@Import(MyConfig2.class) //导入合并其他配置类,类似于配置文件中的import标签
3.自动装配
自动装配是使用spring满足bean依赖的一种方法
spring会在应用上下文中为某个bean寻找其依赖的bean。
Spring的自动装配需要从两个角度来实现,或者说是两个操作:
组件扫描(component scanning):spring会自动发现应用上下文中所创建的bean;
自动装配(autowiring):spring自动满足bean之间的依赖,也就是我们说的IoC/DI;
3.1.在beans.xml配置文件中自动装配
搭建测试环境
1、新建一个项目
2、新建两个实体类,Cat Dog 都有一个叫的方法
public class Cat {
public void shout(){
System.out.println("miao-");
}
}
public class Dog {
public void shout(){
System.out.println("wang-");
}
}
3、新建一个用户类 User
public class User {
private Cat cat;
private Dog dog;
private String name;
public void setCat(Cat cat) {
this.cat = cat;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public void setName(String name) {
this.name = name;
}
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
public String getName() {
return name;
}
}
4、beans.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="cat1" class="com.sxw.pojo.Cat"></bean>
<bean id="dog" class="com.sxw.pojo.Dog"></bean>
<bean id="user" class="com.sxw.pojo.User" >
<property name="name" value="xw" />
<property name="dog" ref="dog" />
<property name="cat" ref="cat1" />
</bean>
</beans>
5、test
public class MyTest {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = context.getBean("user", User.class);
user.getCat().shout();
user.getDog().shout();
}
}
结果输出正常
autowire byName (按名称自动装配)
修改bean配置,增加一个属性 autowire="byName"
<bean id="user" class="com.sxw.pojo.User" autowire="byName">
<property name="name" value="xw" />
</bean>
测试依然成功
这里要注意:
当一个bean节点带有 autowire byName的属性时
将查找其类中所有的set方法名,例如setCat,获得将set去掉并且首字母小写的字符串,即cat。
去spring容器中寻找是否有此字符串名称id的对象。
如果有,就取出注入;如果没有,就报空指针异常。
autowire byType (按类型自动装配)
将user的bean配置修改一下 : autowire="byType"
<bean id="user" class="com.sxw.pojo.User" autowire="byType">
<property name="name" value="xw" />
</bean>
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid!byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean!
3.2.使用注解实现自动装配 @Autowired
1.User.java
public class User {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
public void setName(String name) {
this.name = name;
}
public Cat getCat() {
return cat;
}
public Dog getDog() {
return dog;
}
public String getName() {
return name;
}
}
2.beans.xml
<context:annotation-config />
<bean id="cat" class="com.sxw.pojo.Cat"></bean>
<bean id="dog" class="com.sxw.pojo.Dog"></bean>
<bean id="user" class="com.sxw.pojo.User" >
<property name="name" value="xw" />
</bean>
测试正常
@Autowired(required=false) 说明:false,对象可以为null;true,对象必须存对象,不能为null。
//如果允许对象为null,设置required = false,默认为true
@Autowired(required = false)
private Cat cat
@Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配
@Qualifier不能单独使用。
测试:
1、配置文件修改内容,保证类型存在对象。且名字不为类的默认名字!
<bean id="cat1" class="com.sxw.pojo.Cat"></bean>
<bean id="dog1" class="com.sxw.pojo.Dog"></bean>
<bean id="cat2" class="com.sxw.pojo.Cat"></bean>
<bean id="dog2" class="com.sxw.pojo.Dog"></bean>
2、没有加Qualifier测试,直接报错
3、在属性上添加Qualifier注解
@Autowired
@Qualifier("cat1")
private Cat cat;
@Autowired
@Qualifier("dog2")
private Dog dog;
测试正常
4.Bean的作用域
5.总结
XML与注解比较
XML可以适用任何场景 ,结构清晰,维护方便
注解不是自己提供的类使用不了,开发简单方便
xml与注解整合开发 :推荐最佳实践
xml管理Bean
注解完成属性注入
使用过程中, 可以不用扫描,扫描是为了类上的注解
<context:annotation-config/> 作用:
进行注解驱动注册,从而使注解生效
用于激活那些已经在spring容器里注册过的bean上面的注解,也就是显示的向Spring注册
如果不扫描包,就需要手动配置bean
如果不加注解驱动,则注入的值为null!