spring 註解裝配bean (裝配bean 二)
阿新 • • 發佈:2018-12-18
註解裝配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();
自動裝配失敗
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);
}
}
測試通過
@Primary 按名稱選擇,呼叫方自動選擇
@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=張三}