One 、Bean Scope

spring When the container is created , Will be all configured bean Object creation , Default bean It's all singletons . The code passes getBean() Method gets the specified bean example , The container first calls Bean Class , Create instance object

that ? How can we explain bean Is it a single case ?

Build two student objects , perform , Found that the memory addresses of the two objects are the same , Only one copy in memory

How to make it multi instance ? Then you need to add scope="prototype" This property can be !

scope="prototype" Archetypal model (N Objects ): It's only created when it's actually used , Every time you get , Will create different objects
scope="singleton" The singleton pattern : Container initialization requires the use of name build , Every time you get the same object , The default value is


Two 、 be based on xml Of DI(Dependency Injection)

Injection type :

Define students Student Entity classes and cars Car Entity class : Encapsulate and generate ToString(), And customize the properties 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;
 
// No arguments structure
public  Student() {
     //System.out.println("Student.Student()");
}
 
// Belt structure
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;
 
 
// No arguments structure
public  Car() {
     
}
 
// Belt structure
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 value injection (set Methods to inject ): It's essentially a call to Bean Of setXXX() To inject values . It is divided into common attributes and domain attributes

Test class :

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);
}

Realization effect :


1.2 Tectonic injection

Realization effect :


1.3 Namespace p Inject
Before you use it, you should Spring Introduce... In the configuration file p Namespace

Realization effect :


3、 ... and 、 Set attribute Injection [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 The configuration file :

List And Set Empathy :

Map Double column set :

Test class : Call the corresponding method :

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());
}


Four 、 Annotation based DI

notes : Add... To the project Spring AOP dependent JAR Files and xsd Constraint file .

Because it's annotation based DI, So there's no need to Spring Node configuration in the configuration file , Just configure the packet scanner !

Configure packet scanner usage :

Only the classes under the package and in the subpackages can be Spring scanning , To find annotated classes and properties , Give Way Spring Container management

Student class :

Appoint @Component Medium value In the test class getBean() It can be implanted into the body .

@Value Assign... To this property

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 ( " ha-ha " )   
private  String name;
@Value ( "13" )
private  String age;
 
/*
  * JDK annotation @Resource(name="car2")
  */
 
/*
  * Spring annotation
  */
@Autowired
@Qualifier (value= "car2" )
 
private  Car car;
 
 
@Override
public  String toString() {
     return  "Student [name="  + name +  ", age="  + age +  ", car="  + car +  "]" ;
}

Car class :

1
2
3
4
5
6
7
8
9
10
11
12
13
@Component (value= "car2" )
 
public  class  Car {
@Value ( " black " )
private  String color;
@Value ( " audi " )
private  String brand;
 
 
@Override
public  String toString() {
     return  "Car [color="  + color +  ", brand="  + brand +  "]" ;
}

Test class :

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);
}
}

Realization effect :

Equivalent to @Component Annotations :

@Component[ Without layering ]

@Repository() [Dao layer ]

@Service() [Biz layer ]

@Controller() [Action class ]

Chapter two spring More articles about

  1. Spring Learning Guide - Chapter two -Spring frame foundation ( End )

    Chapter two Spring frame foundation Interface oriented programming design method ​ In the last chapter , We see a dependency on other classes POJO Class contains a reference to a concrete class of its dependencies . for example ,FixedDepositController Class inclusion ...

  2. Chapter two Spring MVC introduction

    2.1.Spring Web MVC What is it? Spring Web MVC It's based on Java Realized. Web MVC Design pattern request driven type lightweight Web frame , That is to use MVC The idea of architecture pattern , take web I'm going to take up my duty ...

  3. [ translate ]Spring Framework reference document (V4.3.3)- Chapter two Spring The framework is introduced 2.1 2.2 translate --2.3 To be continued

    English links :http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/overview.ht ...

  4. java Concurrent learning -- Chapter two spring boot Realize the creation of thread

    In addition to the thread creation method described earlier ,spring boot Provides us with a complete way to create threads , Which includes : Threads . Thread pool . Thread monitoring . One . Use spring boot Provides methods to create threads and thread pools 1. First, in the sp ...

  5. Chapter two Spring MVC introduction —— Follow Kaitao SpringMVC Journal entry 1

    2.1.Spring Web MVC What is it? Spring Web MVC It's based on Java Realized. Web MVC Design pattern request driven type lightweight Web frame , That is to use MVC The idea of architecture pattern , take web I'm going to take up my duty ...

  6. Spring 3.x practice First example (Spring 3.x Enterprise application development practical reading notes Chapter 2 )

    Preface : I've been working on android, Now we need to negotiate more with backstage people , The technology stack is different , It's hard to avoid the same thing , So learn a little bit . This example is taken from <Spring 3.x Enterprise application development actual combat > The second chapter of the book ,I ...

  7. #Spring Chapter 2 learning notes ———— assembly Bean

    Spring Chapter 2 learning notes ---- assembly Bean The act of creating a collaborative relationship between application objects is often called assembly (wiring). It's also dependency injection (DI) The essence of . Spring Configuration options When describing bean How to be assembled , ...

  8. The third chapter Spring.Net Environment preparation and construction

    In the previous chapter, we introduced dependency injection , The concept of inversion of control . Now let's really start building Spring.Net Environment , to glance at Spring.Net What does inversion of control and dependency injection look like in . 3.1  Spring.Net  Next ...

  9. Java Persistence with MyBatis 3( Chinese version ) Chapter two guide MyBatis

    MyBatis The key component is SqlSessionFactory, We can get it from it SqlSession, And execute the mapped SQL sentence .SqlSessionFactory Objects can be created based on XML Or ...

Random recommendation

  1. .NET Interview questions series [14] - LINQ to SQL And IQueryable

    .NET Interview question series catalog Aphorisms " understand IQueryable The easiest way to do this is , Think of it as a query , At the time of execution , A sequence of results will be generated ." - Jon Skeet LINQ to Obje ...

  2. be based on Spring Scalable for Schema Develop custom configuration tag support

    One . background Recently, I want to develop a similar project with my friends alibaba dubbo The function of the tool , One of them is based on Spring Scalable for Schema Develop custom configuration tag support , I wrote one by looking up information on the Internet demo. Here today ...

  3. MBR in “ Starting head / A sector / cylinder “ Same as &quot; Logical block address (LBA)&quot; The difference between

    " Starting head / A sector / cylinder " share 3 Bytes , Maximum energy means 8G Sector number of . When the hard disk sector number is more than 8G when , This expression is not good enough , Then use 4 Bytes of LBA notation ( Logical sector address , Relative sector address ). It's a calculation ...

  4. JS Set up Cookie, And COOKIE The limitation of

    stay Javascript Script , One cookie It's actually a string property . When you read cookie The value of , You get a string , It's inside WEB All of the cookies Name and value of . Every cookie except name ...

  5. elasticsearch Query template

    { "from":#from#, "size":#size#, "_source":#source#, "query": ...

  6. JavaSE note - notes

    Annotation Annotation It's an interface , You can put Annotation As a modifier Annotation The definition of Comments by @interface Definition public @interface Tes ...

  7. tomcat Easy to use

    Download decompression tomcat[root@localhost]# tar zxf apache-tomcat-8.5.4.tar.gz -C /usr/local/tomcat yum Bring their own JDK, Pay attention to differences JRE ...

  8. Struts2 Study ( 3、 ... and )———— Input checksums interceptor

    One . Input verification When we used to write a login page , There are no restrictions on user input , No matter what the user enters , We're all in the database , Obviously, it's not going to work , We need to check whether the text entered by the user is legal , Whether it meets the text format we need , In line with the food release , and s ...

  9. tfs Team project delete the default account of the original connection

    1. In use visual studio When connecting team projects , The first time you enter your user name and password , The credentials are saved by default , After that, the connection will automatically adopt the first certificate . But how to use a new user to log in again . As shown in the figure , Delete the original voucher . Delete and restart the computer ...

  10. mongoDB install windows 64 bit

    mongoDB install windows 64 bit   https://www.mongodb.org/downloads?_ga=1.207888916.746558625.1410501054 download , ...