springboot成神之——ioc容器(依赖注入)

时间:2022-11-24 18:36:02

springboot成神之——ioc容器(依赖注入)

spring,maven环境请自行搭建,本文重点放在ioc容器的工作原理

spring的ioc功能

一个对象的实例和字段的值被一个特殊的对象从外部注入,这个特殊的对象就是ioc

ioc容器包含了所有的Spring Beans

文件目录结构

springboot成神之——ioc容器(依赖注入)

lang

// 定义此接口,用来作为Chinese和English类的公共实现接口

package com.springlearn.learn.lang;

public interface Language{
public String getGreeting();
public String getBye();
}

Chinese

// 具体功能实现类

package com.springlearn.learn.langimpl;
import com.springlearn.learn.lang.Language; public class Chinese implements Language{
@Override
public String getGreeting(){
return "你好";
} @Override
public String getBye() {
return "再见";
}
}

English

// 具体功能实现类

package com.springlearn.learn.langimpl;
import com.springlearn.learn.lang.Language; public class English implements Language{
@Override
public String getGreeting(){
return "Hello";
} @Override
public String getBye() {
return "Bye bye";
}
}

GreetingService

// 此类就是ioc容器中的一个bean,内部属性通过外部注入
// @Service的作用就是声明他是一个bean
// @Autowired的作用就是依赖注入 package com.springlearn.learn.bean;
import com.springlearn.learn.lang.Language; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class GreetingService{ @Autowired
private Language language; public GreetingService() { } public void sayGreeting() {
String greeting = language.getGreeting();
System.out.println("Greeting:" + greeting);
}
}

MyRepository

// @Repository声明此类是一个bean,主要用于内部数据注入到其他bean中,比如MyComponent.java

package com.springlearn.learn.bean;

import java.util.Date;

import org.springframework.stereotype.Repository;

@Repository
public class MyRepository{
public String getAppName(){
return "Hello my first Spring App";
} public Date getSystemDateTime() {
return new Date();
}
}

MyComponent

// @Component声明这是一个bean,并且字面上表示这个类是个组件依赖于其他的bean,也就是上面定义的Repository

package com.springlearn.learn.bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class MyComponent {
@Autowired
private MyRepository repository; public void showAppInfo(){
System.out.println("Now is:" + repository.getSystemDateTime());
System.out.println("App Name" + repository.getAppName());
}
}

AppConfiguration

// 此类是一个定义bean和集中bean的文件
// @Configuration声明这个类是定义bean的
// @ComponentScan扫描bean目录
// @Bean(name="language") 定义了一个名为language的bean,只要访问此bean就会自动调用getLanguage方法 package com.springlearn.learn.config; import com.springlearn.learn.lang.Language;
import com.springlearn.learn.langimpl.Chinese; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan({"com.springlearn.learn.bean"})
public class AppConfiguration{ @Bean(name="language")
public Language getLanguage() {
return new Chinese();
}
}

DemoApplication

// 此类是程序的启动类
// AnnotationConfigApplicationContext会调用配置类
// 通过调用context.getBean("language");会创建Chinese类实例
// context.getBean("greetingService");greetingService其中的@Autowired会将language实例注入 package com.springlearn.learn; import com.springlearn.learn.bean.GreetingService;
import com.springlearn.learn.bean.MyComponent;
import com.springlearn.learn.config.AppConfiguration;
import com.springlearn.learn.lang.Language; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class DemoApplication {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfiguration.class); System.out.println("-------------"); Language language = (Language)context.getBean("language");
System.out.println("Bean Language: "+ language);
System.out.println("Call language.sayBye(): "+ language.getBye()); GreetingService service = (GreetingService) context.getBean("greetingService");
service.sayGreeting();
System.out.println("----------"); MyComponent myComponent = (MyComponent) context.getBean("myComponent");
myComponent.showAppInfo();
}
}

运行结果

-------------
Bean Language: com.springlearn.learn.langimpl.Chinese@258d79be
Call language.sayBye(): 再见
Greeting:你好
----------
Now is:Sun Sep 09 13:48:45 CST 2018
App NameHello my first Spring App