Created
June 2, 2019 08:19
-
-
Save bandicoot86/a457e4fd60956859d4664c17a8878af6 to your computer and use it in GitHub Desktop.
Autowired Annotations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
@Configuration | |
public class SimpleSpringApplication { | |
public static void main(String[] args) { | |
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SimpleSpringApplication.class); | |
System.out.println(context.getBean(B1AndB2Holder.class)); | |
} | |
@Bean | |
public B1AndB2Holder holder() { | |
return new B1AndB2Holder(); | |
} | |
@Bean | |
public Bean1 b1() { | |
return new Bean1(); | |
} | |
@Bean | |
public Bean2 b2() { | |
return new Bean2(); | |
} | |
public class Bean1 { | |
} | |
public class Bean2 { | |
} | |
public class B1AndB2Holder { | |
@Autowired | |
private Bean1 b1; | |
@Autowired | |
private Bean2 b2; | |
@Override | |
public String toString() { | |
return "B1AndB2Holder{" + | |
"b1=" + b1 + | |
", b2=" + b2 + | |
'}'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment