1、bean的作用域
-
单例模式 (Spring默认机制)
<bean id="user2" class="com.zj.pojo.User" c:age="18" c:name="haha" scope="singleton"/>
-
原型模式:每次从容器中get的时候,都会产生一个新对象!
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
-
其余的 request、session、application、这些个只能在web开发中使用到!
2、Bean的自动装配
- 自动装配是Spring满足bean依赖一种方式!
- Spring会在上下文中自动寻找,并自动给bean装配属性!
在Spring中有三种装配的方式
- 在xml中显示的配置
- 在java中显示配置
- 隐式 的自动装配bean 【重要】
环境搭建:一个人有两个宠物!
public class People {
private String name;
@Autowired
@Qualifier(value = "cat1")
private Cat cat;
@Resource(name="dog2")
private Dog dog;
public String getName() {
return name;
}
public void setName(String name) {
this.name = 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;
}
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", cat=" + cat +
", dog=" + dog +
'}';
}
}
2.1、ByName自动装配
<!--
byName: 会自动在容器上下文中查找,和自己对象set方法后面的值对应的 beanid!
-->
<bean id="people" class="com.zj.entitry.People" autowire="byName">
<property name="name" value="zhaozhao"/>
</bean>
2.2、ByType自动装配
<bean class="com.zj.pojo.Cat"/>
<bean class="com.zj.pojo.Dog"/>
<!--
byName: 会自动在容器上下文中查找,和自己对象set方法后面的值对应的 beanid!
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean!
-->
<bean id="people" class="com.kuang.zj.People" autowire="byType">
<property name="name" value="zhaozhao"/>
</bean>
小结:
- byname的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致!
- bytype的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!
3、使用注解实现自动装配
jdk1.5支持的注解,Spring2.5就支持注解了!
要使用注解须知:
- 导入约束 : context约束
- ==配置注解的支持 :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(Spring)容器中存在,且符合名字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;
}
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候、我们可以使用@Qualifier(value=“xxx”)去配置@Autowired的使用,指定一个唯一的bean对象注入!
public class People {
@Autowired
@Qualifier(value="cat111")
private Cat cat;
@Autowired
@Qualifier(value="dog222")
private Dog dog;
private String name;
}
- @Resource注解
public class People {
@Resource(name = "cat2")
private Cat cat;
@Resource
private Dog dog;
}
小结:
@Resource 和@ Autowired 的区别:
- 都是用来自动装配的,都可以放在属性字段上
- @ Autowired 通过byType的方式实现,而且必须要求这个对象存在! 【常用】
- @ Resource 默认通过byname的方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下,就报错! 【常用】
- 执行顺序不同:@ Autowired 通过byType的方式实现。@ Resource 默认通过byname的方式实现。
4、xml使用注解开发
在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
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>
-
bean
-
属性如何注入
// 相当于xml文件中的: <property name="name" value="haha2"/> @Component public class User { public String name; @Value("haha2") public void setName(String name) { this.name = name; } }
-
衍生的注解
@Component 有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!
-
dao 【@Repository】
-
service 【@Service】
-
controller 【@Controller】
这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean
-
-
自动装配置
- @Autowired :自动装配通过类型。名字 如果Autowired不能唯一自动装配上属性,则需要通过 @Qualifier(value="xxx") - @Nullable 字段标记了这个注解,说明这个字段可以为null; - @Resource :自动装配通过名字。类型。java中的
-
作用域
@Component @Scope("prototype") public class User { public String name; //相当于 <property name="name" value="haha2"/> @Value("haha2") public void setName(String name) { this.name = name; } }
-
小结
xml 与 注解:-
xml 更加万能,适用于任何场合!维护简单方便
-
注解 不是自己类使用不了,维护相对复杂!
xml 与 注解最佳实践: -
xml 用来管理bean;
-
注解只负责完成属性的注入;
-
我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持
<!--指定要扫描的包,这个包下的注解就会生效--> <context:component-scan base-package="com.zj"/> <context:annotation-config/>
-
5、使用Java的方式配置Spring
我们现在要完全不使用Spring的xml配置了,全权交给Java来做!
JavaConfig 是Spring的一个子项目,在Spring 4 之后,它成为了一个核心功能!
实体类
package com.zj.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("user")
public class User {
@Value("haha")
public String name;
}
}
}
配置文件
package com.zj.config;
import com.kuang.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
// 这个也会Spring容器托管,注册到容器中,因为他本来就是一个@Component
// @Configuration代表这是一个配置类,就和我们之前看的beans.xml
@Configuration
@ComponentScan("com.zj.pojo")
@Import(StudentConfig.class)
public class ConfigText {
//注册一个bean , 就相当于我们之前写的一个bean标签
//这个方法的名字,就相当于bean标签中的id属性
//这个方法的返回值,就相当于bean标签中的class属性
@Bean
public User user(){
return new User(); //就是返回要注入到bean的对象!
}
}
测试类!
public class MyTest {
public static void main(String[] args) {
//如果完全使用了配置类方式去做,我们就只能通过 AnnotationConfig 上下文来获取容器,通过配置类的class对象加载!
ApplicationContext context = new AnnotationConfigApplicationContext(ConfigText.class);
User getUser = (User) context.getBean("user");
System.out.println(getUser.getName());
}
}
这种纯Java的配置方式,在SpringBoot中随处可见!