Spring使用XML文件配置IOC
spring是一个开发框架
一般Java应用都会用得到spring
spring量大特性IOC,AOP
特点就很强大,能简化开发流程
使用spring第一件事,引包
在pom(service)中引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version> 4.0.0 </version>
</dependency>
控制反转:IOC,是一种设计思想
DI:依赖注入,是实现iOC的一种方法
实现IOC的2中方式:注解、xml配置
1 使用xml配置实现
1.1 spring是用xml做文件配置的,创建一个xml文件在
无论是放在start还是test的resources文件下都行,就是一定要放在resources文件下
创建成功如下图
在start中创建一个包com.longteng.my.dao,在包中创建一个接口MyUserDao,在该接口中创建一个带参String UserName的addUserName方法
public interface MyUserDao {
public void addUserDao(String userName);
}
在包路径my下创建一个实现类的包impl,在该包下创建一个接口实现类
UserDao实现接口MyUserDao,重写addUserDao方法
public class UserDao implements MyUserDao{
@Override
public void addUserDao(String userName){
System.out.println(userName);
}
要对于这个接口实现类做一个测试,选择高接口实现类,右击——Go To——Test——Create New ,就会创建一个该类的接口测试类
创建成功之后Test下就会出现一个该接口实现类的测试类
在xml文件中创建一个bean,bean就相当于new一个object对象
bean中的属性
ID:就相当于给这个bean起一个名字,一般是要实例化的类的类名首字母小写
class:就是要实例化的是哪个类,就是这个类的地址
.xml中的bean就相当于new了一个对象,相当于做了一个实例化
例:
测试类用启动容器的方式创建bean
@Test
public void iocTest(){
//ClassPathXmlApplicationContext
//相当于去运行时环境里找配置文件,在classpath中找配置文件
//配置文件是什么?就是传的参数文件
ApplicationContext context=
new ClassPathXmlApplicationContext("iocs.xml");
//启动容器成功之后就可以在容器中获取bean
//getBean相当于就是new一个UserDao对象
//区别在于用getBean的方式是容器启动时该对象已经存在于容器里
//getBean获取到的是一个object类型,要做强制类型转换
MyUserDao iuserDao=(MyUserDao)context.getBean("userDao");
iuserDao.addUserDao("hello");
}
要使用IOC和AOP要在spring的容器中运行才可以
类中初始化属性用2种方法,1:构造函数 2:set方法
那xml中是如何赋值呢?
有个BookingService 接口
public interface BookingService {
void booking();
}
有好几个类实现了BookingService 接口,并重写了booking方法:MeiTuanService、QunarService、YiLongService.............
有个Boss类,该类里有个BookingService 属性,该属性有构造方法、set方法
public class Boss {
BookingService bookingService;
//set方法初始化
public void setBookingService(BookingService bookingService) {
this.bookingService = bookingService;
}
//构造方法初始化
public Boss(BookingService bookingService){
this.bookingService=bookingService;
}
}
那么xml文件里的要怎么写呢?
<bean id="bookingService" class="com.longteng.lesson2.booking.MeiTuanService">
</bean>
<bean id="boss" class="com.longteng.lesson2.booking.Boss">
<!--相当于构造方法,name是Boos类里的属性名,-->
<!--ref是引用,就是哪个类型的,就是上面MeiTuanService的id,因为MeiTuanService-->
<!--实现了BookingService类-->
<constructor-arg name="bookingService" ref="bookingService"></constructor-arg>
<!--相当于set方法,于上面的解释同理-->
<property name="bookingService" ref="bookingService"></property>
</bean>
在测试类中,要测试多个测试用例,不用每个测试都启动一次容器,可以将所有的bean放在一个容器中托管,前提是所有的bean都在一个xml文件里,用testng的BeforeClass方法,这样执行整个测试类时就用一个容器即可
BeforeClass无论写在类里的哪个位置,都会最先执行,AfterClass无论写在类里的哪个位置,都会最后执行,就是BeforeClass是做初始化操作,AfterClass是做资源回收操作
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
public class Test {
ApplicationContext context;
@BeforeClass
public void before(){
context=new ClassPathXmlApplicationContext("iocs.xml");
System.out.println("初始化操作开始了");
}
@org.testng.annotations.Test
public void test(){
System.out.println("执行业务测试");
}
@AfterClass
public void after(){
System.out.println("资源回收了");
}
}
如果不用注解,那么在xml文件里怎么做初始化、资源回收
init-method方法在容器启动时执行
destroy-method方法在容器关闭时执行
在xml文件里实现,init-method的值before就是方法名,destroy-method的值after也是方法名,这两个方法写在哪个类里,那么class的路径就是哪个类
<bean id="boss" class="com.longteng.lesson2.booking.Boss"
init-method="before" destroy-method="after">
</bean>
xml文件中给构造方法,set方法赋值
public class UserDao implements MyUserDao {
String city;
String address;
public String getCity() {
return city;
}
//要加上无参的构造方法
UserDao(){}
UserDao(String address){
this.address=address;
}
}
xml文件中,constructor-arg设置构造方法,直接写value值
property 设置set方法,直接写value值,这样就为上面的构造方法、set方法设置值成功了
<bean id="userDao" class="com.longteng.lesson2.my.impl.UserDao">
<constructor-arg name="address" value="铜锣湾"></constructor-arg>
<property name="city" value="HongKong"></property>
</bean>
用xml文件赋值成功之后,要怎么将这个值放入map里?
Map<String,String> map = new HashMap();
map.put("userName", "aa");
map.put("address",this.getAddress());
map.put("city",this.getCity());
给map中放值得时候用this.get方法