@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student{private String name;private Address address;private String[] books;private List hobbys;private Map card;private Set games;private String wife;private Properties info;
}
红楼梦 西游记 水浒传 看书 听歌 打球
红楼梦 西游记 水浒传 20190525 男/prop>root 123456
1. 先在beans框架中加入支持:xmlns:p="http://www/springframework.org/schema/p"
1. 先在beans框架中加入支持:xmlns:c="http://www/springframework.org/schema/c"
2. 其次使用c注入的Bean必须存在有参构造器
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user1 = context.getBean("user");
User user2 = context.getBean("user");
System.out.println(user1==user2); //true
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user1 = context.getBean("user");
User user2 = context.getBean("user");
System.out.println(user1==user2); //false
6.1. 别名
6.2 Import(一般用于团队开发使用,他可以将多个配置文件,导入合并为一个)
注意:如果导入的文件中,bean重名了,那么就会把重名的bean合并成一个,所以不会因为不同的bean.xml存在重名而发生冲突
/*在Spring容器中创建一个bean
*/
@Component
public class OrderService{
}
/*在Spring容器中创建一个bean
*/
@Configuration
@ComponentScan
public class AppConfig{@Beanpublic OrderService orderService1(){return new OrderService();}
}
@Component
public class User {private String name;public String getName() {return name;}@Value("黑心白莲") //属性注入值public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +'}';}
}
// @Configuration代表这是一个配置类,就和我们之前看的beans.xml
@Configuration
@ComponentScan
public class KuangConfig {/* @bean == bean标签方法名字 == bean标签中id属性方法返回值 == bean标签中的class属性*/@Beanpublic User user(){return new User(); }
}
public class MyTest {public static void main(String[] args) {//如果完全使用了配置类方式去做,我们就只能通过 AnnotationConfig 上下文来获取容器,通过配置类的class对象加载!ApplicationContext context = new AnnotationConfigApplicationContext(KuangConfig.class);User user = context.getBean("user", User.class);System.out.println(user.getName());}
}
byName: 会自动在容器上下文查找,和自己对象set方法后面的值对应的bean id(通过id匹配)
//实体类
@Data
public class Pepole {private String name;private Books books;private Hobbies hobbies;
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean(通过class匹配)
注意:使用autowire byType首先需要保证:同一类型的bean对象,在spring容器中唯一。如果不唯一,会报不唯一的异常。
注解开发注意事项
@Autowied装配方式
按类型装配(默认使用的装配方式)。
按名称装配(结合@Qualifier注解使用)。
public class Pepole {private String name;
//1. 通过名字装配@Autowired@Qualifier("books1")private Books books;
//2. 通过类型装配@Autowiredprivate Hobbies hobbies;
}
@Resource装配方式
@Resource指定按type自动装配
@Resource(type = Books.class)private Books books;
@Resource指定按name自动装配:
@Resource(name = "books")private Books books;
@Autowired与@Resource区别



这段代码中@mapper,实际上执行了Spring创建Bean对象并放入Spring容器的过程
@mapper
public class UserService{@Autowiredprivate OrderService orderService;public void test(){System.out.println(orderService);}
}
- UserService实例化(无参构造方法),生成普通对象
- 对普通对象依赖注入- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -【根据@Autowired判断是否对属性注入依赖】
- 初始化- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -【执行所有方法】
- 初始化后(AOP),生成代理对象 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -【切面编程】
- 将普通对象或者代理对象放入,Map
(单例池) -【即注册Bean对象】
这里的UserService实例化,可不是说让你自己在main()方法里面手动做一个UserService userService的实例化。而是说你在public class UserService“头顶”上加了注解@Mapper的时候,Spring“背地里”进行了第一步:将UserService实例化了
- 在UserService对象中有一个属性OrderService,Spring也对该属性进行了实例化OrderService orderService。但是实例化出来的普通对象orderService是没有值的,通过@Autowired的依赖注入,向普通对象orderService注入,普通对象orderService就有了值,
//初始化,执行UserService中的所有方法,全都成功后才能将Bean对象创建并放入Map
if (userService instanceof InitializingBean){((InitializingBean) userService).afterPropertiesSet();
}
为什么要执行初始化这个步骤呢?换句话说,为什么要执行所有方法呢?
//1. 通过执行方法来为创建的实例的属性admin注入值为xxx
@Override
public void afterPropertiesSet() throws Exception{this.admin = "xxx";
}//2. 通过执行方法来检验创建的实例是否满足
public void afterPropertiesSet() throws Exception{if(sqlSession == null){throw new NullPointerException();}
}/*
3. 个人项目经历:每个属性都要get/set方法,若是缺失了某个属性的get/set方法,
Spring启动就会报错,这也说明了初始化执行了Bean对象的所有方法
*/
AOP就是对注入依赖后的普通对象进行切面编程,从而生成代理对象。至于什么是切面编程AOP,读者就自行搜索一下吧,因为我也只是一知半解。
- 我们先看到回到上面Spring创建Bean对象的过程,会发现有普通对象和代理对象。两者的区别,大家可以认为是普通对象经过了“加工”,变成了更多功能的代理对象。
- 现在我们要将普通对象或者代理对象放入Map并将代理对象放入Map
中。 - 如果没有进行AOP生成代理对象,那么这时候就是将普通对象放入Map中,普通对象成为Bean对象;如果进行了AOP生成代理对象,那么这时候就是将代理对象放入Map中,代理对象成为Bean对象;
- 将普通对象或者代理对象放入Map这个动作完成了,才是真正意义上生成了Bean对象,也就是注册了Bean
@Service
public class UserService{private OrderService orderService;public void test(){ //初始化会自动执行全部方法,所以test方法会被自动执行System.out.println(orderService);}
}
执行UserService userService = (UserService) applicationContext.getBean(“UserService”);
输出:null
@Service
public class UserService{private OrderService orderService;public UserService(OrderService orderService){this.orderService = orderService;System.out.println(1);}public void test(){System.out.println(orderService);}
}
执行UserService userService = (UserService) applicationContext.getBean(“UserService”);
输出:1、#{orderService指针编号}
@Service
public class UserService{private OrderService orderService;public UserService(){System.out.println(0);}public UserService(OrderService orderService){this.orderService = orderService;System.out.println(1);}public void test(){System.out.println(orderService);}
}
执行UserService userService = (UserService) applicationContext.getBean(“UserService”);
输出:0、null
@Service
public class UserService{private OrderService orderService;public UserService(OrderService orderService1,OrderService orderService2){this.orderService = orderService;System.out.println(2);}public void test(){System.out.println(orderService);}
}
执行UserService userService = (UserService) applicationContext.getBean(“UserService”);
输出:2、#{orderService指针编号}
@Service
public class UserService{private OrderService orderService;public UserService(OrderService orderService){this.orderService = orderService;System.out.println(1);}public UserService(OrderService orderService1,OrderService orderService2){this.orderService = orderService;System.out.println(2);}public void test(){System.out.println(orderService);}
}
执行UserService userService = (UserService) applicationContext.getBean(“UserService”);
报错!Caused by:com.zhouyu.service.UserService.< init >()
总结
- 如果没有重写构造方法,那么创建Bean对象就会用无参构造方法;
- 如果重写了一个有参构造方法,那么创建Bean对象就会用该有参构造方法;
- 如果重写了两个有参构造方法,那么创建Bean对象不知道用哪个有参构造方法,就会去寻找无参构造方法,如果没有无参构造方法就报错。如果存在无参构造方法就执行无参构造方法。
@Service
public class UserService{private OrderService orderService;@Autowired //指定使用该构造方法,即使存在无参构造器,也要使用这个有参构造器public UserService(OrderService orderService){this.orderService = orderService;System.out.println(1);}public UserService(OrderService orderService1,OrderService orderService2){this.orderService = orderService;System.out.println(2);}public void test(){System.out.println(orderService);}
}
执行UserService userService = (UserService) applicationContext.getBean(“UserService”);
输出:1、#{orderService指针编号}
- 当我们创建Bean对象userService时,需要用到有参构造方法,Spring就会在Map中寻找Bean对象OrderService,并且注入到构造方法的参数orderService中。
- 如果将@Component去掉,那么OrderService就无法成为Bean对象,那么Spring也拿不出Bean对象来传给UserService的有参构造方法的参数orderService,那么orderService就为空。但是Spring规定了不能向有参构造方法的参数传null。所以这种情况运行就会出现报错:No qualifying bean of type ‘com.zhouyu.service.OrderService’
- 如果通过参数的类OrderService找出beanName唯一,那就直接将Bean对象注入orderService。
- 如果通过参数的类OrderService找出beanName不唯一,那就通过参数名orderService找出唯一的beanName,然后将Bean对象注入orderService参数
- 如果这样都找不到,那就报错!
- 如下这三个orderService,orderService1,orderService2分别是可选的beanName,将构造器public UserService(OrderService orderService)的参数orderService改成上述之一即可不报错:expected single matching bean but found 3:orderService,orderService1,orderService2
//1. 注册了一个类型为OrderService,名字为orderService的Bean
public class OrderService{
}//2. 注册了一个类型为OrderService,名字为orderService1的Bean
public class AppConfig{@Beanpublic OrderService orderService1(){}
}
@Component
public class OrderService{private UserService userService;public OrderService(UserService userService){this.userService = userService;}
}
@Service
public class UserService{private OrderService orderService;public UserService(OrderService orderService){this.orderService = orderService;}
}
报错:Is there an unresolvable circular reference?
我们来看一下过程:当我要将UserService创建为Bean对象时,那么就需要传入OrderService的Bean对象给orderService。那么OrderService要提前被Spring创建为Bean对象是不是?那我们看看OrderService要提前被Spring创建为Bean对象也是用到有参构造器,需要传入UserService的Bean对象,那么UserService要提前被Spring创建为Bean对象…这样就陷入了循环了(circular reference)
/*在Spring容器中创建一个bean*/
@Configuration
@ComponentScan
public class AppConfig{@Beanpublic OrderService orderService1(){return new OrderService();}@Beanpublic OrderService orderService2(){return new OrderService();}}
public class UserService{//1. 属性注入,先根据OrderService类名,再根据orderService1参数名寻找BeanName@Autowiredprivate OrderService orderService1;//2. set方法注入,先根据OrderService类名,再根据orderService参数名寻找BeanName@Autowiredpublic void setOrderService(OrderService orderService1){this.orderService1 = orderService1;}//3. 有参构造器方法注入,先根据OrderService类名,再根据orderService参数名寻找BeanNamepublic UserService(OrderService orderService1){this.orderService = orderService1;}
}