##1、xml配置文件模板
创建一个applicationContext.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"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
##2、xml中几种注入方式
首先创建三个包,再创建3个类:

UserDAO中:
public class UserDao {
public void insertUser() {
System.out.println("insert success!!");
}
}
UserService中:
public class UserService {
private UserDao userDao;
/* public UserService(UserDao userDao){
this.userDao = userDao;
}*/
public void insertUser() {
userDao.insertUser();
}
public UserDao getUserDao() {
return userDao;
}
@Resource
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void init() {
System.out.println("init");
}
public void destory() {
System.out.println("destory");
}
}
Common类:
public class Common {
private List<Integer> list;
private Map<String, Integer> map;
private Set<Integer> set;
public List<Integer> getList() {
return list;
}
public void setList(List<Integer> list) {
this.list = list;
}
public Map<String, Integer> getMap() {
return map;
}
public void setMap(Map<String, Integer> map) {
this.map = map;
}
public Set<Integer> getSet() {
return set;
}
public void setSet(Set<Integer> set) {
this.set = set;
}
@Override
public String toString(){
System.out.println(list.size()+","+map.size()+","+set.size());
return null;
}
}
xml文件中配置注入可以有四种方式,分别是setter方法注入,构造方法注入,静态工厂注入和实例工厂注入,其中,后面两种注入方法基本用不到,所以这里介绍前面两种注入方式。
setter方法注入:
首先对要注入的对象添加getter、setter方法:
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
然后在xml中添加bean的声明:
<bean id="userDao" class="com.liuyuan.dao.UserDao"/>
<bean id="userService" class="com.liuyuan.service.UserService">
<property name="userDao" ref="userDao"/>
</bean>
最后测试即可:
@Test
public void testDI(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.insertUser();
}
构造方法注入:
首先也要在类中编写构造方法:
public UserService(UserDao userDao){
this.userDao = userDao;
}
然后在xml总进行配置:
<bean id="userService" class="com.liuyuan.service.UserService">
<constructor-arg name="userDao" ref="userDao"/>
</bean>
最后测试即可。
##3、集合的注入
首先也是在类中编写getter、setter方法:
private List<Integer> list;
private Map<String, Integer> map;
private Set<Integer> set;
public List<Integer> getList() {
return list;
}
public void setList(List<Integer> list) {
this.list = list;
}
public Map<String, Integer> getMap() {
return map;
}
public void setMap(Map<String, Integer> map) {
this.map = map;
}
public Set<Integer> getSet() {
return set;
}
public void setSet(Set<Integer> set) {
this.set = set;
}
@Override
public String toString(){
System.out.println(list.size()+","+map.size()+","+set.size());
return null;
}
然后在xml中:
<bean id="common" class="com.liuyuan.pojo.Common">
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<property name="map">
<map>
<entry key="key1" value="12"/>
<entry key ="key2" value="32"/>
</map>
</property>
<property name="set">
<set>
<value>45</value>
<value>32</value>
</set>
</property>
</bean>
最后测试:
@Test
public void testEasyValue(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
Common common = (Common) ctx.getBean("common");
common.toString();
}
##4、scope测试
在spring中,scope指的是单例和多例,当scope值为singleton表示单例,当值为prototype的时候表示多例,默认为单例。
在xml中配置一句:
<bean id="userDao" class="com.liuyuan.dao.UserDao" scope="singleton"/>
然后测试:
@Test
public void testScope(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
UserDao userService = (UserDao) ctx.getBean("userDao");
UserDao userService1 = (UserDao) ctx.getBean("userDao");
System.out.println(userService==userService1);
}
结果为true,当设置为prototype的时候,结果为false。
##5、自动装配
在xml配置一句:
<bean id="userService" class="com.liuyuan.service.UserService" autowire="byType"/>
即可,表示按类型来接收。如果一个类型有两个类,那么将byType改为byName即可。
##6、lifecycle
默认情况下配置在xml中的类会在初始化的容器加载的时候就被实例化,如果配置了init-method值和destroy-method值,那么会在加载和销毁的时候调用对应方法。如果在xml加载时不实例化类,可以配置lazy-init对象。测试:
在类中编写两个方法:
public void init() {
System.out.println("init");
}
public void destory() {
System.out.println("destory");
}
然后在xml中指定:
<bean id="userService" class="com.liuyuan.service.UserService" autowire="byType" init-method="init" destroy-method="destory"/>
最后测试即可:
@Test
public void testLifeCycle(){
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
UserService userService = (UserService) ctx.getBean("userService");
ctx.close();
}
##7、@autowire
之前介绍的全部都是xml文件的配置,这里介绍注解的使用,通常情况下,注解使用起来更加方便。@autowire注解可以写在属性名称上,写在setter方法上,写在构造器上,也可以写在任何可以设置对象的方法上。@autowire默认是通过类型搜索的,如果出现两个实例类型相同的情况,可以使用@Qualifier注解指定名称。
##8、@Resource
@Resource默认是通过名称搜索,当名称找不到是通过类型进行搜索。
@Resource可以放在setter方法,设置属性的方法和字段上,但是不能放在构造方法上。
##9、12.@Component @Service @Controller @Repository
具体差别不大,配合xml中的:
<context:component-scan base-package="com.liuyuan" />
可以将注解声明的类加载到容器中去,可以在括号里面指定名称,不指定的话默认按类的名称的首字母小写的值。
##9、xml和注解的优缺点
xml更加集中,方便管理,和注解更加分散,但是更加简洁。
##10、IOC介绍及意义
IOC指的是控制反转,也可以叫依赖注入。以前我们需要某个对象是自己来创建,通过IOC我们可以直接从容器中获取我们需要的对象而不需要自己创建。IOC的意义如下,方便对类型的生命周期、依赖关系进行统一管理,实现对象之前的解耦。这里实现解耦可以从以下几个方面理解。首先,之前A和B两个对象互相一依赖,如果进行修改,那么需要在源代码中修改,而现在只需要在xml文件中进行修改,方便修改和维护,其次,由自己创建对象变成由容器创建对象,自己只需要有有个引用即可。相当于一个主板上有内存条的接口,需要的时候认为的加上这个内存条和一个主板上本来就有内存条的关系,前面实现了解耦,后面没有实现解耦。最后,依赖对象的生命周期有自己管理变成由容器管理实现了一定的解耦。