SpringIoc基于javaConfig的应用

1.实体类

Person类

package com.hwy.beans;

import org.springframework.beans.factory.annotation.Value;

public class Person {
    @Value("0")
    private Integer age;
    @Value("wanwan")
    private String name;

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Role类

package com.hwy.beans;

import org.springframework.stereotype.Component;


@Component
public class Role {

    private String name="角色";

    public  Role (){
        System.out.println("role已加载");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

User类

package com.hwy.beans;

public class User {

}

MyImportSelector类

package com.hwy.beans;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.stereotype.Component;

@Component
public class MyImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {
        //以字符串的形式返回多个bean
        return new String[]{"com.hwy.beans.Person"};
    }
}

2.主配置类

import com.alibaba.druid.pool.DruidDataSource;
import com.hwy.beans.MyImportSelector;
import com.hwy.beans.Role;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

@Configuration
@ComponentScan(basePackages = "com.hwy")
@PropertySource("classpath:db.properties")
@Import({SecondJavaConfig.class, MyImportSelector.class})
public class IocJavaConfig {

    /*<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
        <property name="username" value="${mysql.username}"></property>
        <property name="password" value="${mysql.password}"></property>
        <property name="url" value="${mysql.url}"></property>
        <property name="driverClassName" value="${mysql.driverName}"></property>
    </bean>*/

    /*
    * 可以将一个bean的实例注册为bean
    * */
    @Value("${mysql.username}")
    private String username;
    @Value("${mysql.password}")
    private String password;
    @Value("${mysql.url}")
    private String url;
    @Value("${mysql.driverName}")
    private String driverClassName;

    @Bean
    public DruidDataSource dataSource(){
        DruidDataSource datasource=new DruidDataSource();
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setUrl(url);
        datasource.setDriverClassName(driverClassName);
        System.out.println(role());
        return datasource;

    }

    @Bean
    public com.hwy.beans.Role role(){
        return new Role();
    }


}

3.其他配置类

import com.hwy.beans.Role;
import com.hwy.beans.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SecondJavaConfig {

    @Bean
    public User user(){
        return new User();
    }


}

db.properties

mysql.username=root
mysql.password=HWY666888
mysql.url=jdbc:mysql://localhost:3306/test
mysql.driverName=com.mysql.jdbc.Driver

4.测试类


import com.alibaba.druid.pool.DruidDataSource;
import com.hwy.beans.Person;
import com.hwy.beans.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class IocTest {

    AnnotationConfigApplicationContext ioc;

    @Before
    public void before() {

        ioc=new AnnotationConfigApplicationContext(IocJavaConfig.class);
        System.out.println("spring容器加载");
    }

    @Test
    public void test(){
        DruidDataSource dataSource = (DruidDataSource)ioc.getBean("dataSource");
        System.out.println(dataSource);
    }

    @Test
    public void test2(){
        User user = (User)ioc.getBean("user");
        System.out.println(user);
    }

    @Test
    public void test3(){
        Person person = (Person)ioc.getBean(Person.class);
        System.out.println(person);
    }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值