创建对象的方法,比如已知对象OrderService,可以通过以下方式创建对象
1、new OrderService()
2、通过反射创建对象(本次不讲)
3、通过spring 的IOC机制
3.1 利用无参构造函数+setter方法注入值
3.2 利用有参构造函数直接注入
-----------------------------方法1 利用有参构造函数直接注入---------------------
1、创建IOrderDao
package com.longteng.lesson2.dao;
public interface IOrderDao {
public String bookOrder(String name);
}
2、创建IOrderDao的实体 OrderDao
package com.longteng.lesson2.dao.impl; import com.longteng.lesson2.dao.IOrderDao; public class OrderDao implements IOrderDao { @Override public String bookOrder(String name) { return name+"预订了订单"; } }
3、创建OrderService
package com.longteng.lesson2.service; import com.longteng.lesson2.dao.IOrderDao; public class OrderService { private String orderSkuName; public OrderService(String orderSkuName){ this.orderSkuName = orderSkuName; } public void setOrderSkuName(String orderSkuName) { this.orderSkuName = orderSkuName; } public String getOrderSkuName() { return orderSkuName; } public void setiOrderDao(IOrderDao iOrderDao) { this.iOrderDao = iOrderDao; } IOrderDao iOrderDao; public String getOrderInfo(String name){ return iOrderDao.bookOrder(name); } }
4、 创建order.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <bean id="orderDao" class="com.longteng.lesson2.dao.impl.OrderDao"></bean> <bean id="orderService" class="com.longteng.lesson2.service.OrderService"> <constructor-arg name="orderSkuName" value="123456789"></constructor-arg> <property name="iOrderDao" ref="orderDao"></property> </bean> </beans>
5、创建测试类
package com.longteng.lesson2.dao; import com.longteng.lesson2.service.OrderService; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class OrderDaoTest { ApplicationContext context; @Before public void initApplication(){ context = new ClassPathXmlApplicationContext("order.xml"); } @Test public void test(){ OrderService orderService = context.getBean("orderService", OrderService.class); System.out.println(orderService.getOrderInfo("zhou")); System.out.println(orderService.getOrderSkuName()); Assert.assertEquals("成功了","zhou预订了订单",orderService.getOrderInfo("zhou")); } }
6、测试结果
............
13:33:21.286 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor' 13:33:21.288 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source 13:33:21.290 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'orderService' zhou预订了订单 123456789 Process finished with exit code 0
----------------------------方法2 利用注解创建对象------------------------------------
1、创建IOrderDao
package com.longteng.lesson2.dao; public interface IOrderDao { public String bookOrder(String name); }
2、创建IOrderDao的实体
package com.longteng.lesson2.dao.impl; import com.longteng.lesson2.dao.IOrderDao; import org.springframework.stereotype.Repository; @Repository public class OrderDao implements IOrderDao { @Override public String bookOrder(String name) { return name+"预订了订单"; } }
3、创建OrderService
package com.longteng.lesson2.service; import com.longteng.lesson2.dao.IOrderDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class OrderService { @Autowired IOrderDao iOrderDao; public String getOrderInfo(String name){ return iOrderDao.bookOrder(name); } }
4、 创建order.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.longteng.lesson2.dao,com.longteng.lesson2.service"/> </beans>
5、创建测试类
package com.longteng.lesson2.dao; import com.longteng.lesson2.service.OrderService; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class OrderDaoTest { ApplicationContext context; @Before public void initApplication(){ context = new ClassPathXmlApplicationContext("order.xml"); } @Test public void test(){ OrderService orderService = context.getBean("orderService", OrderService.class); System.out.println(orderService.getOrderInfo("zhou")); Assert.assertEquals("成功了","zhou预订了订单",orderService.getOrderInfo("zhou")); } }
---------------------------方法3 零配置生成对象-----------------------
1、创建IOrderDao
package com.longteng.lesson2.dao;
public interface IOrderDao {
public String bookOrder(String name);
}
2、创建IOrderDao的实体
package com.longteng.lesson2.dao.impl;
import com.longteng.lesson2.dao.IOrderDao;
import org.springframework.stereotype.Repository;
@Repository
public class OrderDao implements IOrderDao {
@Override
public String bookOrder(String name) {
return name+"预订了订单";
}
}
3、创建OrderService
package com.longteng.lesson2.service;
import com.longteng.lesson2.dao.IOrderDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
@Autowired
IOrderDao iOrderDao;
public String getOrderInfo(String name){
return iOrderDao.bookOrder(name);
}
}
4 创建OrderService的配置类
package com.longteng.lesson2.config; import com.longteng.lesson2.service.OrderService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration//此处功能类似于在 order.xml编写<bean id="" class="xxxx"/> @ComponentScan(basePackages = {"com.longteng.lesson2.dao","com.longteng.lesson2.service"}) public class OrderCfg { @Bean public OrderService getOrderService(){ return new OrderService(); } }
5 创建测试类
package com.longteng.lesson2.dao; import com.longteng.lesson2.config.OrderCfg; import com.longteng.lesson2.service.OrderService; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class OrderDaoTest { ApplicationContext context; @Before public void initApplication(){ context = new AnnotationConfigApplicationContext(OrderCfg.class); } @Test public void test(){ OrderService orderService = context.getBean("orderService", OrderService.class); System.out.println(orderService.getOrderInfo("zhou")); Assert.assertEquals("成功了","zhou预订了订单",orderService.getOrderInfo("zhou")); } }