Spring IoC的具体使用及实现

Floria ·
更新时间:2024-09-20
· 751 次阅读

要求:SpringToolSuite4

工程目录:(注意:工程中不需要手动添加依赖)

 Boy.java

package com.newer.ioc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope("singleton") public class Boy { // 对象(自动分配) @Autowired // 明确指定要注入资源的标识名字 @Qualifier("girl") Loveable lover; // public void setLover(Loveable lover) { // this.lover = lover; // } // 结婚 void marry() { System.out.println("boy marry..."); lover.kiss(); } }


    @Autowired:(自动分配)哪个组件设置@Primary,哪个组件优先显示,不能把@Primary定义到多个组件,不然就会报错。
    @Qualifier("") :明确指定要注入资源的标识名字,前提是组件得命名好,如果一个组件没有命名,程序会默认是该类名的小写。

如果同时存在   @Autowired和  @Qualifier,以  @Qualifier优先

Dog.java

package com.newer.ioc; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @Component("dog") //首选的 @Primary public class Dog implements Loveable { @Override public void kiss() { System.out.println("dog kill..."); } }

Girl.java

package com.newer.ioc; import org.springframework.stereotype.Component; @Component("girl") public class Girl implements Loveable{ @Override public void kiss() { System.out.println("Girl kiss..."); } }

Loveable.java

package com.newer.ioc; import org.springframework.stereotype.Component; @Component public interface Loveable { void kiss(); }

IocApplication.java

package com.newer.ioc; import org.springframework.beans.factory.BeanFactory; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.support.ClassPathXmlApplicationContext; @SpringBootApplication public class IocApplication { public static void main(String[] args) { SpringApplication.run(IocApplication.class, args); } @Bean public CommandLineRunner app(BeanFactory factory) { return new CommandLineRunner() { @Override public void run(String... args) throws Exception { Boy boy=factory.getBean(Boy.class); boy.marry(); } }; } // 命令行执行 // @Bean public CommandLineRunner a(BeanFactory beanFactory) { // 匿名内部类 return new CommandLineRunner() { @Override public void run(String... args) throws Exception { // 创建时需要传递依赖的资源 // Person p1=new Person(); // 把对象的创建任务交给Spring 容器 Person p1=beanFactory.getBean(Person.class); // 获得第二个Perosn实例 Person p2=beanFactory.getBean(Person.class); p1.sayHello("spring"); p2.sayHello("IoC"); // hashcode 默认一样,散列值(对象的标示,类似指纹) // Java 中没有地址(指针)操作 // Spring Bean工创建的组件对象厂默认是单例 System.out.println(p1==p2); System.out.println(p1); System.out.println(p2); } }; } // @Bean public CommandLineRunner b(BeanFactory beanFactory) { // 接口中只有一个方法,可以简化成拉姆达表达式(js中箭头函数) return args->{ // 需要资源,向容器申请再获得 Cat c=beanFactory.getBean(Cat.class); System.out.println("b: "+c); // Person p1=beanFactory.getBean(Person.class); // System.out.println("b: "+p1); }; } // @Bean public CommandLineRunner c(ApplicationContext context) { return new CommandLineRunner() { @Override public void run(String... args) throws Exception { Cat c=context.getBean(Cat.class); System.out.println("c: "+c); // Person p2=beanFactory.getBean(Person.class); // System.out.println("b: "+p2); // context.registerShutdownHook(); // context.close(); } }; } }

Cat.java

package com.newer.ioc; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; //标签,组件,spring容器会管理 @Component //作用域,生存周期,默认是单例singleton 全局唯一 @Scope("prototype") public class Cat { public Cat() { System.out.println("Cat构造方法"); } // 构造后(容器回调)callback/vue 也存在钩子函数 @PostConstruct public void init() { System.out.println(this +" , init"); } // 销毁前 自动执行(容器回调) @PreDestroy public void destory() { System.out.println(this +" , destory"); } void eat() { } void run() { } void sleep() { } }

Person.java

package com.newer.ioc; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; /** * POJO * * Spring IoC容器:管理组件的生命周期 * @author Admin * */ @Component //默认是单例作用域 //原型作用域,从容器每次获得的实例不同(工厂模式) @Scope("prototype") public class Person { /** * * @param msg */ void sayHello(String msg) { System.out.println(this+" say "+msg); } }

控制台执行:

对于 Spring IoC(依赖注入)的理论知识还不够理解清晰,可以去https://blog.csdn.net/weixin_44364444/article/details/105369103

看看,欢迎有问题的小伙伴留言~!!!


作者:爷 叼烟闯天下



spring ioc

需要 登录 后方可回复, 如果你还没有账号请 注册新账号