spring学习(47):bean的作用域
生活随笔
收集整理的這篇文章主要介紹了
spring学习(47):bean的作用域
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄結(jié)構(gòu)
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.geyao</groupId><artifactId>spring01geyao</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.13.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>3.2.0.RELEASE</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.4.RELEASE</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.3.4.RELEASE</version><scope>test</scope></dependency></dependencies> </project>log4j.properties
### 設(shè)置### log4j.rootLogger = ERROR,stdout### 輸出信息到控制抬 ### log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n log4j.category.org.springframework.beans.factory=ERRORapplicationContext.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--bean元素:描述當(dāng)前的對象需要由spring容器管理id屬性:標(biāo)識對象 未來在應(yīng)用程序中可以根據(jù)id獲取對象class對象:被管理的對象的全名--><bean id="notepad" class="com.geyao.demo.NotePad" scope="singleton"/> </beans>notepad類
package com.geyao.demo;public class NotePad {public NotePad() {super();System.out.println("NotePad的構(gòu)造函數(shù)"+this.toString());} }Notepadtest類
package com.geyao.demo;import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; //無論我們是否去主動獲取對象,spring上下文剛加載就會創(chuàng)建對象 //無論獲取多少次,都是統(tǒng)一對象 // public class NotepadTest {@Testpublic void test01(){ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");NotePad notePad1= (NotePad)context.getBean("notepad");NotePad notePad2= (NotePad)context.getBean("notepad");System.out.println(notePad1=notePad2);} }notepadtestAuto類
package com.geyao.demo;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class NotePadTestAuto {@Autowiredprivate NotePad notePad1;@Autowiredprivate NotePad notePad2;@Testpublic void test01(){System.out.println(notePad1==notePad2);} }運行結(jié)果
NotePad的構(gòu)造函數(shù)com.geyao.demo.NotePad@478190fc com.geyao.demo.NotePad@478190fc?
總結(jié)
以上是生活随笔為你收集整理的spring学习(47):bean的作用域的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: figma下载_Figma的自动版式实用
- 下一篇: MAC上DOS常用命令操作