1、通过IOC容器创建对象,并为属性赋值
在IOC容器本身对象创建时(xml文件加载时),会将配置文件中配置好的bean先创建出来,按照xml文件中配置的先后顺序创建
1 <bean id="user1" class="com.neuedu.springfirst.bean.User" >
2 <property name="username" value="张三"></property>
3 <property name="password" value="123456"></property>
4 <property name="email" value="12345@qq.com"></property>
5 </bean>
测试方法:
1 @Test
2 public void test01() {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 Object user = ioc.getBean("user1");
5 System.out.println(user);
6 }
2、根据bean的类型从IOC容器中获取bean的实例★【要求:IOC容器同种类型只存在一个】
1 @Test
2 public void test02() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 User user = ioc.getBean(User.class);
5 System.out.println(user);
6 }
3-4、通过构造器为bean的属性赋值 ,通过index属性指定参数的位置
1 <bean id="book1" class="com.neuedu.springfirst.bean.Book">
2 <constructor-arg index="0" value="二月"></constructor-arg>
3 <constructor-arg index="1" value="张三"></constructor-arg>
4 </bean>
测试代码:
1 @Test
2 public void test03() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 Object book = ioc.getBean("book1");
5 System.out.println(book);
6 }
5、通过类型不同区分重载的构造器
1 <bean id="book2" class="com.neuedu.springfirst.bean.Book">
2 <constructor-arg type="String" value="二月"></constructor-arg>
3 <constructor-arg type="String" value="邹梦洁"></constructor-arg>
4 </bean>
5 <bean id="book3" class="com.neuedu.springfirst.bean.Book">
6 <constructor-arg type="String" value="阿勒股"></constructor-arg>
7 <constructor-arg type="Double" value="123"></constructor-arg>
8 </bean>
测试代码:
1 @Test
2 public void test05() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 Object book = ioc.getBean("book2");
5 System.out.println(book);
6 }
6、通过p名称空间为bean赋值
1 <bean id="book4" class="com.neuedu.springfirst.bean.Book"
2 p:bookName="阿克"
3 p:author="张三" />
测试代码:
1 @Test
2 public void test06() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 Object book = ioc.getBean("book4");
5 System.out.println(book);
6 }
7、测试使用null值
1 <bean id="book00" class="com.neuedu.springfirst.bean.Book">
2 <property name="bookName"><null/></property>
3 </bean>
测试代码:
1 @Test
2 public void test07() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 Object book = ioc.getBean("book00");
5 System.out.println(book);
6 }
8、引用其他bean
1 <bean id="bookshop" class="com.neuedu.springfirst.bean.BookShop">
2 <property name="kind" value="小说"></property>
3 <property name="book" ref="book1"></property>
4 </bean>
测试代码:
1 @Test
2 public void test08() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 Object bookshop = ioc.getBean("bookshop");
5 System.out.println(bookshop);
6 }
9、引用内部bean
1 <bean id="bookshop1" class="com.neuedu.springfirst.bean.BookShop">
2 <property name="kind" value="小说"></property>
3 <property name="book">
4 <bean class="com.neuedu.springfirst.bean.Book">
5 <property name="bookName" value="私发给"></property>
6 <property name="author" value="邹梦洁"></property>
7 </bean>
8 </property>
9 </bean>
测试代码:
1 @Test
2 public void test09() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 Object bookshop = ioc.getBean("bookshop1");
5 System.out.println(bookshop);
6 }
10、使用List类型的集合属性
1 <bean id="bookshop2" class="com.neuedu.springfirst.bean.BookShop">
2 <property name="kind" value="小说"></property>
3 <property name="bookList">
4 <list>
5 <ref bean="book1" />
6 <ref bean="book2" />
7 <ref bean="book3" />
8 </list>
9 </property>
10 </bean>
测试代码:
1 @Test
2 public void test10() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 BookShop bookshop = (BookShop) ioc.getBean("bookshop2");
5 for (Book book : bookshop.getBookList()) {
6 System.out.println(book);
7 }
8 }
11、使用Map类型的集合属性
1 <bean id="bookshop3" class="com.neuedu.springfirst.bean.BookShop">
2 <property name="kind" value="小说"></property>
3 <property name="bookMap">
4 <map>
5 <entry>
6 <key>
7 <value>book1</value>
8 </key>
9 <ref bean="book1" />
10 </entry>
11 <entry>
12 <key>
13 <value>book2</value>
14 </key>
15 <ref bean="book2" />
16 </entry>
17 <entry>
18 <key>
19 <value>book3</value>
20 </key>
21 <ref bean="book3" />
22 </entry>
23 </map>
24 </property>
25 </bean>
测试代码:
1 @Test
2 public void test11() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 BookShop bookshop = (BookShop) ioc.getBean("bookshop3");
5 Map<String, Book> bookMap = bookshop.getBookMap();
6 Set<String> nameSet=bookMap.keySet();
7 for (String name : nameSet) {
8 System.out.println(bookshop.getBookMap().get(name));
9 }
10 }
12、使用prop子元素为Properties类型的属性赋值
1 <bean id="bookshop4" class="com.neuedu.springfirst.bean.BookShop">
2 <property name="kind" value="历史"></property>
3 <property name="prop">
4 <props>
5 <prop key="name1">book1</prop>
6 <prop key="name2">book2</prop>
7 <prop key="name3">book3</prop>
8 </props>
9 </property>
10 </bean>
测试代码:
1 @Test
2 public void test12() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 BookShop bookshop = (BookShop) ioc.getBean("bookshop4");
5 Properties prop=bookshop.getProp();
6 System.out.println(prop.getProperty("name1"));
7 }
14、给bean的级联属性赋值
1 <bean id="book5" class="com.neuedu.springfirst.bean.Book"></bean>
2 <bean id="bookshop5" class="com.neuedu.springfirst.bean.BookShop">
3 <property name="book" ref="book5"></property>
4 <property name="book.bookName" value="熊清华"></property>
5 <property name="book.author" value="爱疯了"></property>
6 <property name="book.price" value="45"></property>
7 </bean>
测试代码:
1 @Test
2 public void test14() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 BookShop bookshop = (BookShop) ioc.getBean("bookshop5");
5 System.out.println(bookshop.getBook());
6 }
15、配置通过静态工厂方法创建的bean[通过静态方法提供实例对象,工厂类本身不需要实例化!
1 <bean id="book6" class="com.neuedu.springfirst.bean.StaticFactory" factory-method="getBook">
2 <constructor-arg value="book02"/>
3 </bean>
测试代码:
1 @Test
2 public void test15() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 Object book = ioc.getBean("book6");
5 System.out.println(book);
6 }
16、配置通过实例工厂方法创建的bean[通过实例方法提供实例对象,工厂类本身需要先创建对象!
1 <bean id="instanceFactory" class="com.neuedu.springfirst.bean.InstanceFactory"></bean>
2 <bean id="book7" class="com.neuedu.springfirst.bean.Book" factory-bean="instanceFactory" factory-method="getBook">
3 <constructor-arg value="book01"/>
4 </bean>
测试代码:
1 @Test
2 public void test16() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 Object book = ioc.getBean("book7");
5 System.out.println(book);
6 }
18、通过继承实现bean配置信息的重用
1 <bean id="user" class="com.neuedu.springfirst.bean.User">
2 <property name="id" value="1"></property>
3 <property name="username" value="杨涛"></property>
4 <property name="password" value="123456"></property>
5 <property name="email" value="12345@qq.com"></property>
6 </bean>
7 <bean id="user2" class="com.neuedu.springfirst.bean.User" parent="user">
8 <property name="password" value="654321"></property>
9 <property name="email" value="54321@qq.com"></property>
10 </bean>
测试代码:
1 @Test
2 public void test18() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 Object user = ioc.getBean("user2");
5 System.out.println(user);
6 }
19、通过abstract属性创建一个模板bean
1 <bean id="userTemplate" class="com.neuedu.springfirst.bean.User" abstract="true">
2 <property name="id" value="1"></property>
3 <property name="username" value="杨涛"></property>
4 <property name="password" value="123456"></property>
5 <property name="email" value="12345@qq.com"></property>
6 </bean>
7 <bean id="user3" class="com.neuedu.springfirst.bean.User" parent="userTemplate">
8 </bean>
测试代码:
1 @Test
2 public void test19() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 //userTemplate为抽象bean,只能被继承,不能被创建
5 //Object user = ioc.getBean("userTemplate");
6 Object user = ioc.getBean("user3");
7 System.out.println(user);
8 }
20、bean之间的依赖 depends-on="order"被依赖的对象会先创建
1 <bean id="user4" class="com.neuedu.springfirst.bean.User" depends-on="book8"></bean>
2 <bean id="book8" class="com.neuedu.springfirst.bean.Book"></bean>
测试代码:
1 @Test
2 public void test20() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 //执行test20测试方法前,需先将ApplicationContext.xml文件中之前的代码全部删除
5 //由于之前创建的对象会显示创建信息,
6 }
21、分别创建单实例和多实例的bean
单实例:
1 <bean id="user1" class="com.neuedu.spring.bean.User" scope="singleton">
2 <property name="id" value="1"></property>
3 <property name="username" value="张三"></property>
4 <property name="password" value="123456"></property>
5 <property name="email" value="123456@qq.com"></property>
6 </bean>
测试代码:
1 @Test
2 public void test21() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 System.out.println("=========");
5 User user1 = ioc.getBean(User.class);
6 User user2 = ioc.getBean(User.class);
7 System.out.println(user1==user2);
8 System.out.println(user1);
9 System.out.println(user2);
10 }
测试结果:是同一个对象,bean对象的创建默认是在ApplicationContext.xml文件加载时创建
多实例:
1 <bean id="user1" class="com.neuedu.spring.bean.User" scope="prototype">
2 <property name="id" value="1"></property>
3 <property name="username" value="张三"></property>
4 <property name="password" value="123456"></property>
5 <property name="email" value="123456@qq.com"></property>
6 </bean>
测试代码同上:
测试结果:不是一个对象,并且scope属性值为prototype的bean对象,将在获取bean对象的时候创建
注意:
①IOC容器本身对象创建时,会将配置文件中配置好的bean先创建出来
②默认是单实例的,只创建bean的一个对象
③如果设置bean的scope属性为prototype,那么创建bean的对象就是多实例的,在获取的时候创建,每次获取对象都会创建新的
④.从IOC容器中获取对象
①根据bean的id获取
②根据bean的类型获取:要求容器中指定类型的bean是唯一的
22、创建带有生命周期方法的bean
先创建含有生命周期相关的方法的类
1 public class LifeObject {
2 public LifeObject(){
3 System.out.println("LifeObject对象创建了");
4 }
5
6 public void initMethod(){
7 System.out.println("init方法执行了");
8 }
9 public void destoryMethod(){
10 System.out.println("destory方法执行了");
11 }
12 }
配置生命周期相关方法
1 <bean id="lifeObject" class="com.neuedu.spring.bean.LifeObject" init-method="initMethod" destroy-method="destoryMethod">
2 </bean>
测试代码:
1 @Test
2 public void test22() throws Exception {
3 ApplicationContext ioc=new ClassPathXmlApplicationContext("ApplicationContext.xml");
4 Object object = ioc.getBean("lifeObject");
5 System.out.println(object);
6 ConfigurableApplicationContext ioc1=(ConfigurableApplicationContext) ioc;
7 ioc1.close();
8 }
测试结果:
22[补充]:测试bean的后置处理器
bean的后置处理器将对所有在.xml文件加载时创建的bean对象执行处理器中的方法,bean的后置处理器实现了BeanPostProcessor接口,
可重写接口中的postProcessBeforeInitialization方法和postProcessAfterInitialization方法,分别在bean对象初始化之前和之后进行相应的操作
注:若bean对象scope属性值为prototype时,表明该bean为多实例,bean对象在获取对象时才创建,因此后置处理器不起作用
1 <bean id="postProcessor" class="com.neuedu.spring.bean.MyBeanPostProcessor"></bean>
2 <bean id="lifeObject" class="com.neuedu.spring.bean.LifeObject" init-method="initMethod" destroy-method="destoryMethod">
3 </bean>
测试代码:
1 @Test
2 public void test22_1() throws Exception {
3 //测试方法不需要写任何代码,
4 //因为ApplicationContext.xml文件加载时就创建了bean对象
5 }
测试结果: