Spring註入方式及註解配置
阿新 • • 發佈:2017-07-25
配置文件 set方法 分享 lec clas 自定義 ima setname etx
一:基於xml的DI(Dependency Injection)
註入類型:
定義學生Student實體類和小汽車Car實體類:進行封裝和生成ToString(),並自定義屬性Car
Student
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 |
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 |
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.設值註入(set方法註入):本質上是調用了Bean的setXXX()進行值的註入。分為普通屬性和域屬性
實現效果:
2.構造註入
實現效果:
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;
}
|
配置文件:
List與Set同理:
Map雙列集合:
三:基於註解的DI
[email protected][不分層的情況下]
[email protected]() [Dao層]
[email protected]() [Biz層]
[email protected]() [Action類]
註:在項目中添加Spring AOP相關的JAR文件以及xsd約束文件。
由於是基於註解的DI,所以無需再Spring配置文件中進行節點配置,只需配置包掃描器即可!
配置包掃描器用途:
該包下以及子包中的類才可以被Spring掃描,去尋找被註解的類和屬性,讓Spring容器管理賦值
Student類:
[email protected]()中植入即可。
@Value為該屬性賦值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@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 |
@Component (value= "car2" )
public class Car {
@Value ( "黑色" )
private String color;
@Value ( "奧迪" )
private String brand;
@Override
public String toString() {
return "Car [color=" + color + ", brand=" + brand + "]" ;
}
|
Spring註入方式及註解配置