使用注解启动IOC容器

/ Spring / 没有评论 / 317浏览

在上篇文章中,虽然我们使用注解成功实例化了对象,但是我们还是依赖了spring的配置文件扫描注解类的路径,在这篇中我们将完全采用注解的方工启动IOC容器。

@Component("work")
public class Work {

private String address;

public Work() {
setAddress("北京");
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "Work{" +
"address='" + address + '\'' +
'}';
}
}
@Component("user")
public class User {

private String username;
private String password;
private String nickname;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}
@Configuration
public class AppConf {

@Bean
public Work getWork() {
Work work = new Work();
work.setAddress("北京");
return work;
}

@Bean
public User getUser() {
User user = new User();
user.setUsername("admin");
user.setPassword("jilinwula");
user.setNickname("吉林乌拉");
return user;
}
}
@Test
public void test() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConf.class);
User user = applicationContext.getBean(User.class);
Work work = applicationContext.getBean(Work.class);
System.out.println(user);
System.out.println(work);
}
User{username='admin', password='jilinwula', nickname='吉林乌拉'}
Work{address='北京'}

我们可以用下面的方式加载多个配置注解类。

@Configuration
public class AppCarConf {

@Bean
public Car getCar() {
Car car = new Car();
car.setBrand("吉普");
return car;
}
}
@Component("car")
public class Car {
private String brand;

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
'}';
}
}
@Test
public void test() {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
annotationConfigApplicationContext.register(AppConf.class); // 加载多个配置注解类
annotationConfigApplicationContext.register(AppCarConf.class);
annotationConfigApplicationContext.refresh();
User user = annotationConfigApplicationContext.getBean(User.class);
Work work = annotationConfigApplicationContext.getBean(Work.class);
Car car = annotationConfigApplicationContext.getBean(Car.class);
System.out.println(user);
System.out.println(work);
System.out.println(car);
}
User{username='admin', password='jilinwula', nickname='吉林乌拉'}
Work{address='北京'}
Car{brand='吉普'}

我们也可以通过下面的方式将多个注解类组合成一个注解类,这样我们只需要加载一个注解类就可以使用所有注解类中的对象了。

@Configuration
@Import(AppCarConf.class)
public class AppConf {

@Bean
public Work getWork() {
Work work = new Work();
work.setAddress("北京");
return work;
}

@Bean
public User getUser() {
User user = new User();
user.setUsername("admin");
user.setPassword("jilinwula");
user.setNickname("吉林乌拉");
return user;
}
}
@Test
public void test() {
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
annotationConfigApplicationContext.register(AppConf.class);
annotationConfigApplicationContext.refresh();
User user = annotationConfigApplicationContext.getBean(User.class);
Work work = annotationConfigApplicationContext.getBean(Work.class);
Car car = annotationConfigApplicationContext.getBean(Car.class);
System.out.println(user);
System.out.println(work);
System.out.println(car);
}
User{username='admin', password='jilinwula', nickname='吉林乌拉'}
Work{address='北京'}
Car{brand='吉普'}

我们也可以直接用注解的方式引入xml中的对象

public class User {

private String username;
private String password;
private String nickname;
private Work work;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getNickname() {
return nickname;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

public Work getWork() {
return work;
}

public void setWork(Work work) {
this.work = work;
}

@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", nickname='" + nickname + '\'' +
", work=" + work +
'}';
}
}
@Configuration
@ImportResource("classpath:bean.xml")
public class AppConf {

@Autowired
private Work work;

@Bean
public User getUser() {
User user = new User();
user.setUsername("admin");
user.setPassword("jilinwula");
user.setNickname("吉林乌拉");
user.setWork(work);
return user;
}
}
<context:component-scan base-package="com.jilinwula.spring"/>
<bean id="work" class="com.jilinwula.spring.work.Work" p:address="杭州"/>
User{username='admin', password='jilinwula', nickname='吉林乌拉', work=Work{address='杭州'}}