Spring配置bean的三种方式

一:传统方式

//定义个接口玩一玩
public interface BeanFactory {
    public void Beantest();
}

//实现这个接口,这个是需要注入到容器中的组件也就是bean
public class BeanFactroyImpl implements BeanFactory {
    @Override
    public void Beantest() {
        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>


//测试一把看看
   @Test
    public void test(){
        ApplicationContext ctx= new ClassPathXmlApplicationContext("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") //别名可以不加直接 @Service
public class BeanFactroyImpl implements BeanFactory {
    @Override
    public void Beantest() {
        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>



//最后来测试一把
@Test
    public void test(){
    // 擦皮鞋这个类 spring容器
        ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
        BeanFactory beanFactory=(BeanFactory) ctx.getBean("beanFactory");
        beanFactory.Beantest();  //This is a 基于java注解的bean!
    }


三:高阶,基于类的JavaConfig

/** 通过java类定义spring配置元数据,且直接消除xml配置文件 **/
//springboot整合mybatis-plus的分页插件的时候就是这么玩的,还记得吗


public interface BeanFactory {
    public void Beantest();
} 

//这个实现类 啥注解也不加
public class BeanFactoryImpl implements BeanFactory {
    @Override
    public void Beantest() {
        System.out.println("----------------This is a 基于类的Java Config的bean!-------------------");
    }
}

// 配置文件里面进行注入
@Configuration
public class BeanConfig {
    @Bean
    public BeanFactory beanFactory(){
        return new BeanFactoryImpl();
    }
}


//测试一把
public class TestBean3 {
    @Test
    public void test(){
        ApplicationContext applicationContext=new AnnotationConfigApplicationContext(BeanConfig.class);
        BeanFactory beanFactorys=applicationContext.getBean(BeanFactory.class);
        beanFactorys.Beantest(); 
         //This is a 基于类的Java Config Bean!
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码头薯条Pro

本文能帮到阁下,在下很开心!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值