资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Java中Spring框架之IOC如何配置

这篇文章主要介绍了Java中Spring框架之IOC如何配置,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

创新互联公司是一家专业提供阿巴嘎企业网站建设,专注与成都做网站、网站设计、外贸营销网站建设成都h5网站建设、小程序制作等业务。10年已为阿巴嘎众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。

Spring的IOC配置

Spring最重要的特性是IOC控制反转,利于IOC我们能降低对象之间的耦合性。

IOC需要通过一定的配置实现,配置方法分为:

1)使用xml文件配置

2)使用注解配置

使用Spring的基本功能,必须先导入Spring的依赖:

  1. org.springframework

  2. spring-context

  3. 5.1.5.RELEASE

Spring Context:向 Spring框架提供上下文信息。Spring 上下文包括企业服务,例如JNDI、EJB、电子邮件、国际化、校验和调度功能。它包含Spring Core组件,能实现IOC的核心功能。

使用xml文件配置

  1. /**

  2. * CPU接口

  3. */

  4. public interface Cpu {

  5. void run();

  6. }

  7. /**

  8. * AMD的CPU

  9. */

  10. public class AMDCpu implements Cpu {

  11. public void run() {

  12. System.out.println("AMD的CPU正在运行....");

  13. }

  14. }

  15. /**

  16. * 内存接口

  17. */

  18. public interface Memory {

  19. void read();

  20. void write();

  21. }

  22. /**

  23. * DDR8G的内存

  24. */

  25. public class DDR8GMemory implements Memory {

  26. public void read() {

  27. System.out.println("使用DDR8G的内存读取数据....");

  28. }

  29. public void write() {

  30. System.out.println("使用DDR8G的内存写入数据....");

  31. }

  32. }

  33. 类似的IntelCpu和DDR16Memory类省略了代码

  34. /**

  35. * 电脑类

  36. */

  37. public class Computer {

  38. private Cpu cpu;

  39. private Memory memory;

  40. private String brand;

  41. ...省略get\set

  42. public Computer() {
    }

            public Computer(String brand, Cpu cpu, Memory memory) {
             this.brand = brand;
         this.cpu = cpu;
         this.memory = memory;
            }

            public void start(){
         System.out.println(brand+"电脑启动了");
         cpu.run();
         memory.read();
         memory.write();
            }

  43. }

在maven项目的resources目录下,添加配置文件:

applicationContext.xml

  1. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2. xmlns:context="http://www.springframework.org/schema/context"

  3. xsi:schemaLocation="http://www.springframework.org/schema/beans

  4. http://www.springframework.org/schema/beans/spring-beans.xsd

  5. http://www.springframework.org/schema/context

  6. http://www.springframework.org/schema/context/spring-context.xsd">

配置说明:

是根标签,代表Spring的Java对象容器

标签代表在容器中创建一个Java对象,属性id代表对象名,class是对象的类型。

在配置文件中首先创建了一个cpu对象和一个memory对象,然后创建了一个computer对象,computer中有Cpu类型的cpu属性和Memory类型memory属性以及String类型的brand属性,这里使用依赖注入的方式给属性赋值。

property 指的是对象的属性,name是属性名,ref是对象引用,这里引用了前面的cpu对象。

brand属性注入的是数值而不是对象引用,这里使用value注入值。

Spring上下文对象

Spring容器可以看做是一个JavaBean的工厂BeanFactory,BeanFactory负责创建并保存各个JavaBean,BeanFactory的子类有:

1)ClassPathXMLApplicationContext

基于XML配置文件上下文

2)AnnotationConfigApplicationContext

基于注解配置的上下文

3)FileSystemApplicationContext

基于文件系统的上下文

使用ClassPathXMLApplicationContext的方法:

  1. public class TestComputer {

  2. @Test

  3. public void testComputer(){

  4. //创建XML文件的应用程序上下文对象

  5. ClassPathXmlApplicationContext cxt =

  6. new ClassPathXmlApplicationContext("applicationContext.xml");

  7. //通过类型从容器获得Java对象

  8. Computer computer = cxt.getBean(Computer.class);

  9. //还可以通过对象名获得对象

  10. //        Computer computer = (Computer) cxt.getBean("computer");

  11. computer.start();

  12. }

  13. }

Java中Spring框架之IOC如何配置

使用注解配置

Spring的IOC也可以不使用配置文件,完全通过Java代码和注解实现配置,这种配置方法代码更加简洁。

常用注解:

@Component

配置到类上面,Spring容器会自动扫描并添加有该注解类的对象

@Autowired

配置到属性或set方法上,容器会将容器中同类型的对象自动注入到属性中

@Qualifier

用于给不同的组件设置标识,用于区分多个相同类型的对象

@Value

注入一般类型的值,如:@Value(20) 、 @Value("张三")

@Configuration

加在配置类上,该类作为Spring启动的入口

@ComponentScan

和@Configuration配合使用,加在配置类上,用于扫描包中所有@Component注解的类

  1. 在DDR8Memory类和IntelCpu类上添加@Component注解

  2. 修改Computer类:

  3. @Component

  4. public class Computer {

  5. @Value("苹果电脑")

  6. private String brand;

  7. @Autowired

  8. private Cpu cpu;

  9. @Autowired

  10. private Memory memory;

  11. ....

  12. }

  13. @Configuration

  14. @ComponentScan("com.qianfeng.springioc.demo4")

  15. public class MyConfig {

  16. public static void main(String[] args) {

  17. //创建基于注解的上下文对象

  18. AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfig.class);

  19. //获得Computer对象

  20. Computer computer = cxt.getBean(Computer.class);

  21. computer.start();

  22. }

  23. }

Java中Spring框架之IOC如何配置

感谢你能够认真阅读完这篇文章,希望小编分享的“Java中Spring框架之IOC如何配置”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!


文章标题:Java中Spring框架之IOC如何配置
URL网址:http://cdkjz.cn/article/iieogd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220