注解装配bean
最简单的例子
@Component("role")
public class Role {
@Value("1")
private Long id;
@Value("zhangsan")
private String roleName;
@Value("hehe")
private String note;
}
如果Component未显示给出名称,则去类名首字母小写为默认名称
获取ioc容器获取bean入口
未配置,扫码当前默认包
package priv.dengjl.spring.day1;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
public class PojoConfig {
}
配置扫码指定包,多个逗号隔开
@ComponentScan(basePackages = {"priv.dengjl.spring.day1.service"})
public class ApplicationConfig {
}
配置扫码指定类,多个逗号隔开
@ComponentScan(basePackageClasses = {Role.class})
public class ApplicationConfig {
}
注解方式调用
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
Role role = context.getBean(Role.class);
System.out.prinln(role);
自动装配,Autowired
装配类属性成员
public interface RoleService2 {
void printRole();
}
@Component("roleService2")
public class RoleServiceImpl2 implements RoleService2{
@Autowired
private Role role = null;
@Override
public void printRole() {
System.out.println(role);
}
}
测试例子
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
RoleService2 service = context.getBean(RoleServiceImpl2.class);
service.printRole();
priv.dengjl.spring.day1.Role@12405818
自动装配失败
Autowired表示必须装配成功
@Component("roleService4")
public class RoleServiceImpl4 implements RoleService2{
@Autowired
private Map map = null;
@Override
public void printRole() {
System.out.println(map);
}
}
测试例子
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
RoleService2 service = context.getBean(RoleServiceImpl4.class);
service.printRole();
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'roleService4': Unsatisfied dependency expressed through field 'map'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'java.util.Map<?, ?>' available: expected single matching bean but found 2: systemProperties,systemEnvironment
Autowired(required=false)
将required属性置为false,表示注入属性可以为空
@Component("roleService4")
public class RoleServiceImpl4 implements RoleService2{
@Autowired(required=false)
private Map map = null;
@Override
public void printRole() {
System.out.println(map);
}
}
测试通过
自动注入Autowired,子类过多歧义性
--RoleService2
--RoleServiceImpl2
--RoleServiceImpl4
定义类RoleController
@Component
public class RoleController {
@Autowired
private RoleService2 service;
public void printRole() {
service.printRole();
}
}
测试文件
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
RoleController controller = context.getBean(RoleController.class);
controller.printRole();
测试结果
Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'roleController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'priv.dengjl.spring.day1.service.RoleService2' available: expected single matching bean but found 2: roleService2,roleService4
@Primary标记为默认首选,组件自身设置
@Component("roleService2")
@Primary
public class RoleServiceImpl2 implements RoleService2{
@Autowired
private Role role = null;
@Override
public void printRole() {
System.out.println(role);
}
}
测试通过
@Qualifier按名称选择,调用方自动选择
@Component
public class RoleController {
@Autowired
@Qualifier("roleService2")
private RoleService2 service;
public void printRole() {
service.printRole();
}
}
测试通过
装配构造方法
@Component
public class RoleController2 {
private RoleService2 service;
public RoleController2(@Autowired @Qualifier("roleService2") RoleService2 service) {
this.service = service;
}
public void printRole() {
service.printRole();
}
}
测试通过
为第三方jar包扩展
由于注解@Component无法注释到jar等地方包,可以通过bean这种方式转换,达到扩展目的
将第三方代码写在方法中,通过方法生成一个Bean对象
@Component
public class PersonData {
// 由于注解@Component无法注释到jar等地方包,可以通过bean这种方式转换,达到扩展目的
@Bean(name = "cust")
public Map getMap() {
Map temp = new HashMap();
temp.put("name", "张三");
temp.put("sex", "1");
return temp;
}
}
注入bean
@Component
public class Person {
@Autowired(required = true)
@Qualifier("cust")
private Map map;
@Override
public String toString() {
return map.toString();
}
}
测试
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
Person person = context.getBean(Person.class);
System.out.println(person);
{sex=1, name=张三}