//定义个接口玩一玩publicinterfaceBeanFactory{publicvoidBeantest();}//实现这个接口,这个是需要注入到容器中的组件也就是beanpublicclassBeanFactroyImplimplementsBeanFactory{@OverridepublicvoidBeantest(){System.out.println("----------------This is a 传统的XML配置的bean!-------------------");}}//在web.xml指定的ApplicationContext.xml文件中指定注入<?xml version="1.0" encoding="UTF-8"?><beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">;<--这个就是要注入的对象啦!--><bean id="beanFactroy"class="com.scma.service.impl.BeanFactroyImpl"/>;</beans>//测试一把看看@Testpublicvoidtest(){ApplicationContext ctx=newClassPathXmlApplicationContext("applicationContext.xml");BeanFactory beanFactory=(BeanFactory) ctx.getBean("beanFactroy");beanFactory.Beantest();//----------------This is a 传统的XML配置的bean!-------------------}
二:进阶,基于java注解的配置
如果一个类使用了@Service,那么此类将自动注册成一个bean,
不需要再在applicationContext.xml文件定义bean了,
类似的还包括@Component、@Repository、@Controller。
要实现这个,只需要在spring的配置文件里面加上这个,让他晓得去哪里加载
在applicationContext.xml文件中加一行
<context:component-scan base-package="com.scma"/>
作用是自动扫描base-package包下的注解
// 这次就高级了,一个注解就搞定了@Service("beanFactory")//别名可以不加直接 @ServicepublicclassBeanFactroyImplimplementsBeanFactory{@OverridepublicvoidBeantest(){System.out.println("----------------This is a 基于Java注解的bean!-------------------");}}//启动文件的写法<?xml version="1.0" encoding="UTF-8"?>;<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">;<--com.stonegeek这个就是要扫描的路径啦!--><--路径下的所有这些注解 都会当成组件注入到容器中供使用--><context:component-scan base-package="com.scma"/>;</beans>//最后来测试一把@Testpublicvoidtest(){// 擦皮鞋这个类 spring容器ApplicationContext ctx=newClassPathXmlApplicationContext("applicationContext.xml");BeanFactory beanFactory=(BeanFactory) ctx.getBean("beanFactory");beanFactory.Beantest();//This is a 基于java注解的bean!}
三:高阶,基于类的JavaConfig
/** 通过java类定义spring配置元数据,且直接消除xml配置文件 **///springboot整合mybatis-plus的分页插件的时候就是这么玩的,还记得吗publicinterfaceBeanFactory{publicvoidBeantest();}//这个实现类 啥注解也不加publicclassBeanFactoryImplimplementsBeanFactory{@OverridepublicvoidBeantest(){System.out.println("----------------This is a 基于类的Java Config的bean!-------------------");}}// 配置文件里面进行注入@ConfigurationpublicclassBeanConfig{@BeanpublicBeanFactorybeanFactory(){returnnewBeanFactoryImpl();}}//测试一把publicclassTestBean3{@Testpublicvoidtest(){ApplicationContext applicationContext=newAnnotationConfigApplicationContext(BeanConfig.class);BeanFactory beanFactorys=applicationContext.getBean(BeanFactory.class);beanFactorys.Beantest();//This is a 基于类的Java Config Bean!}}