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);
}
}