Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.demo;
- import org.springframework.beans.factory.annotation.Required;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.*;
- import org.springframework.stereotype.Component;
- class Tiger implements AnnotationConfig.Animal {
- private String name;
- public Tiger(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- }
- class Lion implements AnnotationConfig.Animal {
- private String name;
- public String getName() {
- return name;
- }
- public Lion(String name) {
- this.name = name;
- }
- }
- @Configuration
- class AnnotationConfig {
- @Bean(name = {"tiger", "kitty"})
- @Scope(value = "prototype")
- Tiger getTiger(String name) {
- return new Tiger(name);
- }
- @Bean(name = "lion")
- Lion getLion() {
- return new Lion("Hardcoded lion name");
- }
- interface Animal {}
- }
- @SpringBootApplication
- public class DemoApplication {
- public static void main(String[] args) {
- //SpringApplication.run(DemoApplication.class, args);
- ApplicationContext context = new AnnotationConfigApplicationContext(AnnotationConfig.class);
- Tiger t = (Tiger) context.getBean("tiger", "sherkan");
- System.out.println(t.getName());//*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement