SpringIoC的javaconfig使用

这个也是属于spring的,

JavaConfig 原来是 Spring 的一个子项目,它通过 Java 类的方式提供 Bean 的定义信息,在 Spring4 的版本, JavaConfig 已正式成为 Spring4 的核心功能 。

绑定Java与XML配置

@Configuration    // 就相当于创建了一个xml 文件 <beans></beans>
@ComponentScan("cn.edsion")   //<context:component-scan base-package="cn.edsion" >
@PropertySource("classpath:db.properties")
public class MainConfiration  {


    @Value("${mysql.username}")
    private String name;
    @Value("${mysql.password}")
    private String password;
    @Value("${mysql.url}")
    private String url;
    @Value("${mysql.driverClassName}")
    private String driverName;


    // <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource"></bean>
    // 可以干预Bean实例化过程!
    @Bean
    public DruidDataSource dataSource(){
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setName(name);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driverName);
        return dataSource;

    }

    //init-method="initByConfig" destroy-method="destroyByConfig"
    @Bean(initMethod = "initByConfig",destroyMethod = "destroyByConfig")
    public User userconf(){

        return  new User();
    }
}
使用AnnotationConfigApplicationContext初始化Spring容器
简单结构

@Test
public void test01(){
    ApplicationContext ioc=new AnnotationConfigApplicationContext(MainConfiration.class);
    UserController bean = ioc.getBean(UserController.class);
    bean.getUser();
}
@Bean 注解
声明一个bean

@Bean是一个方法级别的注解,它与XML中的 元素类似。注解支持 提供的一些属性,例如 * init-method * destroy-method * autowiring * name
开发者可以在@Configuration类或@Component类中使用@Bean注解。
@Configuration
public class AppConfig {

    @Bean
    public TransferService transferService() {
        return new TransferServiceImpl();
    }
}

Bean之间的依赖关系
可以使用方法参数来实现该依赖关系

@Configuration
public class AppConfig {

    @Bean
    public TransferService transferService(AccountRepository accountRepository) {
        return new TransferServiceImpl(accountRepository);
    }
}


接受生命周期回调

@Bean(initMethod = "initByConfig",destroyMethod = "destroyByConfig")
public User userconf(){
    return  new User();
}


指定Bean的作用域

@Configuration
public class MyConfiguration {
    @Bean
    @Scope("prototype")
    public Encryptor encryptor() {
        // ...
    }
}


自定义Bean的名字

//默认情况下,配置类使用@Bean方法的名称作为结果bean的名称。 
//但是,可以使用name属性覆盖此功能,如以下示例所示:
@Configuration
public class AppConfig {

    @Bean(name = "myThing")
    //多个别名:@Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })
    public Thing thing() {
        return new Thing();
    }
}

@Configuration 注解
注入内部bean依赖

//当Bean彼此有依赖关系时,表示依赖关系就像调用另一个bean方法一样简单.如下例所示:
@Configuration
public class AppConfig {
    @Bean
    public BeanOne beanOne() {
        return new BeanOne(beanTwo());
    }
    @Bean
    public BeanTwo beanTwo() {
        return new BeanTwo();
    }
}
构成基于Java的配置
@Import 注解

//就像在Spring XML文件中使用<import/>元素来帮助模块化配置一样,
//@Import 注解允许从另一个配置类加载@Bean定义,如下例所示:
@Configuration
public class ConfigA {

    @Bean
    public A a() {
        return new A();
    }
}

@Configuration
@Import(ConfigA.class)
public class ConfigB {

    @Bean
    public B b() {
        return new B();
    }
}
@Bean是我们实例化好之后,交给spring管理

@Configuration替换xml文件

 怎么去自动依赖外部Bean:直接在方法里面写上需要依赖的参数即可,不需要写@Autowired
怎么去自动依赖内部Bean:直接调用方法即可

@Import这个也很重要

真的很重要,源码里都是它!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

@Import用法_小丑竟是我自己-CSDN博客

 

将一个类注入到ioc中:

1.xml:

2.@Component (@Controller,@Service,@Repository)

3.@Bean

@Import

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

美丽人生1989

有用的小伙伴可以打赏,多谢

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

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

打赏作者

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

抵扣说明:

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

余额充值