-
Bean屬性繼承
應用場景:
場景一:ParentClass(屬性1,屬性2,屬性3,get和set方法)
子類1(屬性4,屬性5)?
子類2(屬性6,屬性7)
場景2:子類1和子類2沒有父類,而是有一些相同的屬性。
類1:(屬性1,屬性2,屬性3,屬性4,屬性5)
類2:(屬性1,屬性2,屬性3,屬性6,屬性7)
這兩個場景下,通過Spring注入類1和類2的所有屬性,一般如下:
?
abstract="true":設置<bean>標簽只是定義性的,不會對它進行實例化操作。
parent屬性:在Bean的子類bean標簽添加,值為父類的Id。
Spring配置代碼:
? <!-- 如果Class1和Class2屬性1、2、3相同 -->
步驟1:?<bean id="bean" class="main.java.com.Bean繼承屬性.ParentClass" abstract="true">
? ? ? ? ? ? ?<property name="attribute1" value="attribute1"></property>
? ? ? ? ? ? ?<property name="attribute2" value="attribute2"></property>?
? ? ? ? ? ? ?<property name="attribute3" value="attribute3"></property>
? ? ? ? ? ? ?</bean>
?步驟2: ? ?<bean id="bean1" class="main.java.com.Bean繼承屬性.Class1" parent="bean">
? ? ? ? ? ? ? ? ?<property name="attribute4" value="attribute4"></property>
? ? ? ? ? ? ? ? ?<property name="attribute5" value="attribute5"></property>
? ? ? ? ? ? ? ? ?</bean> ??
? ? ? ? ? ? ? ? ?
<bean id="bean2" class="main.java.com.Bean繼承屬性.Class2" parent="bean">
? ? ? ? <property name="attribute6" value="attribute6"></property>
? ? ? ? <property name="attribute7" value="attribute7"></property>
? ? ? ? </bean>
? ? ? ?
測試:
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-extends.xml");
Class1 c1=ac.getBean("bean1", Class1.class);
Class2 c2=ac.getBean("bean2",Class2.class);
System.out.println(c1);
System.out.println(c2);
}
結果:
Class1 [attribute1=attribute1, attribute2=attribute2, attribute3=attribute3, attribute4=attribute4, attribute5=attribute5]
Class1 [attribute1=attribute1, attribute2=attribute2, attribute3=attribute3, attribute6=attribute6, attribute7=attribute7]
應用場景2:類1和類2不繼承某一父類,只是存在相同屬性。
spring配置:
?<!-- 如果Class1和Class2屬性1、2、3相同 -->
? ? ?步驟1: ? <bean id="bean" abstract="true">
? ? ? ? ?<property name="attribute1" value="attribute1"></property>
? ? ? ? <property name="attribute2" value="attribute2"></property>?
? ? ? ? <property name="attribute3" value="attribute3"></property>
? ? ? ? </bean>
? ? ? 步驟2: ?<bean id="bean1" class="main.java.com.Bean繼承屬性.Class1" parent="bean">
? ? ? ? <property name="attribute4" value="attribute4"></property>
? ? ? ? <property name="attribute5" value="attribute5"></property>
? ? ? ? </bean> ??
? ? ? ? <bean id="bean2" class="main.java.com.Bean繼承屬性.Class2" parent="bean">
? ? ? ? <property name="attribute6" value="attribute6"></property>
? ? ? ? <property name="attribute7" value="attribute7"></property>
? ? ? ? </bean> ? ? ? ?
查看全部 -
ioc,控制反轉查看全部
-
Bean初始化及銷毀邏輯處理(在singleton作用域上進行講解)
Bean初始化——如果需要在Bean實例化時執行一些邏輯,Spring提供了兩種方法:(有一個javaBean,該Bean作用,連接數據庫,例如session,斷開數據庫連接,該Bean被創建之后,執行數據庫連接的過程)
方法1:bean標簽里的init-method屬性,該值為Bean的某一方法,Spring實例化時,會調用Bean的該方法。
方法2:讓Bean實現InitializingBean接口,Spring在實例化該Bean時,檢測到Bean實現了該接口,就會調用該接口所定義的相應方法。
Bean銷毀——如果需要在Bean銷毀之前執行一些邏輯,有兩種方法。
方法1:bean標簽里的destory-method屬性,該值為Bean的某一方法,Spring銷毀該Bean時,會調用Bean的該方法。
方法2:讓Bean實現DisposableBean接口,Spring在銷毀該Bean時,檢測到Bean實現了該接口,就會調用該接口所定義的相應方法。
案例1:通過bean標簽的init-method和destory-method屬性來指定Bean初始化邏輯和銷毀邏輯。
代碼:
public class Bean {
public void onInit(){
System.out.println("Bean的初始化邏輯方法被執行了");
}
public void onDestory(){
System.out.println("Bean的銷毀邏輯方法被執行了");
}
}
測試代碼:
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-initAnddestory.xml");
Bean bean=ac.getBean("bean", Bean.class);
System.out.println(bean);
}
結果:bean的銷毀方法沒執行,因為當前是單例模式,所以Bean的初始化是在Spring上下文實例化完成的,Bean的銷毀是在Spring上下文的銷毀過程中執行Bean的銷毀。(Spring上下文的銷毀定義到AbstractApplicationContext中)
Bean的初始化邏輯方法被執行了
main.java.com.Bean初始化及銷毀.Bean@b8a1063
spring配置:
<bean class="main.java.com.Bean初始化及銷毀.Bean" id="bean" init-method="onInit" destroy-method="onDestory">
改進:
@Test
public void test(){
AbstractApplicationContext ac=new ClassPathXmlApplicationContext("spring-initAnddestory.xml");
Bean bean=ac.getBean("bean", Bean.class);
System.out.println(bean);
ac.close();
}
注意:如果所有的Bean都有相同名稱的初始化方法和相同名稱的銷毀方法,可以在<beans default-init-method="onInit" ? default-destory-method="onDestory">,含義:所有的bean都有初始化方法和銷毀方法。
案例2:實現相應接口,并實現相應方法
代碼:
public class Bean implements InitializingBean,DisposableBean{
@Override
public void destroy() throws Exception {
System.out.println("Bean的銷毀邏輯方法被執行了");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Bean的初始化邏輯方法被執行了");
}
}
spring配置:
<bean class="main.java.com.Bean初始化及銷毀.Bean" id="bean"></bean>
測試:
@Test
public void test(){
AbstractApplicationContext ac=new ClassPathXmlApplicationContext("spring-initAnddestory.xml");
Bean bean=ac.getBean("bean", Bean.class);
System.out.println(bean);
ac.close();
}
查看全部 -
Bean的懶加載
測試:
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("spring_lanjiazai.xml");
System.out.println("context被創建了");
Bean bean=ac.getBean("bean", Bean.class);
System.out.println(bean);
結果:
Bean被創建了
context被創建了
main.java.com.Bean懶加載.Bean@b8a1063
證明:當Spring上下文初始化時,作用域為singleton的Bean就已經被創建好了,而不是通過Spring上下文獲取時才去創建。
Bean懶加載概念:
Spring容器會在創建容器時提前初始化Singleton作用域的bean,但是如果Bean被標注了lazy-init=“true”,則該Bean只有在其被需要的時候才會被初始化。(只對singleton作用域的bean有效)
代碼:
<bean id="bean" class="main.java.com.Bean懶加載.Bean" scope="singleton" lazy-init="true"></bean>
解決多個bean使用懶加載問題:
<beans標簽里添加default-lazy-init=“true”>:表示spring配置文件里所有bean都是懶加載模式。
使用場景:
如果某個Bean在程序整個運行周期都可能不會被使用,那么可考慮設定該Bean為懶加載。
優點:盡可能的節省了資源。
缺點:可能會導致某個操作相應操作時間增加。
查看全部 -
scope 的web 作用域
查看全部 -
代碼回顧
javabean 的作用域是request 每次請求都會創建一個新的實例
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?session? 在一次回話中都是同一個實例,重開瀏覽器會有不同的實例
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?appliction 是同一個服務內都是同一個實例,重啟服務會產生不同的實例
查看全部 -
三個類對應的配置文件
查看全部 -
application session? request? 三個測試類
查看全部 -
spring web 上下文環境配置
查看全部 -
springweb 上下文環境
查看全部 -
課程內容:注入bean的課程內容
查看全部 -
spring通過xml實現自定義作用域(自定義雙例模式作用域)
SimpleThreadScope:Spring內置作用域。
編寫自定義作用域:
步驟1:實現Scope接口(org.springframework.beans.factory.config),主要關注實現的get方法和remove方法。
get方法:按照name參數,按照我們自己定義的規則,去返回一個Bean,如果我們定義的規則里,Bean不存在,那么將通過objectFactory去創建一個Bean。
雙例模式:一個bean的Id對應兩個實例,實現雙例模式,需要兩個Map集合,Map<String,Object> map1=new HashMap<String,Object>();Map<String,Object> map2=new HashMap<String,Object>();。
get方法:
public Object get(String name, ObjectFactory<?> objectFactory) {
if(!map1.containsKey(name)){ ? //判斷map1是否包含名為name的Bean實例
Object o=objectFactory.getObject();//如果不存在則創建一個ObjectFactory規則Bean
map1.put(name, o); ? //并把這個Bean放入集合,并命名為name
return o;
}
if(!map2.containsKey(name)){ ? //map2同map1操作
Object o=objectFactory.getObject();?
map2.put(name, o); ?
return o;
}
//如果map1和map2都包含這個Bean,也就是說Spring上下文通過Id獲取有兩個實例,則返回一個0或1
int i=new Random().nextInt(2);
if(i==0){
return map1.get(name);//如果是0,則返回對象1
}else{
return map2.get(name);//如果是0,則返回對象2
}
}
remove方法:和get方法相反,按照name參數,去移除一個Bean。
public Object remove(String name) {
if(map1.containsKey(name)){
Object o=map1.get(name);
map1.remove(name);
return o;
}
if(map2.containsKey(name)){
Object o=map2.get(name);
map2.remove(name);
return o;
}
return null; ? ?//說明沒拿到任何實例
}
xml配置:
<bean id="myScope" class="main.java.com.自定義作用域.MyScope" ?>
</bean>
<bean ?class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="myscope" value-ref="myScope"></entry>
</map>
</property>
</bean>
測試代碼:
@Test
public void testBean(){
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-zidingyi.xml");
for(int i=0;i<10;i++){
Bean bean=ac.getBean(“myScope”", Bean.class);
System.out.println(bean);
}
}
Spring提供的另外一個作用域:SimpleThreadScope(同一線程,Spring上下文會分配同一實例,不同線程,則獲得不同實例。
srping配置:
<bean id="myScope" class="main.java.com.自定義作用域.MyScope" ></bean>
<bean id="simpleThreadScope" class="org.springframework.context.support.SimpleThreadScope"></bean>
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="myscope" value-ref="myScope"></entry>
<entry key="simpleThreadScope" value-ref="simpleThreadScope"></entry>
</map>
</property>
</bean>
<bean id="bean11" class="main.java.com.自定義作用域.Bean" scope="simpleThreadScope"></bean>
測試代碼:
public void testBean(){
final ApplicationContext ac=new ClassPathXmlApplicationContext("spring-zidingyi.xml");
for(int i=0;i<10;i++){
new Thread(new Runnable(){
@Override
public void run() {
Bean bean=ac.getBean("bean11", Bean.class);
System.out.println(bean);
}
}).start();
}
}
}
查看全部 -
代碼內容回顧
查看全部 -
課程內容回顧
查看全部 -
Web環境相關作用域
1、request作用域
bean標簽的scope作用域設置為request后,每次請求,spring都會重新創建一個新的Bean。
2、session作用域
bean標簽的scope作用域設置為session后,每個session都會被創建一個單獨的實例。
3、application作用域
bean標簽的scope作用域設置為application后,每個servletContext都會被創建一個單獨的實例。
4、(不帶傳入)websocket作用域,使用很少,暫時忽略
bean標簽的scope作用域設置為websocket后,每次請求,spring都會重新創建一個新的Bean。
因為是web應用作用域,所以程序應處于SpringWeb上下文環境,SpringMVC中就使用到了SpringWeb上下文環境。
建立SpringWeb上下文環境方式:
方式一:使用DispathcherServlet,不需要增加其他任何配置,因為這就是SpringWeb標準的上下文環境。
代碼:
?<servlet-name>SpringWebContext</servlet-name>
? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
? <init-param>
? <param-name>contextConfigLocation</param-name>
? <param-value>classpath:spring.xml</param-value>
? </init-param>
? </servlet>
? <servlet-mapping>
? <servlet-name>SpringWebContext</servlet-name>
? <url-pattern>/*</url-pattern>
? </servlet-mapping>
方式二:不使用DispatcherServlet,那么需要增加listener或filter,取決于Servlet版本是2.4以上還是2.4以下。
(1)Servlet2.4以上
listener用處:在Web應用啟動初始化過程中,加載一個SpringWeb的上下文。
代碼:
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
(2)Servlet2.4以下
filter作用:為每一個請求覆蓋一個SpringWeb上下文。
代碼:
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
<filter-mapping>
代碼演示:這里用到了另外兩個包——spring-web和spring-webmvc
步驟1:配置web.xml
@RequestMapping:在瀏覽器上通過URL路徑去訪問這個方法。
@ResponseBody:該注解下的方法返回值在瀏覽器上進行顯示。
@Controller:放置在類上,spring可以通過它來找到該類。
代碼:
@Controller
public class RequestScope {
@RequestMapping("testRequest")
@ResponseBody
public String test(){
return this.toString();
}
}
訪問地址:http://localhost:8080/SpringWeb/testRequest,每次刷新都是會變化的。
查看全部
舉報