第二章 spring
一、Bean作用域
spring容器創建的時候,會將所有配置的bean對象創建出來,默認bean都是單例的。代碼通過getBean()方法從容器獲取指定的bean實例,容器首先會調用Bean類的無參構造器,創建實例對象
那么?我們如何說明出bean是單例的呢?
構建出兩份學生對象,執行,發現兩個對象的內存地址相同,內存中只有一份
?
如何使它成為多例的呢?那么則需要在配置文件中添加scope="prototype"該屬性即可!
scope="prototype" 原型模式(N個對象):真正使用時才會創建,每獲取一次,都會創建不同對象
scope="singleton" 單例模式:容器初始化時需要使用name建,每次獲取的都是同一個對象,默認值
?二、基于xml的DI(Dependency Injection)
注入類型:
定義學生Student實體類和小汽車Car實體類:進行封裝和生成ToString(),并自定義屬性Car
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | public?class?Student { private?String name; private?String age; private?Car car; //無參構造 public?Student() { ????//System.out.println("Student.Student()"); } //帶參構造 public?Student(String name, String age, Car car) { ????this.name = name; ????this.age = age; ????this.car = car; } @Override public?String toString() { ????return?"Student [name="?+ name +?", age="?+ age +?", car="?+ car +?"]"; } public?Car getCar() { ????return?car; } public?void?setCar(Car car) { ????this.car = car; } public?String getAge() { ????return?age; } public?void?setAge(String age) { ????this.age = age; } public?String getName() { ????return?name; } public?void?setName(String name) { ????this.name = name; } } |
Car:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public?class?Car { private?String color; private?String brand; //無參構造 public?Car() { ????? } //帶參構造 public?Car(String color, String brand) { ????super(); ????this.color = color; ????this.brand = brand; } @Override public?String toString() { ????return?"Car [color="?+ color +?", brand="?+ brand +?"]"; } public?String getColor() { ????return?color; } public?void?setColor(String color) { ????this.color = color; } public?String getBrand() { ????return?brand; } public?void?setBrand(String brand) { ????this.brand = brand; } } |
1.1設值注入(set方法注入):本質上是調用了Bean的setXXX()進行值的注入。分為普通屬性和域屬性
?
測試類:
| 1 2 3 4 5 6 7 8 | public?class?Test01 { @Test public?void?addTest(){ ????ApplicationContext ctx=new?ClassPathXmlApplicationContext("applicationContext.xml"); ????? ????Student student=(Student) ctx.getBean("stu"); ????System.out.println(student); } |
實現效果:
?
?1.2構造注入
?
實現效果:
1.3命名空間p注入
使用前要先要在Spring配置文件中引入p命名空間
?
實現效果:
?
三、集合屬性注入[List、Set、Map]
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public?class?MyCollection { private?List<String> list; private?Set<String> set; private?Map<String,String> map; public?Map<String, String> getMap() { ????return?map; } public?void?setMap(Map<String, String> map) { ????this.map = map; } public?Set<String> getSet() { ????return?set; } public?void?setSet(Set<String> set) { ????this.set = set; } public?List<String> getList() { ????return?list; } public?void?setList(List<String> list) { ????this.list = list; } |
Spring配置文件:
?List與Set同理:
?
?
?
Map雙列集合:
?
?
測試類:調用對應的方法:
| 1 2 3 4 5 6 7 8 9 10 | public?class?Test01 { @Test public?void?addTest(){ ????ApplicationContext ctx=new?ClassPathXmlApplicationContext("applicationContext.xml"); ????MyCollection collection=(MyCollection) ctx.getBean("collection"); ????//System.out.println(collection.getList()); ????//System.out.println(collection.getSet()); ????System.out.println(collection.getMap()); } |
四、基于注解的DI
注:在項目中添加Spring AOP相關的JAR文件以及xsd約束文件。
由于是基于注解的DI,所以無需再Spring配置文件中進行節點配置,只需配置包掃描器即可!
配置包掃描器用途:
該包下以及子包中的類才可以被Spring掃描,去尋找被注解的類和屬性,讓Spring容器管理賦值
?
Student類:
指定@Component中的value即可在測試類中的getBean()中植入即可。
@Value為該屬性賦值
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | @Component(value="stu") public?class?Student { @Value("呵呵")??? private?String name; @Value("13") private?String age; /* ?* JDK注解 @Resource(name="car2") ?*/ /* ?* Spring注解 ?*/ @Autowired @Qualifier(value="car2") private?Car car; @Override public?String toString() { ????return?"Student [name="?+ name +?", age="?+ age +?", car="?+ car +?"]"; } |
Car類:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | @Component(value="car2") public?class?Car { @Value("黑色") private?String color; @Value("奧迪") private?String brand; @Override public?String toString() { ????return?"Car [color="?+ color +?", brand="?+ brand +?"]"; } |
測試類:
| 1 2 3 4 5 6 7 8 9 | public?class?Test01 { @Test public?void?addTest(){ ????ApplicationContext ctx=new?ClassPathXmlApplicationContext("applicationContext.xml"); ????? ????Student student=(Student) ctx.getBean("stu"); ????System.out.println(student); } } |
實現效果:
等價于@Component的注解:
@Component[不分層的情況下]
@Repository() [Dao層]
?@Service() [Biz層]
@Controller() [Action類]?
?
轉載于:https://www.cnblogs.com/hr1997/p/5993387.html
總結
以上是生活随笔為你收集整理的第二章 spring的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django【进阶篇 】
- 下一篇: 命令模式——HeadFirst设计模式学