spring in action之bean装配

spring in action之bean装配

本文介绍了sping in action之bean装配,内容包括自动扫描、Java代码装配、xml装配、选择哪种装配方式最合适以及项目中犯过的错误等。

sping in action之bean装配

  • 自动扫描
  • Java代码装配
  • xml装配
  • 选择哪种装配方式最合适?
  • 项目中犯过的错误

自动扫描

User类
ComponentScan注解在spring中启用组件扫描,默认扫描与配置类相同的包,查找带有Component注解的类。也可以通过xml配置启用组件扫描。

1
2
3
4
5
6
7
package spring.di.componentScan.dto;

@Configuration
@ComponentScan
public class User {

}

Role类
Component注解表明该类会作为组件类,并告知spring要为这个类创建bean。

1
2
3
4
5
6
package spring.di.componentScan.dto;

@Component
public class Role{

}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=User.class)
public class BeanTest {

@Autowired
private Role role;

@Test
public void roleNotNull(){
assertNotNull(role);
}
}

单元测试能够通过,成功创建了Role的bean实例,默认的实例ID为role,也就是将类名的第一个字母变为小写,可以使用@Component(“reNameRole”)将ID修改为reNameRole。

这种方式需要在类上添加@Component和@Autowired注解,如果需要将第三方库中的组件装配到应用中,就需要采用显示装配方式:JavaConfig装配或xml装配。

JavaConfig装配

User类
Configuration注解表明该类是一个配置类,应该包含在spring应用上下文中如何创建bean的细节。可以将配置类放到单独的包中,使它与业务逻辑分离开。
Bean注解告诉spring,这个方法将会返回一个对象,该对象要注册为sprig应用上下文中的bean。
bean的默认ID与方法名一样,本例中bean的ID为getRole,可以使用@Bean(“reNameRole”)将ID修改为reNameRole。

1
2
3
4
5
6
7
@Configuration
public class User {
@Bean
public Role getRole(){
return new Role();
}
}

Role类

1
2
3
public class Role{

}

xml装配

User类

1
2
3
4
5
6
7
8
9
10
package spring.di.xml.dto;

public class User {

private Role role;

public User(Role role){
this.role = role;
}
}

Role类

1
2
3
4
5
package spring.di.xml.dto;

public class Role{

}

xml配置
这里显示地设置了bean的ID,如果没有明确给定ID,默认的ID将会是spring.di.xml.dto.Role#0。

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

<bean id="role" class="spring.di.xml.dto.Role"/>
<bean id="user" class="spring.di.xml.dto.User" c:role-ref = "role"/>
</beans>

测试类

1
2
3
4
5
6
7
8
9
10
11
12
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application.xml")
public class BeanTest {

@Autowired
private Role role;

@Test
public void roleNotNull(){
assertNotNull(role);
}
}

选择哪种装配方式最合适?

根据项目的具体情况来选择,三种装配方式也可以相互搭配使用。我目前所在项目组使用自动装配与xml装配相结合的方式,《spring in action》作者给出了如下建议:

尽可能地使用自动装配,显示配置越少越好。当必须使用显示配置时(比如,有些源码不是由你来维护,而你需要为这些代码配置bean时),推荐使用类型安全并比xml更加强大的JavaConfig装配。最后,只有当你想要使用便利的xml命名空间,并且在JavaConfig中没有同样的实现时,才应使用xml。

项目中犯过的错误

因为对bean装配以及bean的默认ID理解不清楚,在项目中犯过一个错误。
通过xml配置的方式导入RoleService服务,UserService服务根据ID为”roleService”从上下文获取bean,但是获取的bean为null。
如果对bean装配的三种方式理解清楚的话,很容易知道,通过xml配置的方式装配bean时,默认ID为spring.di.sysconfig.service.RoleService#0,所以通过AppContext.getBean(“roleService”)方式获取的bean必定会为null。
UserService类

1
2
3
4
5
@Component
public class UserService {
private RoleService roleService = AppContext.getBean("roleService");
...
}

xml配置

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

<bean class="spring.di.sysconfig.service.RoleService"/>

</beans>
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×