1. xml应用模式
项目构建
1.1在idea开发工具中创建maven项目,选择模板为webapp,模块名称为springIoc
1.2在pom文件中引入spring-context和junit
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
1.3在src的main目录下新建java和resource目录,并在src目录下新建test目录
1.4将java目录转换为 source root,resources转换为resources root,test目录转换为Test Source Root(右键目录->Mark Directory as)
1.5在java目录下新建com.yf.cn包,然后在该包下新建dao包和service包,然后新建userDao接口以及实现了、UserService接口以及实现类作为测试类
UserDao:
package com.yf.cn.dao;
public interface UserDao {
public void sayMsg(String msg);
}
UserDaoImpl:
package com.yf.cn.dao.impl;
import com.yf.cn.dao.UserDao;
import org.springframework.stereotype.Component;
public class UserDaoImpl implements UserDao {
@Override
public void sayMsg(String msg) {
System.out.println("你好啊"+msg);
}
}
UserService:
package com.yf.cn.service;
public interface UserService {
public void say(String msg);
}
UserServiceImpl:
package com.yf.cn.service.impl;
import com.yf.cn.dao.UserDao;
import com.yf.cn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
public class UserServiceImpl implements UserService {
private UserDao userDao;
@Override
public void say(String msg) {
userDao.sayMsg(msg);
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
1.6在resources目录下新建application.xml
1.7将类交给spring容器管理,在application.xml中进行配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byType">
<bean id="userService" class="com.yf.cn.service.impl.UserServiceImpl">
<property name="userDao" ref="userDaoImpl"/>
</bean>-
<bean id="userDao" class="com.yf.cn.dao.impl.UserDaoImpl"></bean>
</beans>
1.8创建测试类进行测试,执行testRunMethod方法能够看到控制台输出的内容,说明springIoc xml应用已经配置完成
package com.yf.cn;
import com.yf.cn.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpringXml {
ApplicationContext context;
@Before
public void setup(){
/**
* spring xml配置应用程序类
*/
context = new ClassPathXmlApplicationContext("classpath:application.xml");
}
@Test
public void testRunMethod(){
UserService userService = applicationContext.getBean(UserService.class);
userService.say("小辣椒");
}
}
2. 基于注解的应用模式
2.1将application.xml中的UserDaoImpl和UserServiceImpl配置去掉,在代码中通过注解方式配置,在UserDaoImpl类中添加@Repository注解,在UserServiceImpl中添加@Service,并在UserDao属性上添加@Autowired注解
2.2在application.xml中开启注解扫描和包扫描
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-autowire="byType">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.yf.cn"/>
</beans>
说明:在spring4.0后context:component-scan标签同时具备包扫描和注解扫描功能,所以context:annotation-config添加也可,去掉也可
2.3继续测试TestSpringXml 类中的testRunMethod方法,发现也可以正常输出,说明基于注解的方式配置完成
3. 基于javaconfig应用模式
3.1将resources目录下的application.xml删除,或者修改后缀
3.2在java目录的com.yf.cn包下面新建App类,并进行配置
App.class:
package com.yf.cn;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/**
* @Configuration 添加此注解知名该类是一个xml配置类
*/
@Configuration
@ComponentScan("com.yf.cn")
//@ImportResource("classpath:application.xml")
public class App {
}
重点说明:在上面App类中添加两个注解:@Configuration标识该类是一个xml配置类,另外通过注解开启包扫描@ComponentScan(“com.yf.cn”)
3.3在测试类中修改构建spring工厂类方法:
3.4继续测试TestSpringXml 类中的testRunMethod方法,发现也可以正常输出,说明基于javaConfig的方式配置完成