1,SpringIoc自动装配bean
总结:
- byname的时候,需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
-
bytype的时候,需要所有的bean的类型唯一 ,并且这个bean需要和自动注入的属性的类型一致
<?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="cat" class="com.ding.pojo.Cat"></bean>
<bean id="dog1" class="com.ding.pojo.Dog"></bean>
<bean id="people" class="com.ding.pojo.People" autowire="byType">
<property name="name" value="小明"></property>
</bean>
</beans>
public class Dog {
public Dog(){
System.out.println("调用了dog的无参构造函数");
}
public void shout(){
System.out.println("喵!!!");
}
}
public class Cat {
public Cat(){
System.out.println("调用了cat的无参构造函数");
}
public void shout(){
System.out.println("旺!!!");
}
}
public class People {
private Cat cat;
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2,注解实现自动装配
jdk1.5支持的注解,spring2.5就支持注解了
要使用注解支持
1,导入约束 context约束
2,配置注解的支持 <context:annotation-config/>
<?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/>
</beans>
@Autowired:直接在类上,也可以在set属性上使用
使用Autowired我们可以不用编写set方法了,前提是你这个自动装配的属性在IOC(容器)中存在,且符合名字byname,
科普
@Nullable 字段标记了这个注解,说明这个字段可以为null
public @interface Autowired {
boolean required() default true;
}
测试代码
public class People {
//如果显式定义了@Autowired的required属性为false,说明这个对象可以为null,否则不能为空
@Autowired(required = false)
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
如果@Autowrited自动装配比较复杂,自动装配无法通过一个注解完成的时候,我们可以使用@qualifier(value="xxx")去配合@Autowrited使用,指定一个唯一的bean对象注入
@Autowired()
@Qualifier(value = "cat22")
private Cat cat;
@Autowired
@Qualifier(value = "dog22")
private Dog dog;
private String name;
<context:annotation-config/>
<bean id="cat11" class="com.ding.pojo.Cat"></bean>
<bean id="dog11" class="com.ding.pojo.Dog"></bean>
<bean id="cat22" class="com.ding.pojo.Cat"></bean>
<bean id="dog22" class="com.ding.pojo.Dog"></bean>
@Resource注解
@Resource(name = "cat22")
private Cat cat;
@Resource()
private Dog dog;
private String name;
小结:
@Resource和@Autowrited的区别
- 都是用来自动装配的,都可以放在属性字段上
- @Autowrited通过bytype实现,而且必须要求这个对象存在。【常用】
- @Resource默认通过byname实现,如果找不到名字,则通过bytype实现,如果两个都找不到的情况下,就会报错。【常用】
- 执行顺序不同:
3,使用注解开发
1.在spring4之后使用注解开发,必须要保证aop的包导入了(通过导入srpingwebmvc包就自动导入了aop)
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
2.在使用注解,导入注解的约束
<?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/>
</beans>
1.bean
2.属性如何注入
//等价于<bean id="user" class="com.ding.pojo.User"/>
//@Component 组件
@Component
public class User {
//相当于<property name="name" value="丁天亮"></property>
@Value("丁天亮")
public String name;
}
3.衍生的注解
@Component有几个的衍生注解,我们在web开发中,会按mvc三层开发架构分层
- dao 【@Repository】
- service 【@Service】
- controller【@Controller】
这四个注解功能都是一样的,都是代表将某个类注册到spring中装配bean
4.自动装配置
- @Autowrited自动装配通过类型,名字,如果不能唯一自动装配上属性,则需要通过一个@qualifier(value="xxx")
- @Resource:自动装配通过名字,类型
- @Nullable 字段标记了这个注解,说明这个字段可以为null
- @Component:组件,放在类上,说明这个类被spring容器管理了
5.作用域
6.小结
xml与注解:
- xml更加万能,适合于任何场合!维护更加方便
- 注解,不是自己的类合使用不了,维护相对复杂
xml与注解最佳实践:
- xml用来管理bean
- 注解只负责完成属性的注入
- 我们在使用过程中,只需要注意一个问题:必须让注解生效就需要开启注解的支持
<!--开启注解配置-->
<context:annotation-config/>
<!--指定要扫描的包,这个包下面的注解就会生效-->
<context:component-scan base-package="com.ding"/>
4.使用java的方式配置spring
我们现在完全不使用Spring的xml配置了, 全权交由java来做。
JavaConfig是Spring的一个子项目,在Spring4之后,它成为了一个核心功能。
public class User {
public String getName() {
return name;
}
@Value("dingtianliang")
public void setName(String name) {
this.name = name;
}
private String name;
}
@Configuration //这个也会被spring容器托管,注册到容器中,因为他本来就是一个Component ,@Configuration代表这是一个配置类,就和我们之前看的beans.xml
@ComponentScan("com.ding.pojo")
public class DingConfig {
//注册一个bean,就相当于我们之前写的bean标签
//这个方法的名字,相当于bean中的id标签
//这个方法的返回值 就相当于bean中的class属性
@Bean
public User getUser() {
return new User();//就是返回要注入到bean的对象
}
}
测试类:
public class MyTest01 {
public static void main(String[] args) {
//如果完全使用了配置类去做,我们就只能使用AnnotationConfig上下文来获取容器,通过配置类的class对象加载
ApplicationContext context=new AnnotationConfigApplicationContext(DingConfig.class);
User user=(User) context.getBean("getUser");
System.out.println(user.getName());
}
}
这种纯JAVA的配置方式,在Springboot中随处可见