文章目录
- 0.方式选择的原则
- 1使用XML自动装配
- 1.1**手动装配**
- 1.2**ByType式自动装配=> autowire="byType"**
- 1.3**ByName式自动装配=> autowire="byName"**
- 2.使用半注解半xml装配
- 方式一:
- 方式二:
- @Resource 和@ Autowired 的区别:
- 3.接近注解开发
- 4.完全java注解开发
在Spring中有三种装配的方式
- 在 XML 文件中显式配置
- 在 Java 的接口和类中实现配置
- 隐式 Bean 的发现机制和自动装配原则
注意:自动装配原则在注解和xml中都会使用
0.方式选择的原则
在现实的工作中,这 3 种方式都会被用到,并且在学习和工作之中常常混合使用,所以这里给出一些关于这 3 种优先级的建议:
1.最优先:通过隐式 Bean 的发现机制和自动装配的原则。
基于约定由于配置的原则,这种方式应该是最优先的
- **好处:**减少程序开发者的决定权,简单又不失灵活。
2.其次:Java 接口和类中配置实现配置
在没有办法使用自动装配原则的情况下应该优先考虑此类方法
- **好处:**避免 XML 配置的泛滥,也更为容易。
- **典型场景:**一个父类有多个子类,比如学生类有两个子类,一个男学生类和女学生类,通过 IoC 容器初始化一个学生类,容器将无法知道使用哪个子类去初始化,这个时候可以使用 Java 的注解配置去指定。
3.最后:XML 方式配置
在上述方法都无法使用的情况下,那么也只能选择 XML 配置的方式。
- **好处:**简单易懂(当然,特别是对于初学者)
- **典型场景:**当使用第三方类的时候,有些类并不是我们开发的,我们无法修改里面的代码,这个时候就通过 XML 的方式配置使用了。
4.xml 与 注解:
- xml 更加万能,适用于任何场合!维护简单方便
- 注解 不是自己类使用不了,维护相对复杂!
xml 与 注解最佳实践:
- xml 用来管理bean;
- 注解只负责完成属性的注入;
- 我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持
参考:地址
1使用XML自动装配
例子理解:
1.1手动装配
- 创建三个实体类
Student
public class Student implements Serializable {
private String name;
private Cat cat;
private Dog dog;
}
Dog
public class Dog {
public void eat(){
System.out.println("吃骨头");
}
}
Cat
public class Cat {
public void eat(){
System.out.println("猫吃鱼");
}
}
- 配置文件:
<?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">
<bean id="cat" class="com.g.pojo.Cat"/>
<bean id="dog" class="com.g.pojo.Dog"/>
<bean id="student" class="com.g.pojo.Student">
<property name="name" value="张三"/>
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
</bean>
</beans>
- 测试:
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = context.getBean("student", Student.class);
student.getCat().eat();
student.getDog().eat();
}
}
1.2ByType式自动装配=> autowire="byType"
配置文件:
1.bean中不ref
<bean id="cat" class="com.g.pojo.Cat"/>
<bean id="dog" class="com.g.pojo.Dog"/>
<bean id="student" class="com.g.pojo.Student" autowire="byType">
<property name="name" value="张三"/>
</bean>
2.写错id
<bean id="catsss" class="com.g.pojo.Cat"/>
<bean id="dogsss" class="com.g.pojo.Dog"/>
<bean id="student" class="com.g.pojo.Student" autowire="byType">
<property name="name" value="张三"/>
</bean>
3.不写id
<bean class="com.g.pojo.Cat"/>
<bean class="com.g.pojo.Dog"/>
<bean id="student" class="com.g.pojo.Student" autowire="byType">
<property name="name" value="张三"/>
</bean>
注意:当在bean的参数autowire设置为按类型查找时,可以不用写ref引用。
但是前提是只存在一种引用的类型,保证只有唯一的Class类
1.3ByName式自动装配=> autowire="byName"
配置文件:
<bean id="cat" class="com.g.pojo.Cat"/>
<bean id="dog" class="com.g.pojo.Dog"/>
<bean id="student" class="com.g.pojo.Student" autowire="byName">
<property name="name" value="张三"/>
</bean>
注意:当student对象ref 其他类型的对象(bean id),这时也可以省略。
但是id必须保证在student中设置的set后面的name
- 小结:
- byname的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!
- bytype的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!
2.使用半注解半xml装配
jdk1.5支持的注解,Spring2.5支持注解
使用的注解:
@Autowired
直接在属性上使用即可!也可以在set方式上使用,使用Autowired 我们可以不用编写Set方法了,前提是你这个自动装配的属性在 IOC(Spring)容器中存在,且符唯一的name。
如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
@Nullable
字段标记了这个注解,说明这个字段可以为null;
@Qualifier
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解==@Autowired==完成的时候、我们可以使用@Qualifier**(value=“xxx”)**去配置@Autowired的使用,指定一个唯一的bean对象注入!
@Resource
放在属性上使用,@Resource(name = “别名”),
步骤
方式一:
1.使用注解结合xml需要更改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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean class="com.g.pojo.Cat"/>
<bean class="com.g.pojo.Dog"/>
<bean id="student" class="com.g.pojo.Student"/>
</beans>
@Autowired装配的思想就是byType,要求class唯一,因此删掉id也可以使用
2.实体类student的操作:
public class Student implements Serializable {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
public Student() {
}
}
@Autowired也可以放在set的上面
3.测试:
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = context.getBean("student", Student.class);
student.getDog().eat();
student.getCat().eat();
}
}
方式二:
- 修改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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="cat" class="com.g.pojo.Cat"/>
<bean id="dog" class="com.g.pojo.Dog"/>
<bean id="student" class="com.g.pojo.Student"/>
</beans>
- 修改实体类:
public class Student implements Serializable {
@Resource
private Cat cat;
@Resource(name = "dog")
private Dog dog;
public Student() {
}
注意:@Resource(name = “dog”)中的name对应的是xml中的bean id
- 测试
@Resource 和@ Autowired 的区别:
- 都是用来自动装配的,都可以放在属性字段上
- @ Autowired 通过byType的方式实现,而且必须要求这个对象存在! 【常用】
- @ Resource 默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错! 【常用】
- 执行顺序不同:@ Autowired 通过byType的方式实现。@ Resource 默认通过byname的方式实现。
3.接近注解开发
举例:
- 导入头文件
<?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
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<!--扫描该包下的所有的注解都会生效-->
<context:component-scan base-package="com.g"/>
</beans>
- 实体类:
@Component
public class Student implements Serializable {
@Value("20")
private int age;
@Value("李四")
private String name;
}
==@Component==表示该类已经被Spring托管了
==@value==可以理解为原来的xml中的bean中属性注入属性
- 测试
4.完全java注解开发
全程不适用注解开发,完全使用java进行开发,并且这是在SpringBoot中常用的开发方式。
步骤:
- 创建实体类
@Component
public class Student implements Serializable {
@Value("20")
private int age;
@Value("李四")
private String name;
}
- 编写配置类
@Configuration
//@Configuration 表示该类是个配置类,其实本身就是一个组件
@ComponentScan(value ="com.g.pojo")
//@ComponentScan 扫描该包下的组件
public class Beansconfig {
@Bean
//注册一个bean,相当于我们之前的写的bean标签
//定义的方法名相当于bean中的id的值
//返回值的类型类相当于bean标签中的calss的值
public Student student(){
return new Student();
}
}
- 测试
public class MyTest {
@Test
public void test(){
//如果完全使用了配置类方式去做,我们就只能通过 AnnotationConfig 上下文来获取容器,通过配置类的class对象加载
ApplicationContext context = new AnnotationConfigApplicationContext(Beansconfig.class);
Student student = context.getBean("student", Student.class);
System.out.println(student);
}
}