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中隨處可見!